2017 © Pedro Peláez
 

yii2-extension yii2-aws-s3

An Amazon S3 component for Yii2

image

frostealth/yii2-aws-s3

An Amazon S3 component for Yii2

  • Thursday, November 30, 2017
  • by frostealth
  • Repository
  • 8 Watchers
  • 65 Stars
  • 82,477 Installations
  • PHP
  • 2 Dependents
  • 1 Suggesters
  • 22 Forks
  • 5 Open issues
  • 19 Versions
  • 12 % Grown

The README.md

Yii2 AWS S3

An Amazon S3 component for Yii2., (*1)

License Latest Stable Version Total Downloads Latest Unstable Version, (*2)

Yii2 AWS S3 uses SemVer., (*3)

Version 2.x requires PHP 7. For PHP less 7.0 use 1.x., (*4)

Installation

  1. Run the Composer command to install the latest version:, (*5)

    composer require frostealth/yii2-aws-s3 ~2.0
    
  2. Add the component to config/main.php, (*6)

    'components' => [
        // ...
        's3' => [
            'class' => 'frostealth\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',
        ],
        // ...
    ],
    

Basic usage

Usage of the command factory and additional params

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

Asynchronous execution

/** @var \frostealth\yii2\aws\s3\Service $s3 */
$s3 = Yii::$app->get('s3');

/** @var \GuzzleHttp\Promise\PromiseInterface $promise */
$promise = $s3->commands()->get('filename.ext')->async()->execute();

$promise = $s3->commands()->put('filename.ext', 'body')->async()->execute();

$promise = $s3->commands()->delete('filename.ext')->async()->execute();

$promise = $s3->commands()->upload('filename.ext', 'source')->async()->execute();

$promise = $s3->commands()->list('path/')->async()->execute();

Advanced usage

/** @var \frostealth\yii2\aws\s3\interfaces\Service $s3 */
$s3 = Yii::$app->get('s3');

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

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., (*7)

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., (*8)

Every handler must extend the Handler class or implement the Handler interface. Handlers gets the S3Client instance into its constructor., (*9)

The implementation of the HasBucket and HasAcl interfaces allows the command builder to set the values of bucket and acl by default., (*10)

To make the plain commands asynchronously, you have to implement the Asynchronous interface. Also, you can use the Async trait to implement this interface., (*11)

Consider the following command:, (*12)

<?php

namespace app\components\s3\commands;

use frostealth\yii2\aws\s3\base\commands\traits\Options;
use frostealth\yii2\aws\s3\interfaces\commands\Command;
use frostealth\yii2\aws\s3\interfaces\commands\HasBucket;

class MyCommand implements Command, HasBucket
{
    use Options;

    protected $bucket;

    protected $something;

    public function getBucket()
    {
        return $this->bucket;
    }

    public function inBucket(string $bucket)
    {
        $this->bucket = $bucket;

        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:, (*13)

<?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->getBucket(),
            $command->getSomething(),
            $command->getOptions()
        );
    }
}

And usage this command:, (*14)

/** @var \frostealth\yii2\aws\s3\interfaces\Service */
$s3 = Yii::$app->get('s3');

/** @var \app\components\s3\commands\MyCommand $command */
$command = $s3->create(MyCommand::class);
$command->withSomething('some value')->withOption('OptionName', 'value');

/** @var \Aws\ResultInterface $result */
$result = $s3->execute($command);

Custom plain command looks like this:, (*15)

<?php

namespace app\components\s3\commands;

use frostealth\yii2\aws\s3\interfaces\commands\HasBucket;
use frostealth\yii2\aws\s3\interfaces\commands\PlainCommand;

class MyPlainCommand implements PlainCommand, HasBucket
{
    protected $args = [];

    public function getBucket()
    {
        return $this->args['Bucket'] ?? '';
    }

    public function inBucket(string $bucket)
    {
        $this->args['Bucket'] = $bucket;

        return $this;
    }

    public function getSomething()
    {
        return $this->args['something'] ?? '';
    }

    public function withSomething($something)
    {
        $this->args['something'] = $something;

        return $this;
    }

