首页 > 解决方案 > 为什么将错误的区域发送到 Amazon S3 存储桶查询?

问题描述

我正在尝试流式传输存储在 Amazon S3 存储桶中的音乐。

import boto3
SERVICE_NAME = 's3'
REGION_NAME = 'eu-west-2'

def generate_presigned_url(bucket_name, object_key, expiry=3600):
    client = boto3.client("s3",region_name=REGION_NAME,
               aws_access_key_id=ACCESS_KEY,
               aws_secret_access_key=SECRET_KEY)
     response =  client.generate_presigned_url(
             'get_object', 
              Params={'Bucket': bucket_name,
             'Key': object_key},
              ExpiresIn=expiry)
     return response

url = generate_presigned_url('bucket','music.mp3',3600)
print(url)

输出(重新格式化,以便您可以看到...):

https://bucket.s3.amazonaws.com/music.mp3?
X-Amz-Algorithm=AWS4-HMAC-SHA256&
X-Amz-Credential=[redacted]%2Feu-west-[etc]

这失败并出现以下错误:

Error parsing the X-Amz-Credential parameter; 
the region 'eu-west-2' is wrong; expecting 'us-east-1'

任何想法出了什么问题?正确的区域位于生成的 URL 中。

我已经阅读了有关此错误的所有内容,但到目前为止还没有运气。

标签: pythonamazon-web-servicesamazon-s3boto

解决方案


也许您在 ~/.aws/credentials 或 ~/.aws/config 中的 [default] 配置文件中的区域设置为另一个并且它正在创建冲突?

你也可以尝试设置一个配置:

my_config = Config(
    region_name = 'us-west-2',
    signature_version = 'v4',
    retries = {
        'max_attempts': 10,
        'mode': 'standard'
    })
client = boto3.client('s3', config=my_config)

推荐阅读