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

使用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日

悬赏问题

  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载