将amazon AWS与yii 2.0集成

时间:2015-04-21 05:57:47

标签: php amazon-web-services amazon-s3 yii2 aws-sdk

如何将我的Yii 2.0项目与Aws集成? 我用composer

安装了它
"aws/aws-sdk-php": "2.*",

并包含

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

但是当我尝试实例化我的S3客户端时,它一直告诉我Aws不存在。

4 个答案:

答案 0 :(得分:2)

您可以参考github上的以下链接

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

它详细介绍了如何使用S3 SDK和Yii 2 App。

答案 1 :(得分:1)

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

答案 2 :(得分:1)

运行Composer命令以安装s3扩展名。作曲家需要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"];

答案 3 :(得分:0)

我使用composer重新导入我的扩展, 并添加

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

不知何故,我通过在json composer中添加'autoload'来实现它的工作

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

然后运行

php composer.phar dumpautoload