AWS CLI:aws s3前缀不适用于ap-east-1(香港)区域(“位置限制对于特定于区域的终端节点不兼容”)

时间:2019-09-28 04:08:58

标签: amazon-web-services amazon-s3 aws-sdk

我一直无法对位于ap-east-1(香港)AWS地区的存储桶中的S3对象使用签名的URL。

具体来说,我首先将签名版本设置为V4(请参见https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#specify-signature-version),如下所示:

aws configure set default.s3.signature_version s3v4

然后使用以下命令创建一个签名的URL:

aws s3 presign --region=ap-east-1 s3://<name of bucket in ap-east-1 region>/<object name>

当我测试结果URL时:

curl -i "https://<bucket name>.s3.amazonaws.com/<object name>?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=...%2Fap-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190928T034534Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=..."

...我收到一个HTTP / 400响应,其主体设置为:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
   <Code>IllegalLocationConstraintException</Code>
   <Message>The ap-east-1 location constraint is incompatible for the region specific endpoint this request was sent to. 
   </Message>
   <RequestId>...</RequestId>
<HostId>

有趣的是,当对位于us-west-2(俄勒冈)AWS区域中的另一个存储桶中的对象执行相同操作时,生成的签名URL可以正常工作。

有人遇到这个问题吗?我不太了解如何解析错误消息,但是我确实想知道是否需要设置--endpoint-url配置设置,如果需要,则设置为什么?

有想法吗?

谢谢, 索伦

1 个答案:

答案 0 :(得分:0)

您需要在CLI配置中设置寻址方式,此功能才能起作用。有关详细信息,请参见AWS CLI S3 Configuration文档。

步骤集:

aws configure set default.s3.addressing_style virtual
aws s3 presign s3://<bucket-in-ap-east-1>/<object_key> --region ap-east-1

Python中的等效代码

import boto3
def generate_presigned_url(bucket_region, bucket_name, object_key, expiration):
   s3_client = boto3.client('s3', region_name=bucket_region,  config=boto3.session.Config(s3={'addressing_style': 'virtual'}, signature_version='s3v4'))
   response = s3_client.generate_presigned_url('get_object', Params={'Bucket': bucket_name, 'Key': object_key}, ExpiresIn=expiration)
   return response

用法

generate_presigned_url('ap-east-1', bucket_name, object_name, expiration)

样本输出

https://your-bucket.s3.ap-east-1.amazonaws.com/object-key?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=XXXX%2Fap-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200714T050137Z&X-Amz-Expires=7200&X-Amz-SignedHeaders=host&X-Amz-Security-Token=TTTT&X-Amz-Signature=cde17576df4502854639bdd52fdd493e3b4b946fd668b8478c70be0d294df78d'