Integrate amazon AWS with yii 2.0

How do I integrate a Yii 2.0 project with Aws? I installed it with a composer

"aws/aws-sdk-php": "2.*",

and turned on

require '../vendor/aws/aws-autoloader.php';

But when I try to create an instance of my S3 client, it continues to tell me that Aws does not exist.

+4
source share
4 answers

You can link to the following link on github

https://github.com/JDpawar/yii2-aws-s3-sdk

It provides accurate information on how to use the S3 SDK with the Yii 2 app.

+2
source

Run the Composer command to install the s3 extension. composer requires frostealth / yii2-aws-s3 ~1.0@stable

Open common/config/main.php file and add below code into "components" section. "s3bucket" => [ "class" => \frostealth\yii2\aws\s3\Storage::className(), "region" => "Your region", "credentials" => [ "key" => "your aws s3 key", "secret" => "your aws s3 secret", ], "bucket" => "your aws s3 bucket", "defaultAcl" => \frostealth\yii2\aws\s3\Storage::ACL_PUBLIC_READ, "debug" => false, // bool|array ],

Use below code to upload image on s3 $s3 = Yii::$app->get('s3bucket')->upload('upload image name', 'path of local folder where image located');

After uploading you get status code and image url. you can get like below $status = $s3["@metadata"]["statusCode"]; $imageUrl = $s3["@metadata"]["effectiveUri"];
+1
source

AWS SDK Yii2 - - Amazon Yii2

AWS SDK 3 Yii2

composer.

php composer.phar --prefer-dist fedemotta/yii2-aws-sdk "*"

"fedemotta/yii2-aws-sdk": "*"

require composer.json.

. AWS 2, fedemotta/yii2-aws-sdk "1. *"

, :

<?php
return [
//....
'components' => [
    'awssdk' => [
        'class' => 'fedemotta\awssdk\AwsSdk',
        'credentials' => [ //you can use a different method to grant access
            'key' => 'your-aws-key',
            'secret' => 'your-aws-secret',
        ],
        'region' => 'your-aws-region', //i.e.: 'us-east-1'
        'version' => 'your-aws-version', //i.e.: 'latest'
    ],
],
];
?>

AWS:

<?php
$aws = Yii::$app->awssdk->getAwsSdk();
$elb = $aws->createElasticloadbalancing();
$load_balancers = $elb->describeLoadBalancers()->toArray();
if (isset($load_balancers['LoadBalancerDescriptions'])){
    foreach ($load_balancers['LoadBalancerDescriptions'] as $balancer){
        if (isset($balancer['LoadBalancerName'])){ 
            echo $balancer['LoadBalancerName'];
        }
    }
}
?>

S3:

<?php
//specify the region if it is different than the main configuration region
Yii::$app->awssdk->region = 'sa-east-1';
$aws = Yii::$app->awssdk->getAwsSdk();
//use s3
$s3 = $aws->createS3();
$result = $s3->listObjects(['Bucket' => 'your-bucket-id',
                            "Prefix" =>   "your-path"])->toArray();
//get the last object from s3
$object = end($result['Contents']);
$key = $object['Key'];
$file = $s3->getObject([
'Bucket' => 'your-bucket-id',
'Key' => $key
]);
//download the file
header('Content-Type: ' . $file['ContentType']);
echo $file['Body'];
?>
0

I reimport ,

require (\Yii::getAlias('@vendor/autoload.php'));

- , "" json composer

"autoload": {
        "psr-4": {
            "vendor\\aws\\" :""
        }
    }

php composer.phar dumpautoload
0

All Articles