Juned Ansari
Juned Ansari

Reputation: 5283

unable to upload file to sub-folder of main bucket

I am trying to upload error file in AWSS3 but it shows error like "The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint: "test9011960909.s3.amazonaws.com"."

i also specified 'region' => 'us-east-1' but still same error occurs.

it is working when i specify

'Bucket'       => $this->bucket,

but i wanted to upload file in sub-folder of main bucket

'Bucket'       => $this->bucket . "/" . $PrefixFolderPath,

i already applied approved answer from AWS S3: The bucket you are attempting to access must be addressed using the specified endpoint

but still getting same error, i am using php

Code :

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

class AWSS3Factory {

    private $bucket;
    private $keyname;

    public function __construct() {
        $this->bucket = AWSS3_BucketName;
        $this->keyname = AWSS3_AccessKey;
        // Instantiate the client.
    }

    public function UploadFile($FullFilePath,$PrefixFolderPath="") {
        try {
            $s3 = S3Client::factory(array(
                'credentials' => array(
                    'key'    => MYKEY,
                    'secret' => MYSECKEY,
                    'region' => 'eu-west-1',
                )
            ));

            // Upload data.
            $result = $s3->putObject(array(
                'Bucket'       => $this->bucket . "/" . $PrefixFolderPath,
                'Key'          => $this->keyname,
                'SourceFile'   => $FullFilePath,
                'StorageClass' => 'REDUCED_REDUNDANCY'
            ));
            return true;
            // Print the URL to the object.
            //echo $result['ObjectURL'] . "\n";
        } catch (S3Exception $e) {
            echo $e->getMessage() . "\n";
        }
    }
 }

Upvotes: 2

Views: 358

Answers (2)

noname
noname

Reputation: 28

You must create s3 instance in another way, like this:

$s3 = S3Client::factory([
    'region'      => '',
    'credentials' => ['key' => '***', 'secret' => '***'],
    'version' => 'latest',
]);

Upvotes: 1

cn0047
cn0047

Reputation: 17051

You must add $PrefixFolderPath not to 'Bucket' but to 'Key':

$result = $s3->putObject(array(
    'Bucket'       => $this->bucket,
    'Key'          => $PrefixFolderPath . "/" . $this->keyname,
    'SourceFile'   => $FullFilePath,
    'StorageClass' => 'REDUCED_REDUNDANCY'
));

Upvotes: 1

Related Questions