bilberrry/yii2-digitalocean-spaces
最新稳定版本:0.1.3
Composer 安装命令:
composer require bilberrry/yii2-digitalocean-spaces
包简介
DigitalOcean Spaces component for Yii2
README 文档
README
DigitalOcean Spaces component for Yii2. Based on frostealth/yii2-aws-s3.
Installation
-
Run the Composer command to install the latest version:
composer require bilberrry/yii2-digitalocean-spaces
-
Add the component to config:
'components' => [ // ... 'storage' => [ 'class' => 'bilberrry\spaces\Service', 'credentials' => [ 'key' => 'my-key', 'secret' => 'my-secret', ], 'region' => 'nyc3', // currently available: nyc3, ams3, sgp1, sfo2 'defaultSpace' => 'my-space', 'defaultAcl' => 'public-read', ], // ... ],
Basic usage
Usage of the command factory and additional params
/** @var \bilberrry\spaces\Service $storage */ $storage = Yii::$app->get('storage'); /** @var \Aws\ResultInterface $result */ $result = $storage->commands()->get('filename.ext')->saveAs('/path/to/local/file.ext')->execute(); $result = $storage->commands()->put('filename.ext', 'body')->withContentType('text/plain')->execute(); $result = $storage->commands()->delete('filename.ext')->execute(); $result = $storage->commands()->upload('filename.ext', '/path/to/local/file.ext')->withAcl('private')->execute(); $result = $storage->commands()->restore('filename.ext', $days = 7)->execute(); $result = $storage->commands()->list('path/')->execute(); /** @var bool $exist */ $exist = $storage->commands()->exist('filename.ext')->execute(); /** @var string $url */ $url = $storage->commands()->getUrl('filename.ext')->execute(); /** @var string $signedUrl */ $signedUrl = $storage->commands()->getPresignedUrl('filename.ext', '+2 days')->execute();
Short syntax
/** @var \bilberrry\spaces\Service $storage */ $storage = Yii::$app->get('storage'); /** @var \Aws\ResultInterface $result */ $result = $storage->get('filename.ext'); $result = $storage->put('filename.ext', 'body'); $result = $storage->delete('filename.ext'); $result = $storage->upload('filename.ext', '/path/to/local/file.ext'); $result = $storage->restore('filename.ext', $days = 7); $result = $storage->list('path/'); /** @var bool $exist */ $exist = $storage->exist('filename.ext'); /** @var string $url */ $url = $storage->getUrl('filename.ext'); /** @var string $signedUrl */ $signedUrl = $storage->getPresignedUrl('filename.ext', '+2 days');
Asynchronous execution
/** @var \bilberrry\spaces\Service $storage */ $storage = Yii::$app->get('storage'); /** @var \GuzzleHttp\Promise\PromiseInterface $promise */ $promise = $storage->commands()->get('filename.ext')->async()->execute(); $promise = $storage->commands()->put('filename.ext', 'body')->async()->execute(); $promise = $storage->commands()->delete('filename.ext')->async()->execute(); $promise = $storage->commands()->upload('filename.ext', 'source')->async()->execute(); $promise = $storage->commands()->list('path/')->async()->execute();
Advanced usage
/** @var \bilberrry\spaces\interfaces\Service $storage */ $storage = Yii::$app->get('storage'); /** @var \frostealth\yii2\aws\s3\commands\GetCommand $command */ $command = $storage->create(GetCommand::class); $command->inSpace('my-another-space')->byFilename('filename.ext')->saveAs('/path/to/local/file.ext'); /** @var \Aws\ResultInterface $result */ $result = $storage->execute($command); // or async /** @var \GuzzleHttp\Promise\PromiseInterface $promise */ $promise = $storage->execute($command->async());
Custom commands
Commands have two types: plain commands that's handled by the PlainCommandHandler and commands with their own handlers.
The plain commands wrap the native AWS S3 commands.
The plain commands must implement the PlainCommand interface and the rest must implement the Command interface.
If the command doesn't implement the PlainCommand interface, it must have its own handler.
Every handler must extend the Handler class or implement the Handler interface.
Handlers gets the S3Client instance into its constructor.
The implementation of the HasSpace and HasAcl interfaces allows the command builder to set the values
of space and acl by default.
To make the plain commands asynchronously, you have to implement the Asynchronous interface.
Also, you can use the Async trait to implement this interface.
Consider the following command:
<?php namespace app\components\s3\commands; use bilberrry\spaces\base\commands\traits\Options; use bilberrry\spaces\interfaces\commands\Command; use bilberrry\spaces\interfaces\commands\HasSpace; class MyCommand implements Command, HasSpace { use Options; protected $space; protected $something; public function getSpace() { return $this->space; } public function inSpace(string $space) { $this->space = $space; return $this; } public function getSomething() { return $this->something; } public function withSomething(string $something) { $this->something = $something; return $this; } }
The handler for this command looks like this:
<?php namespace app\components\s3\handlers; use app\components\s3\commands\MyCommand; use frostealth\yii2\aws\s3\base\handlers\Handler; class MyCommandHandler extends Handler { public function handle(MyCommand $command) { return $this->s3Client->someAction( $command->getSpace(), $command->getSomething(), $command->getOptions() ); } }
And usage this command:
/** @var \bilberrry\spaces\interfaces\Service */ $storage = Yii::$app->get('storage'); /** @var \app\components\s3\commands\MyCommand $command */ $command = $storage->create(MyCommand::class); $command->withSomething('some value')->withOption('OptionName', 'value'); /** @var \Aws\ResultInterface $result */ $result = $storage->execute($command);
Custom plain command looks like this:
<?php namespace app\components\s3\commands; use bilberrry\spaces\interfaces\commands\HasSpace; use bilberrry\spaces\interfaces\commands\PlainCommand; class MyPlainCommand implements PlainCommand, HasSpace { protected $args = []; public function getSpace() { return $this->args['Bucket'] ?? ''; } public function inSpace(string $space) { $this->args['Bucket'] = $space; return $this; } public function getSomething() { return $this->args['something'] ?? ''; } public function withSomething($something) { $this->args['something'] = $something; return $this; } public function getName(): string { return 'SpaceCommandName'; } public function toArgs(): array { return $this->args; } }
Any command can extend the ExecutableCommand class or implement the Executable interface that will
allow to execute this command immediately: $command->withSomething('some value')->execute();.
License
Yii2 DigitalOcean Spaces is licensed under the MIT License.
See the LICENSE file for more information.
bilberrry/yii2-digitalocean-spaces 适用场景与选型建议
bilberrry/yii2-digitalocean-spaces 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.38k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 09 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「digitalocean」 「yii2」 「spaces」 「do」 「yii2-spaces」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bilberrry/yii2-digitalocean-spaces 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bilberrry/yii2-digitalocean-spaces 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bilberrry/yii2-digitalocean-spaces 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Automatically copies media uploads to a storage provider's bucket for delivery. Optionally configure a CDN for even faster delivery.
Laravel S3 cache driver. Support: Amazon S3, Digital Ocean Spaces, etc.
A custom URL rule class for Yii 2 which allows to create translated URL rules
External storage driver for Imager X that integrates with DigitalOcean Spaces
DigitalOcean OAuth 2.0 Client Provider for The PHP League OAuth2-Client
A small and simple CSS gutter to create rows and cells using the flexbox model.
统计信息
- 总下载量: 17.38k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-19