indielab / yii2s3
Yii 2 S3 Component
Package info
Type:yii2-extension
pkg:composer/indielab/yii2s3
Requires
- php: ^8.4
- aws/aws-sdk-php: ^3.387
- yiisoft/yii2: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.40
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^11.1
- rector/rector: ^1.0
This package is auto-updated.
Last update: 2026-07-02 19:48:56 UTC
README
This component allows you to work with the amazon AWS S3 buckets for uploading and finding files.
Installation
Add the the package to your composer file:
composer require indielab/yii2s3
Add the component to your application configuration file:
'components' => [ // ... 's3' => [ 'class' => \indielab\yii2s3\S3::class, 'bucket' => 'mybucket', 'key' => 'KEY', 'secret' => 'SECRET', 'region' => 'eu-central-1', ], // ... ]
Usage
Using the component in order to upload a file:
Yii::$app->s3->upload('path/to/the/file.jpg');
Where file.jpg will be used as the key of the uploading file. Now in order to get the url to a key use:
$url = Yii::$app->s3->url('file.jpg');
Configure Uploading
You can also provide more options to the uploading configuration method:
Yii::$app->s3->upload('path/to/the/file.jpg', [ 'override' => true, // whether existing file should be overriden or not 'Key' => 'CacheControlTestFile.txt', // Define a specific name for the file instead of the source file name 'CacheControl' => 'max-age=' . strtotime('+1 year') // Add cache controler options ]);
Object Ownership / ACL
By default files are uploaded with the public-read ACL. Buckets created with the "Bucket owner enforced" Object
Ownership setting have ACLs disabled, and passing any ACL will make the upload fail with AccessControlListNotSupported.
Set acl to false to omit the ACL parameter entirely for those buckets:
's3' => [ 'class' => \indielab\yii2s3\S3::class, 'bucket' => 'mybucket', 'key' => 'KEY', 'secret' => 'SECRET', 'region' => 'eu-central-1', 'acl' => false, // omit the ACL parameter (required for buckets with ACLs disabled) ],