PHPackages                             nandorodpires/yii2-aws-s3 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. nandorodpires/yii2-aws-s3

ActiveYii2-extension[File &amp; Storage](/categories/file-storage)

nandorodpires/yii2-aws-s3
=========================

An Amazon S3 component for Yii2

v1.0.0(3y ago)014MITPHPPHP &gt;=7.0

Since Aug 4Pushed 3y ago1 watchersCompare

[ Source](https://github.com/nandorodpires/yii2-aws-s3)[ Packagist](https://packagist.org/packages/nandorodpires/yii2-aws-s3)[ RSS](/packages/nandorodpires-yii2-aws-s3/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Yii2 AWS S3
===========

[](#yii2-aws-s3)

An Amazon S3 component for Yii2.

[![License](https://camo.githubusercontent.com/de05aa42f55410bae65fd21f3ed2df08f5856572d2a4a112c41c2917400e88a7/68747470733a2f2f706f7365722e707567782e6f72672f6e616e646f726f6470697265732f796969322d6177732d73332f6c6963656e7365)](https://github.com/nandorodpires/yii2-aws-s3/blob/2.x/LICENSE)[![Latest Stable Version](https://camo.githubusercontent.com/59234b92e7aad36c4f796984142df72814522acb0e347db5fdd1ad84a9bf1d0b/68747470733a2f2f706f7365722e707567782e6f72672f6e616e646f726f6470697265732f796969322d6177732d73332f762f737461626c65)](https://packagist.org/packages/nandorodpires/yii2-aws-s3)[![Total Downloads](https://camo.githubusercontent.com/aaf621cc2ff176115b8897dde728906532a1b621dbd4b8d46519f05792be6ab1/68747470733a2f2f706f7365722e707567782e6f72672f6e616e646f726f6470697265732f796969322d6177732d73332f646f776e6c6f616473)](https://packagist.org/packages/nandorodpires/yii2-aws-s3)[![Latest Unstable Version](https://camo.githubusercontent.com/d3646c800b07377f5f7214301ad5ea165ae148b0d01471d5e262c9b97d05d237/68747470733a2f2f706f7365722e707567782e6f72672f6e616e646f726f6470697265732f796969322d6177732d73332f762f756e737461626c65)](https://packagist.org/packages/nandorodpires/yii2-aws-s3)

> Yii2 AWS S3 uses [SemVer](http://semver.org/).

> Version 2.x requires PHP 7. For PHP less 7.0 use [1.x](https://github.com/nandorodpires/yii2-aws-s3/tree/1.x).

Installation
------------

[](#installation)

1. Run the [Composer](http://getcomposer.org/download/) command to install the latest version:

    ```
    composer require nandorodpires/yii2-aws-s3 ~2.0
    ```
2. Add the component to `config/main.php`

    ```
    'components' => [
        // ...
        's3' => [
            'class' => 'nandorodpires\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
-----------

[](#basic-usage)

### Usage of the command factory and additional params

[](#usage-of-the-command-factory-and-additional-params)

```
/** @var \nandorodpires\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

[](#short-syntax)

```
/** @var \nandorodpires\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

[](#asynchronous-execution)

```
/** @var \nandorodpires\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
--------------

[](#advanced-usage)

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

/** @var \nandorodpires\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

[](#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 `HasBucket` and `HasAcl` interfaces allows the command builder to set the values of bucket 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:

```