    public function getName(): string
    {
        return 'AwsS3CommandName';
    }

    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();., (*16)

License

Yii2 AWS S3 is licensed under the MIT License., (*17)

See the LICENSE file for more information., (*18)

The Versions

30/11 2017

2.x-dev

2.9999999.9999999.9999999-dev https://github.com/frostealth/yii2-aws-s3

An Amazon S3 component for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 s3 aws aws-s3 yii2-s3 yii2-aws

30/11 2017

v2.1.1

2.1.1.0 https://github.com/frostealth/yii2-aws-s3

An Amazon S3 component for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 s3 aws aws-s3 yii2-s3 yii2-aws

13/04 2017

v2.1.0

2.1.0.0 https://github.com/frostealth/yii2-aws-s3

An Amazon S3 component for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 s3 aws aws-s3 yii2-s3 yii2-aws

08/02 2017

v2.0.0

2.0.0.0 https://github.com/frostealth/yii2-aws-s3

An Amazon S3 component for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 s3 aws aws-s3 yii2-s3 yii2-aws

07/10 2016

v2.0.0-rc

2.0.0.0-RC https://github.com/frostealth/yii2-aws-s3

An Amazon S3 component for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 s3 aws aws-s3 yii2-s3 yii2-aws

07/10 2016

v2.0.0-beta.4

2.0.0.0-beta4 https://github.com/frostealth/yii2-aws-s3

An Amazon S3 component for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 s3 aws aws-s3 yii2-s3 yii2-aws

30/05 2016

v2.0.0-beta.3

2.0.0.0-beta3 https://github.com/frostealth/yii2-aws-s3

An Amazon S3 component for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 s3 aws aws-s3 yii2-s3 yii2-aws

28/04 2016

v2.0.0-beta.2

2.0.0.0-beta2 https://github.com/frostealth/yii2-aws-s3

An Amazon S3 component for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 s3 aws aws-s3 yii2-s3 yii2-aws

11/04 2016

v2.0.0-beta.1

2.0.0.0-beta1 https://github.com/frostealth/yii2-aws-s3

An Amazon S3 component for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 s3 aws aws-s3 yii2-s3 yii2-aws

09/04 2016

v2.0.0-beta

2.0.0.0-beta https://github.com/frostealth/yii2-aws-s3

An Amazon S3 component for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 s3 aws aws-s3 yii2-s3 yii2-aws

08/04 2016

1.x-dev

1.9999999.9999999.9999999-dev https://github.com/frostealth/yii2-aws-s3

An Amazon S3Client wrapper as Yii2 component

  Sources   Download

MIT

The Requires

 

yii2 s3 aws aws-s3

26/12 2015

v1.0.0

1.0.0.0 https://github.com/frostealth/yii2-aws-s3

An Amazon S3Client wrapper as Yii2 component

  Sources   Download

MIT

The Requires

 

yii2 s3 aws aws-s3

06/12 2015

v0.4.4

0.4.4.0 https://github.com/frostealth/yii2-aws-s3

An Amazon S3Client wrapper as Yii2 component

  Sources   Download

MIT

The Requires

 

yii2 s3 aws aws-s3

05/11 2015

v0.4.3

0.4.3.0 https://github.com/frostealth/yii2-aws-s3

An Amazon S3Client wrapper as Yii2 component

  Sources   Download

MIT

The Requires

 

yii2 s3 aws aws-s3

03/06 2015

v0.4.2

0.4.2.0 https://github.com/frostealth/yii2-aws-s3

An Amazon S3Client wrapper as Yii2 component

  Sources   Download

MIT

The Requires

 

yii2 s3 aws-s3

02/06 2015

v0.4.1

0.4.1.0 https://github.com/frostealth/yii2-aws-s3

An Amazon S3Client wrapper as Yii2 component

  Sources   Download

MIT

The Requires

 

yii2 s3 aws-s3

31/05 2015

v0.3.0

0.3.0.0 https://github.com/frostealth/yii2-aws-s3

An Amazon S3Client wrapper as Yii2 component

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 s3 aws-s3

09/05 2015

v0.2.0

0.2.0.0 https://github.com/frostealth/yii2-aws-s3

Yii2 AWS S3

  Sources   Download

MIT

The Requires

 

by Constantin Chuprik

yii2 s3 aws-s3

30/04 2015

v0.1.0

0.1.0.0

Yii2 AWS S3 component

  Sources   Download

MIT

The Requires

 

by Constantin Chuprik

yii2 s3 aws-s3