fedemotta / yii2-aws-sdk
此扩展为 Yii2 框架提供 AWS SDK 集成
v2.0
2015-12-10 20:58 UTC
Requires
- aws/aws-sdk-php: 3.*
- yiisoft/yii2: 2.*
This package is not auto-updated.
Last update: 2024-09-25 15:06:22 UTC
README
此扩展为 Yii2 框架提供 AWS SDK 3 集成
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist fedemotta/yii2-aws-sdk "*"
或添加
"fedemotta/yii2-aws-sdk": "*"
到您的 composer.json
文件的 require 部分。
Note: You can still use AWS version 2 if you specify fedemotta/yii2-aws-sdk "1.*"
用法
要使用此扩展,只需在您的应用程序配置中添加以下代码
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 获取所有均衡器名称
$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 下载对象
//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'];