bp-sys/yii2-aws-s3
Composer 安装命令:
composer require bp-sys/yii2-aws-s3
包简介
An Amazon S3 component for Yii2
README 文档
README
An Amazon S3 component for Yii2.
Yii2 AWS S3 uses SemVer.
Version 2.x requires PHP 7. For PHP less 7.0 use 1.x.
About this project
This project is a fork from the excellent project yii2-aws-s3 by frostealth and adnsio.
Upon their work, we add support for IAM role attached to the EC2, which you don't need to insert your credentials.
We will also add support for easier integration with your models, by adding a S3MediaTrait.
Installation
-
Run the Composer command to install the latest version:
composer require bp-sys/yii2-aws-s3 ~2.0 -
Add the component to
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' => 'http://localhost:9000', ], // ... ],
Credentials parameter is optional: if you plan to use IAM roles attached to your EC2 instance there is no need for credentials. Just remove this parameter. Just be cautious if you need those credentials, this may cause errors during execution. Endpoint parameter is optional: You can specify a custom endpoint here. Great for debugging with minio for example.
Basic usage
Usage of the command factory and additional params
/** @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();
Short syntax
/** @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
Advanced usage
/** @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());
Using Traits
Attach the Trait to the model with some media attribute that will be saved in 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 );
License
Yii2 AWS S3 is licensed under the MIT License.
See the LICENSE file for more information.
bp-sys/yii2-aws-s3 适用场景与选型建议
bp-sys/yii2-aws-s3 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 96.62k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2020 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「s3」 「aws」 「yii2」 「yii2-s3」 「aws-s3」 「yii2-aws」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bp-sys/yii2-aws-s3 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bp-sys/yii2-aws-s3 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bp-sys/yii2-aws-s3 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
This package includes a script and fail2ban configuration that allows you to use fail2ban when utilizing AWS elastic load balancer (ELB) and an apache webserver.
Elastic Driver for Laravel Scout
An Yandex Cloud component for Yii2
simple api library.
An Amazon S3Client wrapper as Yii2 component
统计信息
- 总下载量: 96.62k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 16
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-07