好的,是以我嘗試了幾種方法來使檔案上傳到我的S3帳戶.終于發現自己到了BOOM——混亂的文檔和奇怪的錯誤消息似乎與自己沖突.
好的,首先,我不使用作曲家或類似的工具,我以老式的方式進行操作:
require '/path/to/aws-autoload.php';
現在,這可以正确加載,并且我已将自動加載調整為僅使用Common和S3類-不需要所有内容!
接下來,我加載S3用戶端和憑據:
use Aws\S3\S3Client;
use Aws\Common\Credentials\Credentials;
現在,開始魔術發生的代碼:
$file = '/path/to/' . $filename;
$credentials = new Credentials('KEY', 'SECRET KEY');
$s3 = S3Client::factory(array('credentials' => $credentials));
try{
$s3->putObject(array(
'Bucket' => 'sub.domain.com.s3.amazonaws.com',
'Body' => fopen($file, 'r'),
'ACL' => 'public-read',
));
} catch (S3Exception $e) {
$result = array(
'ok' => false,
'descr' => 'There was an error uploading the file to S3 : ' . $filename
);
}
我似乎遇到的問題是“存儲桶”本身.
當我将存儲桶格式化為sub.domain.com時,我從AWS API收到以下消息:
AWS Error Message: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint: “sub.domain.com.s3.amazonaws.com”.
現在,當我更改“ Bucket”以比對上面的内容時,如下所示:sub.domain.com.s3.amazonaws.com
我收到以下錯誤消息:
AWS Error Message: The specified bucket does not exist
難道我做錯了什麼?缺少什麼嗎?不幸的是,AWS文檔還不夠完善.該API目前似乎自相沖突.我知道所有鍵都是正确的,并且所有權限都是正确的.根據自己的建議,它從301-重定向到404-未找到.
任何幫助/建議将不勝感激.我覺得我在這裡轉了圈!
解決方法:
這裡有一些事情要仔細檢查.
>如果存儲桶是在某個區域(例如us-west-2)中建立的,請使用該區域[…,’region’=> ‘us-west-2’,…].
>“存儲桶”參數應準确設定為存儲桶的名稱(例如,如果存儲桶名為“ sub.domain.com”,則“存儲桶”參數應設定為“ sub.domain.com”) .不要在“桶”參數中包含區域或“ s3.amazonaws.com”(即,桶!=端點). SDK會自動确定端點(基于用戶端的區域和提供的存儲桶名稱),并根據需要調整URL的路徑樣式.
> PutObject操作需要’Key’參數,該參數在上面的代碼示例中不存在.
标簽:amazon-web-services,amazon-s3,aws-php-sdk,php
來源: https://codeday.me/bug/20191029/1957232.html