Karate_Dog
Karate_Dog

Reputation: 1277

Integrate amazon AWS with yii 2.0

How do I integrate my Yii 2.0 project with Aws ? I have installed it using composer

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

and included the

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

But when I try to instantiate my S3 client, it keeps telling me that Aws does not exist.

Upvotes: 4

Views: 7605

Answers (5)

fighcell
fighcell

Reputation: 21

Was looking around for this for a few hours and only found other packages to solve the issue. Wanted to implement the AWS package directly. So from

Installed the latest aws-sdk via composer.

I am using "aws/aws-sdk-php": "^3.259"

Make sure aws source and location is correct in the vendor/composer/autoload_psr4.php 'Aws\\' => array($vendorDir . '/aws/aws-sdk-php/src')

After that ran composer update as mentioned by @Karate_Dog

    php composer.phar dumpautoload

Upvotes: 0

Patrick R
Patrick R

Reputation: 6857

Run the Composer command to install s3 extension. composer require 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"];

Upvotes: 1

Federico Nicolas Motta
Federico Nicolas Motta

Reputation: 129

AWS SDK for Yii2 - Use Amazon Web Services in your Yii2 project

This extension provides the AWS SDK 3 integration for the Yii2 framework

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

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

to the require section of your composer.json file.

Note: You can still use AWS version 2 if you specify fedemotta/yii2-aws-sdk "1.*"

Usage

To use this extension, simply add the following code in your application configuration:

<?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'
    ],
],
];
?>

Getting all balancer names from 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'];
        }
    }
}
?>

Download an object from 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'];
?>

Upvotes: 1

JDpawar
JDpawar

Reputation: 3574

You can refer the following link on github

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

It has the exact details how to use the S3 SDK along with the Yii 2 App.

Upvotes: 2

Karate_Dog
Karate_Dog

Reputation: 1277

I reimport my extension using composer, and adding

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

Somehow I got it working by adding 'autoload' in json composer

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

and then run

php composer.phar dumpautoload

Upvotes: 0

Related Questions