1. Go to this page and download the library: Download diecoding/yii2-aws-s3 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace app\components\s3\commands;
use diecoding\aws\s3\base\commands\traits\Options;
use diecoding\aws\s3\interfaces\commands\Command;
use diecoding\aws\s3\interfaces\commands\HasBucket;
class MyCommand implements Command, HasBucket
{
use Options;
protected $bucket;
protected $something;
public function getBucket()
{
return $this->bucket;
}
public function inBucket(string $bucket)
{
$this->bucket = $bucket;
return $this;
}
public function getSomething()
{
return $this->something;
}
public function withSomething(string $something)
{
$this->something = $something;
return $this;
}
}
namespace app\components\s3\handlers;
use app\components\s3\commands\MyCommand;
use diecoding\aws\s3\base\handlers\Handler;
class MyCommandHandler extends Handler
{
public function handle(MyCommand $command)
{
return $this->s3Client->someAction(
$command->getBucket(),
$command->getSomething(),
$command->getOptions()
);
}
}
namespace app\components\s3\commands;
use diecoding\aws\s3\interfaces\commands\HasBucket;
use diecoding\aws\s3\interfaces\commands\PlainCommand;
class MyPlainCommand implements PlainCommand, HasBucket
{
protected $args = [];
public function getBucket()
{
return $this->args['Bucket'] ?? '';
}
public function inBucket(string $bucket)
{
$this->args['Bucket'] = $bucket;
return $this;
}
public function getSomething()
{
return $this->args['something'] ?? '';
}
public function withSomething($something)
{
$this->args['something'] = $something;
return $this;
}
public function getName(): string
{
return 'AwsS3CommandName';
}
public function toArgs(): array
{
return $this->args;
}
}
/**
* @property string|null $file
*/
class Model extends \yii\db\ActiveRecord
{
use \diecoding\aws\s3\traits\ModelTrait;
// ...
public function rules()
{
return [
['image', 'string'], // Stores the filename
];
}
/**
* @inheritdoc
*/
protected function attributePaths()
{
return [
'image' => 'images/'
];
}
// ...
}
$image = \yii\web\UploadedFile::getInstance($model, 'image');
// Save image_thumb.* to S3 on //my_bucket/images/ path
// The extension of the file will be determined by the submitted file type
// This allows multiple file types upload (png,jpg,gif,...)
// $model->image will hold "image_thumb.png" after this call finish with success
$model->saveUploadedFile($image, 'image', 'image_thumb');
// Save image_thumb.png to S3 on //my_bucket/images/ path
// The extension of the file will be determined by the submitted file type
// This force the extension to *.png
$model->saveUploadedFile($image, 'image', 'image_thumb.png', false);
// Get the URL to the image on S3
$model->getFileUrl('image');
// Get the presigned URL to the image on S3
// The default duration is "+5 Minutes"
$model->getFilePresignedUrl('image');
// Remove the file with named saved on the image attribute
// Continuing the example, here "//my_bucket/images/my_image.png" will be deleted from S3
$model->removeFile('image');
public function getS3Component()
{
return Yii::$app->get('my_s3_component');
}
protected function attributePaths()
{
return [
'logo' => 'logos/',
'badge' => 'images/badges/'
];
}
// or use another attribute, example: id
// ! Note: id must contain a value first if you don't want it to be empty
protected function attributePaths()
{
return [
'logo' => 'thumbnail/' . $this->id . '/logos/',
'badge' => 'thumbnail/' . $this->id . '/images/badges/'
];
}
protected function getPresignedUrlDuration($attribute)
{
return '+2 Hours';
}
// or if you want to set the attribute differently
protected function getPresignedUrlDuration($attribute)
{
switch ($attribute) {
case 'badge':
return '+2 Hours';
break;
default:
return '+1 Days';
break;
}
}
protected function isSuccessResponseStatus($response)
{
// Response is always valid
return true;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.