方法Aws \ S3 \ S3Client :: putObject()不存在

时间:2017-07-17 06:33:37

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

在我的应用程序中,我正在尝试将图像从本地目录上传到亚马逊S3存储桶,然后首先上传。当我上传文件时,我收到错误。我不知道该改变什么。有没有什么可以改变或添加供应商?请帮忙。

控制器:

$s = new Storage();
$result = $s->upload($bucket,$keyname,$filepath);

模型\ Storage.php:

<?php
namespace app\models;
use Yii;
use yii\base\Model;

class Storage extends Model
{

  private $aws;
  private $s3;

   function __construct() {
     $this->aws = Yii::$app->awssdk->getAwsSdk();
     $this->s3 = $this->aws->createS3();
  }

    public function upload($bucket,$keyname,$filepath) {
        $result = $this->s3->putObject(array(
        'Bucket'       => $bucket,
        'Key'          => $keyname,
        'SourceFile'   => $filepath,
        'ContentType'  => 'text/plain',
        'ACL'          => 'public-read',
        'StorageClass' => 'REDUCED_REDUNDANCY',
        'Metadata'     => array(
            'param1' => 'value 1',
            'param2' => 'value 2'
        )
    ));
    return $result;

    }

错误如下:

  

处理其他错误时出错:

     

例外&#39; ReflectionException&#39;使用消息&#39;方法   Aws \ S3 \ S3Client :: putObject()不存在&#39;在   d:\ XAMPP \ htdocs中\ teespring-testmailer \厂商\ yiisoft \ yii2 \网络\ ErrorHandler.php:195

1 个答案:

答案 0 :(得分:0)

我在 config \ web.php 中给出了错误的地区名称。这就是问题。我改变了地区,然后运作良好。

'awssdk' => [
            'class' => 'fedemotta\awssdk\AwsSdk',
            'credentials' => [ //you can use a different method to grant access
                'key' => 'XXXXXXXXXXXXXXXXX',
                'secret' => 'XXXXXXXXXXXXXXXXXXXXX',
            ],
            'region' => 'us-west-2', //before 'us-east-1'
            'version' => 'latest', 
        ],