The code provides a layer above the Amazon S3 SDK to be used in Yii2 app. Following thing need to be done before using the code
-
Install the S3 SDK using composer. The file looks like follow
"require": { ... "aws/aws-sdk-php": "~3.0@dev" ... },
-
Once done you can find the aws sdk in app/vendor/aws
-
Add the following code to params.php file located in app/config/params.php
return [ ... 'amazon' => [ 'region' => 'ap-southeast-1', 'credentials' => [ 'key' => 'your_amazon_key', 'secret' => 'your_amazon_secrete', ], ], ... ]
You can modify your own region of the server.
- You can use the code by creating the object of the AwsS3 class.
- Uploading/adding object to amazon bucket.
$url = $object->uploadObject($source, $bucket, $key, $acl);
- Get presigned url for particular time.
$preSignedUrl = $object->getPreSignedUrl($bucket, $key, $timeInMinutes);
- Deleting an object from S3.
$success = $object->deleteObject($bucket, $key);
- Get/Download object
$data = $object->downloadObject($bucket, $key);