dozxos6346 2018-09-08 23:44
浏览 392
已采纳

使用AWS PHP SDK上传到AWS S3无法通过身份验证

I am using version 3.67.5 of AWS SDK PHP to upload files to S3. The code is in an Heroku dyno.

I've created the access keys, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, and set them as configuration variables in the Heroku instance. I know that they are there, because with getenv() I 've outputted them.

I'm using the following code:

try {
   //Create a S3Client
   $s3Client = new S3Client([
        'profile' => 'default',
        'region' => 'some-region',
        'version' => 'latest'
    ]);
    $result = $s3Client->putObject([
        'Bucket'     => 'some-bucket',
        'Key'        => $fileName,
        'SourceFile' => $this->getParameter('photos_directory') . $fileName,
    ]);
} catch (AwsException $e) {
    echo $e->getMessage() . "
";
}

Still, uploading a file, it shows the following error:

request.CRITICAL: Uncaught PHP Exception Aws\Exception\CredentialsException: "Cannot read credentials from /app/.aws/credentials"

I've tested passing a new element 'credentials' in the constructor, like they do in theirs unit tests, and the error is the same.

  • 写回答

2条回答 默认 最新

  • dongri1989 2018-09-09 22:36
    关注

    The problem was in defining the profile as "default". When I removed the profile parameter, the error disappeared.

    When using the latest version, 3.67.8, the following error occurred:

    AWS HTTP error: Client error: `PUT https: // some-url` resulted in a` 403 Forbidden`
    

    Anyway, in my case, and in both versions, omitting the profile parameter was the solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 专家修改了标签 8月18日