AWS PHP SDK上传文件导致S3混乱(AWS PHP SDK upload file to S3 confusing errors)

好的,所以我尝试了几种获取文件上传到我的S3帐户的方法。 终于发现自己到了某个地方和BOOM - 令人困惑的文档和奇怪的错误信息似乎与自己相矛盾。

好吧,首先我不使用作曲家或类似的东西,我这样做是老式的方式:

require '/path/to/aws-autoload.php';

现在这个加载正确,我已经减少了自动加载只使用Common和S3类 - 不需要一切!

接下来我加载S3 Client和Credentials:

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

我似乎遇到的问题是“Bucket”本身。

当我将Bucket格式化为sub.domain.com时,我从AWS API返回以下消息:

AWS错误消息:您尝试访问的存储区必须使用指定的端点进行寻址。 请将以后的所有请求发送到此端点:“sub.domain.com.s3.amazonaws.com”。

现在,当我改变'Bucket'以匹配上面的内容时: sub.domain.com.s3.amazonaws.com

我收到以下错误消息:

AWS错误消息:指定的存储桶不存在

难道我做错了什么? 缺少什么? 遗憾的是,AWS文档并不是最新的。 API目前似乎自相矛盾。 我知道所有的密钥都是正确的,并且所有权限都是正确的。 它从301 - Redirect变为404 - Not Found from its advice。

任何帮助/建议将不胜感激。 我觉得我在这里走了一圈!

Ok, so I have tried several methods of getting a file to upload to my S3 account. Finally found myself getting somewhere and BOOM - confusing documentation and strange error messages which appear to contradict themselves.

Ok, to start with I am not using composer or anything like that, I am doing this the old fashioned way:

require '/path/to/aws-autoload.php';

Now this loads correctly, and I have trimmed down the autoload to only use Common and S3 classes - don't need everything!

Next up I load the S3 Client and Credentials:

use Aws\S3\S3Client; use Aws\Common\Credentials\Credentials;

Now the code to start the magic happening:

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

The issue I seem to be having is with the 'Bucket' itself.

When I format the Bucket as sub.domain.com I get the following message back from the 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".

Now when I change the 'Bucket' to match the above like so : sub.domain.com.s3.amazonaws.com

I get the following error message :

AWS Error Message: The specified bucket does not exist

Am I doing something wrong? Is something missing? Unfortunately the AWS Documentation is not quite up to scratch. The API seems to be contradicting itself at the moment. I know all the keys are correct, and all permissions are correct. It went from 301 - Redirect to 404 - Not Found from its own advice.

Any help/advise would be greatly appreciated. I feel like I am going in circles a little here!

最满意答案

这里有一些要仔细检查的事情。

如果桶是在某个区域(例如,us-west-2)中创建的, S3Client使用该区域[..., 'region' => 'us-west-2', ...]实例化S3Client 。 'Bucket'参数应设置为您的存储桶的名称(例如,如果您的存储桶名为“sub.domain.com”),则'Bucket'参数应设置为'sub.domain.com' 。 不要在'Bucket'参数中包含区域或“s3.amazonaws.com”(即bucket!= endpoint)。 SDK自动确定端点(基于客户端的区域和提供的存储桶名称),并根据需要调整路径样式的URL。 PutObject操作需要'Key'参数,该参数在上面的代码示例中不存在。

Here are some things to double check.

If the bucket was created in a certain region (e.g., us-west-2), instantiate the S3Client with that region [..., 'region' => 'us-west-2', ...]. The 'Bucket' parameter should be set to exactly what your bucket is named (e.g., if your bucket is named "sub.domain.com", then the 'Bucket' parameter should be set to 'sub.domain.com'). Do not include the region or "s3.amazonaws.com" in the 'Bucket' parameter (i.e., bucket != endpoint). The SDK automatically determines the endpoint (based on the client's region and provided bucket name) and adjusts the URL for path-style if needed. The PutObject operation requires the 'Key' parameter, which is not present in the code sample above.

更多推荐