php 整合亚马逊AWS与yii 2.0

30byixjq  于 2023-02-21  发布在  PHP
关注(0)|答案(5)|浏览(169)

如何将Yii 2.0项目集成到AWS?我已经使用Composer安装了它

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

包括

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

但是当我试图示例化我的S3客户机时,它一直告诉我Aws不存在。

vlf7wbxs

vlf7wbxs1#

您可以在github上引用以下链接
https://github.com/JDpawar/yii2-aws-s3-sdk
它包含了如何使用S3 SDK沿着Yii 2 App的详细信息。

e5nszbig

e5nszbig2#

AWS SDK for Yii2-在您的Yii2项目中使用亚马逊网络服务
该扩展为Yii2框架提供AWS SDK 3集成

安装

安装此扩展的首选方法是通过composer
无论运行
php composer . phar要求--首选-距离fedemetta/yii2-aws-sdk ""
或添加
"费德莫塔/yii2-aws-sdk":"
"
composer.json文件的require部分。
注意:如果指定fedemetta/yii2-aws-sdk "1. *",您仍然可以使用AWS版本2。

用法

要使用此扩展,只需在应用程序配置中添加以下代码:

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

htzpubme3#

运行Composer命令安装s3扩展,composer需要frostealth/yii 2-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"];
anauzrmj

anauzrmj4#

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

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

不知何故,我通过在json composer中添加“autoload”使它工作

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

然后运行

php composer.phar dumpautoload
qvsjd97n

qvsjd97n5#

我四处寻找了几个小时,只找到了其他包来解决这个问题。想直接实现AWS包。所以从
通过composer安装了最新的aws-sdk。
我使用的是"aws/aws-sdk-php": "^3.259"
确保aws源代码和位置在vendor/composer/autoload_psr4.php 'Aws\\' => array($vendorDir . '/aws/aws-sdk-php/src')中正确
之后,运行@Karate_Dog提到的 composer 更新

php composer.phar dumpautoload

相关问题