PHPackages                             diecoding/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. diecoding/yii2-aws-s3

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

diecoding/yii2-aws-s3
=====================

Amazon S3 or Amazon Simple Storage Service component for Yii2

v1.0.6(3y ago)44.3k↓57.3%1[1 issues](https://github.com/wanforge/yii2-aws-s3/issues)MITPHPPHP &gt;=7.4.0

Since Apr 3Pushed 3y ago1 watchersCompare

[ Source](https://github.com/wanforge/yii2-aws-s3)[ Packagist](https://packagist.org/packages/diecoding/yii2-aws-s3)[ GitHub Sponsors](https://github.com/sugeng-sulistiyawan)[ RSS](/packages/diecoding-yii2-aws-s3/feed)WikiDiscussions main Synced today

READMEChangelog (7)Dependencies (3)Versions (8)Used By (0)

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

[](#yii2-aws-s3)

Amazon S3 or Amazon Simple Storage Service component for Yii2.

[![Latest Stable Version](https://camo.githubusercontent.com/bb76f62f7b86c54ba43e43ae911be47eaa29c8579ae9ed5f79cbd6b998121948/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646965636f64696e672f796969322d6177732d73333f6c6162656c3d737461626c65)](https://packagist.org/packages/diecoding/yii2-aws-s3)[![Total Downloads](https://camo.githubusercontent.com/14e09e837ddbdda6fb694d73bc3a292bfb8d2ea0d4ada708e62cca32c57a7b2a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646965636f64696e672f796969322d6177732d7333)](https://packagist.org/packages/diecoding/yii2-aws-s3)[![Latest Stable Release Date](https://camo.githubusercontent.com/688df70edbe738a9089c36a7d66dcd63fafb4acf501a8ea98bcd65ab82903321/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652d646174652f737567656e672d73756c69737469796177616e2f796969322d6177732d7333)](https://github.com/sugeng-sulistiyawan/yii2-aws-s3)[![Quality Score](https://camo.githubusercontent.com/0c1e283b161214f38d4a6fad71002fa3b2f8604d2c144cb6bb919d40645a6152/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f737567656e672d73756c69737469796177616e2f796969322d6177732d7333)](https://scrutinizer-ci.com/g/sugeng-sulistiyawan/yii2-aws-s3)[![Build Status](https://camo.githubusercontent.com/fd18f38f8aa7d2602e74e2567a664ab00137c81ac4b995bdad261969f4f55965/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6d2f737567656e672d73756c69737469796177616e2f796969322d6177732d7333)](https://app.travis-ci.com/sugeng-sulistiyawan/yii2-aws-s3)[![License](https://camo.githubusercontent.com/c68aeef1ed0d006f638d3ac8e7cd91e25bafd56c1d0f04a9a1fd7088f75d9c63/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f737567656e672d73756c69737469796177616e2f796969322d6177732d7333)](https://github.com/sugeng-sulistiyawan/yii2-aws-s3)[![PHP Version Require](https://camo.githubusercontent.com/7b1b25e70530b907a5103c127af047f0447a4d3c6f131da183ed9532e12d5de0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f646965636f64696e672f796969322d6177732d73332f7068703f636f6c6f723d366637336136)](https://packagist.org/packages/diecoding/yii2-aws-s3)

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

Table of Contents
-----------------

[](#table-of-contents)

- [Yii2 AWS S3](#yii2-aws-s3)
    - [Table of Contents](#table-of-contents)
    - [Instalation](#instalation)
    - [Dependencies](#dependencies)
    - [Configuration](#configuration)
    - [Usage](#usage)
        - [Basic Usage](#basic-usage)
        - [Advanced Usage](#advanced-usage)
        - [Custom Commands](#custom-commands)
    - [Using Traits](#using-traits)
        - [Model Trait](#model-trait)
            - [Using Trait Methods](#using-trait-methods)
            - [Overriding Trait Methods](#overriding-trait-methods)
                - [getS3Component](#gets3component)
                - [attributePaths](#attributepaths)
                - [getPresignedUrlDuration](#getpresignedurlduration)
                - [isSuccessResponseStatus](#issuccessresponsestatus)

Instalation
-----------

[](#instalation)

Package is available on [Packagist](https://packagist.org/packages/diecoding/yii2-aws-s3), you can install it using [Composer](https://getcomposer.org).

```
composer require diecoding/yii2-aws-s3 "^1.0"
```

or add to the require section of your `composer.json` file.

```
"diecoding/yii2-aws-s3": "^1.0"
```

Dependencies
------------

[](#dependencies)

- PHP 7.4+
- [yiisoft/yii2](https://github.com/yiisoft/yii2)
- [aws/aws-sdk-php](https://github.com/aws/aws-sdk-php)

Configuration
-------------

[](#configuration)

1. Add the component to `config/main.php`

    ```
    'components' => [
        // ...
        's3' => [
            'class' => \diecoding\aws\s3\Service::class,
            'endpoint' => 'my-endpoint',
            'usePathStyleEndpoint' => true,
            'credentials' => [ // Aws\Credentials\CredentialsInterface|array|callable
                'key' => 'my-key',
                'secret' => 'my-secret',
            ],
            'region' => 'my-region',
            'defaultBucket' => 'my-bucket',
            'defaultAcl' => 'public-read',
        ],
        // ...
    ],
    ```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

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

// Usage of the command factory and additional params
// ==================================================

/** @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 \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 \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 \diecoding\aws\s3\Service $s3 */
$s3 = Yii::$app->get('s3');
// or
$s3 = Yii::$app->s3;

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

```
