简体   繁体   English

使用boto for AWS S3 Buckets for Signature V4

[英]Using boto for AWS S3 Buckets for Signature V4

I have a problem with using Python-Boto SDK for S3 Buckets for region Frankfurt. 我在区域法兰克福使用Python-Boto SDK for S3 Buckets时遇到问题。 According to Amazon link this region will only support V4. 亚马逊链接,该地区仅支持V4。 This document explains how to add V4 support for Boto SDK. 文档介绍了如何为Boto SDK添加V4支持。 I have added a new section: 我添加了一个新的部分:

if not boto.config.get('s3', 'use-sigv4'):
    boto.config.add_section('s3')
    boto.config.set('s3', 'use-sigv4', 'True')

and then I have created new connection and got all buckets: 然后我创建了新连接并获得了所有桶:

connection = S3Connection(accesskey, secretkey, host=S3Connection.DefaultHost)
buckets = connection.get_all_buckets()

it works fine, but then I tried to get all keys for my bucket: 它工作正常,但后来我试图获取我的桶的所有键:

for bucket in buckets:
    bucket.get_all_keys()

and I got the following: 我得到以下内容:

S3ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AuthorizationHeaderMalformed</Code><Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'eu-central-1'</Message><Region>eu-central-1</Region>

Why did it occur? 它为什么会发生? After that I connected to the region and got all needed data: 之后我连接到该地区并获得所有需要的数据:

region_con = boto.s3.connect_to_region('eu-central-1', aws_access_key_id=accesskey, aws_secret_access_key=secretkey)
bucket = region_con.get_bucket(bucket.name)
bucket.get_all_keys()

How can I fix it properly? 我该如何正确修理?

I had the same issue using Boto. 我使用Boto时遇到了同样的问题。 The region was Frankfurt and got errors about wrong regions. 该地区是法兰克福,错误地区出错。 The solution for me was just to point a host (an URI got from this page http://docs.aws.amazon.com/general/latest/gr/rande.html ) to 's3.eu-central-1.amazonaws.com' instead of default 's3.amazonaws.com' 我的解决方案只是将主机(从此页面http://docs.aws.amazon.com/general/latest/gr/rande.html获取的URI)指向's3.eu-central-1.amazonaws .com'而不是默认's3.amazonaws.com'

s3 = boto.s3.connect_to_region('eu-central-1',
                               aws_access_key_id=accesskey,
                               aws_secret_access_key=secretkey,
                               host='s3.eu-central-1.amazonaws.com')

尝试从boto配置中删除s3,以下代码适用于我

if 's3' in boto.config.sections(): boto.config.remove_section('s3')

hsrv's answer above works for boto 2. For boto3 , the following is broadly equivalent: hsrv的答案适用于boto 2.对于boto3 ,以下内容大致相同:

s3 = boto3.client('s3', region_name='eu-central-1')

Alternatively, you can set the region field in your .aws/config : 或者,您可以在.aws/config设置region字段:

[default]
output = json
region = eu-central-1

This sets the default region; 这设置了默认区域; you can still pick a specific region in Python as above. 你仍然可以在Python中选择一个特定的区域。

The significance of the region varies from service to service (for example, assuming you're not sat in a VPC, you can access an S3 bucket from anywhere). 该区域的重要性因服务而异(例如,假设您没有坐在VPC中,您可以从任何地方访问S3存储桶)。 In this case, however, the important thing is that newer regions (such as Frankfurt) only support the newer authentication scheme (AWS4-HMAC-SHA256). 然而,在这种情况下,重要的是较新的区域(例如法兰克福)仅支持较新的认证方案(AWS4-HMAC-SHA256)。 Boto runs into problems if you try to connect to anything in such a region from a region that still uses the old scheme (such as Dublin). 如果您尝试从仍使用旧方案的区域(例如Dublin)连接到此类区域中的任何内容,Boto会遇到问题。

for boto2 -- adding this to the .boto config worked 对于boto2 - 将此添加到.boto配置工作

[s3]
use-sigv4 = True
host=s3.eu-central-1.amazonaws.com

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

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