如何使用Boto向端点发送请求

时间:2014-03-07 19:13:04

标签: python amazon-s3 boto

我正在尝试使用以下代码列出S3容器中的项目。

import boto.s3
from boto.s3.connection import OrdinaryCallingFormat

conn = boto.connect_s3(calling_format=OrdinaryCallingFormat())
mybucket = conn.get_bucket('Container001')

for key in mybucket.list():
    print key.name.encode('utf-8')

然后我收到以下错误。

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    mybucket = conn.get_bucket('Container001')
  File "/usr/lib/python2.7/dist-packages/boto/s3/connection.py", line 370, in get_bucket
bucket.get_all_keys(headers, maxkeys=0)
File "/usr/lib/python2.7/dist-packages/boto/s3/bucket.py", line 358, in get_all_keys
'', headers, **params)
File "/usr/lib/python2.7/dist-packages/boto/s3/bucket.py", line 325, in _get_all
response.status, response.reason, body)
boto.exception.S3ResponseError: S3ResponseError: 301 Moved Permanently
<?xml version="1.0" encoding="UTF-8"?>
PermanentRedirectThe bucket you are attempting to access  must be addressed using the  specified endpoint. Please send all future requests to this endpoint.99EBDB9DE3B6E3AF
Container001
<HostId>5El9MLfgHZmZ1UNw8tjUDAl+XltYelHu6d/JUNQsG3OaM70LFlpRchEJi9oepeMy</HostId><Endpoint>Container001.s3.amazonaws.com</Endpoint></Error>

我尝试搜索如何将请求发送到指定的终点,但找不到有用的信息。

如何避免此错误?

2 个答案:

答案 0 :(得分:8)

正如@garnaat所提及的那样,@ Ric answered in another question connect_to_regionOrdinaryCallingFormat合作:

conn = boto.s3.connect_to_region(
   region_name = '<your region>',
   aws_access_key_id = '<access key>',
   aws_secret_access_key = '<secret key>',
   calling_format = boto.s3.connection.OrdinaryCallingFormat()
   )
bucket = conn.get_bucket('<bucket name>')

答案 1 :(得分:0)

在终端运行中

  

纳米〜/ .boto

如果有一些配置尝试注释或重命名文件并再次连接。 (它对我有帮助)

http://boto.cloudhackers.com/en/latest/boto_config_tut.html

有boto配置文件目录。看一个一个地清理它们,它将默认配置。也可以在.bash_profile,.bash_source中配置... 我想你必须只允许KEY-SECRET

也尝试使用

  

calling_format = boto.s3.connection.OrdinaryCallingFormat()