我使用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();
}
}
我真的无法弄清楚我做错了什么。当我在谷歌上搜索时,似乎找不到任何与此错误相关的参考资料。
任何帮助都将不胜感激。亚马逊的文档实在是没有什么用。
提前致谢。