Andrew Christensen
Andrew Christensen

Reputation: 864

Amazon S3 Upload error PermanentRedirectException

I've installed the Amazon SDK for PHP using composer. I copied the example code and implemented into my system, but when I attempt to upload a file, I'm getting the following (i've made the paths generic for security):

Fatal error: Uncaught exception 'Aws\S3\Exception\PermanentRedirectException' with message 'Encountered a permanent redirect while requesting https://s3.amazonaws.com/mybucket/path/to/image.jpg. Are you sure you are using the correct region for this bucket?' in /path/to/vendor/aws/aws-sdk-php/src/S3/PermanentRedirectMiddleware.php:49 Stack trace: #0 /path/to/vendor/guzzlehttp/promises/src/Promise.php(199): Aws\S3\PermanentRedirectMiddleware->Aws\S3{closure}(Object(Aws\Result)) #1 /path/to/vendor/guzzlehttp/promises/src/Promise.php(152): GuzzleHttp\Promise\Promise::callHandler(1, Object(Aws\Result), Array) #2 /path/to/vendor/guzzlehttp/promises/src/TaskQueue.php(60): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise{closure}() #3 /path/to/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(96): GuzzleHttp\Promise\TaskQueue->run() #4 /path/to/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(123): Gu in /path/to/vendor/aws/aws-sdk-php/src/S3/PermanentRedirectMiddleware.php on line 49

In my composer.json I'm specifying the 3.1 Amazon SDK.

In my PHP I'm doing the following (based on several image files posted to the script):

require $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';

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

$credentials = new Credentials('my-accessKey', 'accessSecret');

try {
    $s3Client = S3Client::factory(array(
        'credentials' => $credentials,
        'region' => 'us-east-1',
        'version' => 'latest'
    ));
} catch (S3Exception $e) {
    print_r($e->getMessage());
}

foreach($file['files']['tmp_name'] as $key => $tmp_name ){
    $file_name = $file['files']['name'][$key];
    $file_tmp = $file['files']['tmp_name'][$key];

    try { 
        $result = $s3Client->putObject(array(
            'Bucket'    => $awsCredentials['bucket'],
            'Key'       => "horsePhotos/".$horseId."/".$file_name,
            'SourceFile' => $file_tmp,
            'ACL'       => 'public-read'
        ));
    } catch(PDOException $e) {
        echo $e->getMessage();
    }
}

For the life of me I can't figure out what I'm doing wrong. I can't seem to find any reference to this error when Googling.

Any help would be great appreciated. Amazon's documentation is horrendously unhelpful.

Thanks in advance.

Upvotes: 4

Views: 7514

Answers (1)

Andrew Christensen
Andrew Christensen

Reputation: 864

Argh. I could have SWORN I was using the us-east-1 region. Apparently not. This error is indeed from having the wrong region.

Upvotes: 8

Related Questions