简体   繁体   English

在 Amazon AWS PHP SDK 中为 S3 客户端设置适当的端点

[英]Set proper endpoint in for S3 Client in Amazon AWS PHP SDK

I'm trying to use the AWS SDK for PHP to programatically upload a file to a bucket that's set to be a static website in the S3 Console.我正在尝试使用适用于 PHP 的 AWS 开发工具包以编程方式将文件上传到在 S3 控制台中设置为静态网站的存储桶。

The bucket is named foo.ourdomain.com and is hosted in eu-west.该存储桶名为 foo.ourdomain.com,托管在 eu-west。 I'm using the following code to try and test if I can upload a file:我正在使用以下代码来尝试测试我是否可以上传文件:

  $client = \Aws\S3\S3Client::factory(array('key' => bla, 'secret' => bla));
  $client->upload('foo.ourdomain.com', 'test.txt', 'hello world', 'public-read');

This is pretty much like it is in the examples, however, I received the following exception:这与示例中的情况非常相似,但是,我收到了以下异常:

PHP Fatal error: Uncaught Aws\\S3\\Exception\\PermanentRedirectException: AWS Error Code: PermanentRedirect, Status Code: 301, AWS Request ID: -, AWS Error Type: client, AWS Error Message: The bucket you are attempting to access must be addressed using the specified endpoint. PHP 致命错误:未捕获的 Aws\\S3\\Exception\\PermanentRedirectException:AWS 错误代码:PermanentRedirect,状态代码:301,AWS 请求 ID:-,AWS 错误类型:客户端,AWS 错误消息:必须解决您尝试访问的存储桶使用指定的端点。 Please send all future requests to this endpoint: "foo.ourdomain.com.s3.amazonaws.com"., User-Agent: aws-sdk-php2/2.4.8 Guzzle/3.7.4 curl/7.22.0 PHP/5.3.10-1ubuntu3.8请将所有未来的请求发送到此端点:“foo.ourdomain.com.s3.amazonaws.com”。,用户代理:aws-sdk-php2/2.4.8 Guzzle/3.7.4 curl/7.22.0 PHP/5.3 .10-1ubuntu3.8

At this point I was surprised as there's no mention of this in the manual for the S3 SDK.在这一点上我很惊讶,因为 S3 SDK 的手册中没有提到这一点。 But okay, I found a method setEndpoint and adjusted the code to:不过还好,我找到了一个方法 setEndpoint 并将代码调整为:

   $client = \Aws\S3\S3Client::factory(array('key' => bla, 'secret' => bla));
   $client->setEndpoint('foo.ourdomain.com.s3.amazonaws.com');
   $client->upload('foo.ourdomain.com', 'test.txt', 'hello world', 'public-read');

I assumed that'd work, but I'm getting the exact same error.我认为那行得通,但我得到了完全相同的错误。 I've doublechecked and the endpoint mentioned in the exception byte-for-byte matches the one I'm setting in the second line.我已经仔细检查过,异常字节对字节中提到的端点与我在第二行中设置的端点相匹配。

I've also tried using foo.ourdomain.com.s3-website-eu-west-1.amazonaws.com as the endpoint (this is the host our CNAME points to as per the S3 consoles instructions).我还尝试使用 foo.ourdomain.com.s3-website-eu-west-1.amazonaws.com 作为端点(这是我们的 CNAME 根据 S3 控制台说明指向的主机)。 Didn't work either.也没有用。

I must be missing something, but I can't find it anywhere.我一定是遗漏了什么,但我在任何地方都找不到。 Perhaps buckets set to 'static website' behave differently in a way which is not currently supported by the SDK?也许设置为“静态网站”的存储桶以 SDK 当前不支持的方式表现不同? If so, I can't find mention of it in the docs nor in the management console.如果是这样,我在文档和管理控制台中都找不到提及它。

Got it.知道了。 The solution was to change the initialisation of the client to:解决方案是将客户端的初始化更改为:

$client = \Aws\S3\S3Client::factory(array(
  'key' => bla, 
  'secret' => bla, 
  'region' => 'eu-west-1'
));

Ie rather than specify an endpoint I needed to explicitly set the region in the options array.即,我不需要指定一个端点,而是在选项数组中显式设置区域。 I guess the example code happens to use whatever the default region is.我猜示例代码碰巧使用了默认区域。

If you don't want to initialize the client to a specific region and/or you'll need to work with different regions, I have been successful in using the getBucketLocation/setRegion set of calls as follows:如果您不想将客户端初始化到特定区域和/或您需要使用不同的区域,我已经成功地使用了 getBucketLocation/setRegion 调用集,如下所示:

// Bucket location is fetched
$m_bucketLocation = $I_s3->getBucketLocation(array(
        'Bucket' => $s_backupBucket,
));
// Bucket location is specified before operation is made
$I_s3->setRegion($m_bucketLocation['Location']);

I have one extra call, but solved my issue without the need to intervene on the factory.我有一个额外的电话,但无需干预工厂即可解决我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM