为 Yii2 的 DigitalOcean Spaces 组件

1.0.2 2022-01-19 14:37 UTC

This package is auto-updated.

Last update: 2024-09-19 20:58:53 UTC


README

为 Yii2 的 DigitalOcean Spaces 组件。基于 frostealth/yii2-aws-s3

License

安装

  1. 运行 Composer 命令以安装最新版本

    composer require nurielmeni/yii2-digitalocean-spaces
  2. 将组件添加到配置

    'components' => [
        // ...
        'storage' => [
            'class' => 'nurielmeni\spaces\Service',
            'credentials' => [
                'key' => 'my-key',
                'secret' => 'my-secret',
            ],
            'region' => 'nyc3', // currently available: nyc3, ams3, sgp1, sfo2
            'defaultSpace' => 'my-space',
            'defaultAcl' => 'public-read',
        ],
        // ...
    ],

基本用法

使用命令工厂和额外的参数

/** @var \nurielmeni\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();

简短语法

/** @var \nurielmeni\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');

异步执行

/** @var \nurielmeni\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();

高级用法

/** @var \nurielmeni\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());

自定义命令

命令有两种类型:由 PlainCommandHandler 处理的普通命令和具有自己处理器的命令。普通命令封装了本机 AWS S3 命令。

普通命令必须实现 PlainCommand 接口,其余的必须实现 Command 接口。如果命令没有实现 PlainCommand 接口,它必须有自己的处理器。

每个处理器都必须扩展 Handler 类或实现 Handler 接口。处理器在构造函数中获取 S3Client 实例。

实现 HasSpaceHasAcl 接口允许命令构建器默认设置空间和 acl 的值。

要使普通命令异步,您必须实现 Asynchronous 接口。此外,您还可以使用 Async 特性来实现此接口。

考虑以下命令

<?php

namespace app\components\s3\commands;

use nurielmeni\spaces\base\commands\traits\Options;
use nurielmeni\spaces\interfaces\commands\Command;
use nurielmeni\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;
    }
}

此命令的处理程序看起来像这样

<?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()
        );
    }
}

使用此命令的方式

/** @var \nurielmeni\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);

自定义普通命令看起来像这样

<?php

namespace app\components\s3\commands;

use nurielmeni\spaces\interfaces\commands\HasSpace;
use nurielmeni\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;
    }
}

任何命令都可以扩展 ExecutableCommand 类或实现 Executable 接口,这将允许立即执行此命令:$command->withSomething('some value')->execute();

许可

Yii2 DigitalOcean Spaces 采用 MIT 许可证。

有关更多信息,请参阅 LICENSE 文件。