【问题标题】:Upload file to S3 Bucket with AWS SDK NoSuchBucket error使用 AWS 开发工具包 NoSuchBucket 错误将文件上传到 S3 存储桶
【发布时间】:2021-01-29 09:55:00
【问题描述】:

尝试将文件上传到 S3 Bucket,我使用以下代码设置了 SDK:

<?php

use Aws\S3\S3Client;

use Aws\Exception\AwsException;

try {

    require 'vendor/autoload.php';

    $s3 = new Aws\S3\S3Client([
        'region'  => 'us-east-1',
        'version' => 'latest',
        'credentials' => [
            'key'    => "KEY",
            'secret' => "SECRET",
        ]
    ]);

    $result = $s3->putObject([
        'Bucket' => 'bucketname',
        'Key'    => 'test.txt',
        'SourceFile' => 'test.txt'
    ]);

    var_dump($result);

} catch (\Throwable $th) {
    echo $th;
}

错误日志

AWS HTTP error: Client error: `PUT https://bucketname.s3.amazonaws.com/test.txt` resulted in a `404 Not Found` response: NoSuchBucketThe specified bucket does not exist

可能的错误:

URL 应基于以下格式: Documentation

https://s3.Region.amazonaws.com/bucket-name/key name

【问题讨论】:

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


    【解决方案1】:

    我认为问题出在S3 Client 本身。下面是来自文档PutObject.php的示例

    
    use Aws\S3\S3Client;  
    use Aws\Exception\AwsException;
    ....
    try {
        //Create a S3Client
        $s3Client = new S3Client([
            'profile' => 'default',
            'region' => 'us-west-2',
            'version' => '2006-03-01'
        ]);
        $result = $s3Client->putObject([
            'Bucket' => $bucket,
            'Key' => $key,
            'SourceFile' => $file_Path,
        ]);
    } catch (S3Exception $e) {
        echo $e->getMessage() . "\n";
    }
    

    【讨论】:

    • 配置文件只是切换到另一个密钥/秘密对,但我使用硬编码凭据,更多信息在这里:docs.aws.amazon.com/sdk-for-php/v3/developer-guide/…
    • @X11 仍然是Using Hard-Coded Credentials,检查链接,构造函数是如何被初始化的,并通过headbucket添加对存储桶存在的检查
    • 我从 headbucket 得到:“导致 404 Not Found 响应 NotFound”
    • @X11 然后使用您在脚本中添加的凭据通过命令行aws-cli s3 ls 将您的存储桶存在加倍
    • 我得到:“从调用 aws s3 ls 的 aws cli 调用 ListBuckets 操作时发生错误(AccessDenied):访问被拒绝”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 2018-12-15
    • 2018-04-25
    • 2017-08-08
    • 2018-09-07
    • 2014-09-20
    相关资源
    最近更新 更多