mamadali / yii2-s3-storage
A package for yii2 to store files on s3 storage
1.0.0
2024-09-08 05:58 UTC
Requires
- php: ^8.0
- aws/aws-sdk-php: 3.316.2
- yiisoft/yii2: ^2.0
README
Yii2 S3 Storage is a Yii2 component that provides an easy way to store files on Amazon S3.
Installation
The preferred way to install this extension is through composer.
Either run
composer require mamadali/yii2-s3-storage
or add
"mamadali/yii2-s3-storage": "*"
to the require section of your composer.json
file.
and run composer update
then run migrations
php yii migrate/up --migrationPath=@vendor/mamadali/yii2-s3-storage/src/migrations
Basic usage
add s3 component to components
section of config file
'components' => [ ... 's3storage' => [ 'class' => 'mamadali\S3Storage\components\S3Storage', 'key' => // your access key 'secret' => // your secret key 'endpoint' => // your endpoint 'default_bucket_name' => // your bucket name 'bucket_domain' => // Optional: your bucket domain ], ... ];
add behavior to your model
public function behaviors() { return [ ... [ 'class' => StorageUploadBehavior::class, 'attributes' => ['file'], 'scenarios' => [self::SCENARIO_UPLOAD], 'path' => 'path/model_class/{id}' ], ... ]; }
then just add file input to your form and save model in controller
<?= $form->field($model, 'file')->fileInput() ?>
$model->save();
get usage of storage
S3Storage::formatUsageSpace(Yii::$app->s3storage->getTotalUsage()); // return "1 MB"
S3Storage::formatUsageSpace(Yii::$app->s3storage->getUsageByModelClass(Model::class));
Yii::$app->s3storage->getUsageSeperatedByModelClass(); /** * return [ * [ * 'model_class' => 'common\models\Model', * 'size' => 1048576 // bytes * ], * ] */