Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

php - Upload file to S3 Bucket with AWS SDK NoSuchBucket error

Tried to upload file to S3 Bucket, i set up the SDK using the following code:

<?php

use AwsS3S3Client;

use AwsExceptionAwsException;

try {

    require 'vendor/autoload.php';

    $s3 = new AwsS3S3Client([
        '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;
}

Error log

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

Possible errors:

The URL should be this form based on: Documentation

https://s3.Region.amazonaws.com/bucket-name/key name
question from:https://stackoverflow.com/questions/65952234/upload-file-to-s3-bucket-with-aws-sdk-nosuchbucket-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think the trouble is with the S3 Client itself. Below is the example from the documentation PutObject.php


use AwsS3S3Client;  
use AwsExceptionAwsException;
....
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() . "
";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...