简体   繁体   中英

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 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;
}

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

I think the trouble is with the S3 Client itself. Below is the example from the documentation 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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM