亚马逊S3上传错误 PermanentRedirectException

4

我使用composer安装了Amazon SDK for PHP。我复制了示例代码并将其实现到我的系统中,但是当我尝试上传文件时,我收到以下错误(我已经将路径设置为通用以确保安全):

致命错误:遇到永久重定向,请求https://s3.amazonaws.com/mybucket/path/to/image.jpg时发生。您确定使用了正确的区域吗?位于 /path/to/vendor/aws/aws-sdk-php/src/S3/PermanentRedirectMiddleware.php 的第49行。堆栈跟踪:#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在 /path/to/vendor/aws/aws-sdk-php/src/S3/PermanentRedirectMiddleware.php 的第49行。

在我的composer.json中,我正在指定3.1版本的Amazon SDK。

在我的PHP代码中,我根据发布到脚本的几个图像文件执行以下操作:

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

我真的无法弄清楚我做错了什么。当我在谷歌上搜索时,似乎找不到任何与此错误相关的参考资料。

任何帮助都将不胜感激。亚马逊的文档实在是没有什么用。

提前致谢。


2
你确定存储桶在美国东部(弗吉尼亚)吗? - David Maust
这个 Bucket 值是空的吗?不是这个位置或方式吗? - Master James
1个回答

7

天啊,我本以为我正在使用us-east-1地区。显然不是这样。这个错误确实是由于错误的地区引起的。


你是如何确定你正在使用哪个区域的?当登录时,URL显示s3/home?region=us-west-2,但我的存储桶设置为爱尔兰。当我在代码中将区域切换到爱尔兰时,我的页面将无法加载。但是,如果使用us-west-2,则会出现错误的区域。 - alsobubbly
没问题,找到答案了。我需要指定的是爱尔兰地区,但它被写成了eu-west-1(非常令人困惑)! - alsobubbly

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接