bp-sys / yii2-aws-s3
Yii2 的 Amazon S3 组件
2.4.0
2022-08-21 22:57 UTC
Requires
- php: >=7.0.0
- aws/aws-sdk-php: ~3.17
- yiisoft/yii2: ~2.0
Requires (Dev)
- phpro/grumphp: ~0.8
- squizlabs/php_codesniffer: ~2.3
This package is auto-updated.
Last update: 2024-09-25 03:10:02 UTC
README
为 Yii2 提供的 Amazon S3 组件。
Yii2 AWS S3 使用 SemVer。
2.x 版本需要 PHP 7。对于低于 7.0 的 PHP,请使用 1.x。
关于本项目
本项目是从 frostealth 和 adnsio 的优秀项目 yii2-aws-s3 分支出来的。
基于他们的工作,我们添加了对附加到 EC2 的 IAM 角色的支持,您不需要插入凭证。
我们还将添加支持通过添加 S3MediaTrait 简化与您的模型的集成。
安装
-
运行 Composer 命令安装最新版本
composer require bp-sys/yii2-aws-s3 ~2.0
-
将组件添加到
config/main.php
'components' => [ // ... 's3' => [ 'class' => 'bpsys\yii2\aws\s3\Service', 'credentials' => [ // Aws\Credentials\CredentialsInterface|array|callable 'key' => 'my-key', 'secret' => 'my-secret', ], 'region' => 'my-region', 'defaultBucket' => 'my-bucket', 'defaultAcl' => 'public-read', 'defaultPresignedExpiration' => '+1 hour', 'endpoint' => 'https://:9000', ], // ... ],
凭证参数是可选的:如果您计划使用附加到您的 EC2 实例的 IAM 角色则无需凭证。只需删除此参数。如果您需要这些凭证,这可能会导致执行时出错。端点参数是可选的:您可以在此处指定自定义端点。例如,与 minio 调试时非常有用。
基本用法
命令工厂和额外参数的用法
/** @var \bpsys\yii2\aws\s3\Service $s3 */ $s3 = Yii::$app->get('s3'); /** @var \Aws\ResultInterface $result */ $result = $s3->commands()->get('filename.ext')->saveAs('/path/to/local/file.ext')->execute(); $result = $s3->commands()->put('filename.ext', 'body')->withContentType('text/plain')->execute(); $result = $s3->commands()->delete('filename.ext')->execute(); $result = $s3->commands()->upload('filename.ext', '/path/to/local/file.ext')->withAcl('private')->execute(); $result = $s3->commands()->restore('filename.ext', $days = 7)->execute(); $result = $s3->commands()->list('path/')->execute(); /** @var bool $exist */ $exist = $s3->commands()->exist('filename.ext')->execute(); /** @var string $url */ $url = $s3->commands()->getUrl('filename.ext')->execute(); /** @var string $signedUrl */ $signedUrl = $s3->commands()->getPresignedUrl('filename.ext', '+2 days')->execute();
简短语法
/** @var \bpsys\yii2\aws\s3\Service $s3 */ $s3 = Yii::$app->get('s3'); /** @var \Aws\ResultInterface $result */ $result = $s3->get('filename.ext'); $result = $s3->put('filename.ext', 'body'); $result = $s3->delete('filename.ext'); $result = $s3->upload('filename.ext', '/path/to/local/file.ext'); $result = $s3->restore('filename.ext', $days = 7); $result = $s3->list('path/'); /** @var bool $exist */ $exist = $s3->exist('filename.ext'); /** @var string $url */ $url = $s3->getUrl('filename.ext'); /** @var string $signedUrl */ $signedUrl = $s3->getPresignedUrl('filename.ext', '+2 days'); // Pass only one parameter to get expiration date from component defaults
高级用法
/** @var \bpsys\yii2\aws\s3\interfaces\Service $s3 */ $s3 = Yii::$app->get('s3'); /** @var \bpsys\yii2\aws\s3\commands\GetCommand $command */ $command = $s3->create(GetCommand::class); $command->inBucket('my-another-bucket')->byFilename('filename.ext')->saveAs('/path/to/local/file.ext'); /** @var \Aws\ResultInterface $result */ $result = $s3->execute($command); // or async /** @var \GuzzleHttp\Promise\PromiseInterface $promise */ $promise = $s3->execute($command->async());
使用特性
将特性附加到包含将保存到 S3 的媒体属性的模型上
class Person extends \yii\db\ActiveRecord { use \bpsys\yii2\aws\s3\traits\S3MediaTrait; // ... }
$image = \yii\web\UploadedFile::getInstance( $formModel, 'my_file_attribute' ); // Save image as my_image.png on S3 at //my_bucket/images/ path // $model->image will hold "my_image.png" after this call finish with success $model->saveUploadedFile( $image, 'image', 'my_image.png' ); // Get the URL to the image on S3 $model->getFileUrl( 'image' ); // Get the presigned URL to the image on S3 // The default duration is "+1 day" $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' ); // Save my_image.* 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->saveUploadedFile( $image, 'image', 'my_image', true );
许可证
Yii2 AWS S3 在 MIT 许可证下发布。
有关更多信息,请参阅 LICENSE 文件。