3

好的,所以我尝试了几种方法将文件上传到我的 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
     );
}

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

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

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

现在,当我更改“桶”以匹配上述内容时,如下所示:sub.domain.com.s3.amazonaws.com

我收到以下错误消息:

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

难道我做错了什么?有什么遗漏吗?不幸的是,AWS 文档还没有达到标准。API 目前似乎自相矛盾。我知道所有的密钥都是正确的,所有的权限都是正确的。根据自己的建议,它从 301 - 重定向到 404 - Not Found。

任何帮助/建议将不胜感激。我觉得我在这里有点绕圈子!

4

1 回答 1

4

Here are some things to double check.

  1. If the bucket was created in a certain region (e.g., us-west-2), instantiate the S3Client with that region [..., 'region' => 'us-west-2', ...].
  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.
  3. The PutObject operation requires the 'Key' parameter, which is not present in the code sample above.
于 2014-10-07T18:34:09.663 回答