Python 无法使用 boto 连接 aws s3 存储桶

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22454559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:58:19  来源:igfitidea点击:

Unable to connect aws s3 bucket using boto

pythonamazon-web-servicesamazon-s3boto

提问by MONTYHS

AWS_ACCESS_KEY_ID = '<access key>'
AWS_SECRET_ACCESS_KEY = '<my secret key>'
Bucketname = 'Bucket-name' 
import boto
from boto.s3.key import Key
import boto.s3.connection
conn = boto.connect_s3(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,
        host ='s3.ap-southeast-1.amazonaws.com',
        is_secure=True,               # uncommmnt if you are not using ssl
        calling_format = boto.s3.connection.OrdinaryCallingFormat(),
        )
bucket = conn.get_bucket(Bucketname)

Error:

错误:

  Traceback (most recent call last):
   File "uploads3.py", line 69, in <module>
    upload_hello_file_s3()
  File "uploads3.py", line 25, in upload_hello_file_s3
    bucket = conn.get_bucket(Bucketname)
  File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 431, in get_bucket
    bucket.get_all_keys(headers, maxkeys=0)
  File "/usr/local/lib/python2.7/dist-packages/boto/s3/bucket.py", line 364, in get_all_keys
    '', headers, **params)
  File "/usr/local/lib/python2.7/dist-packages/boto/s3/bucket.py", line 321, in _get_all
    query_args=s)
  File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 543, in make_request
    override_num_retries=override_num_retries)
  File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 937, in make_request
    return self._mexe(http_request, sender, override_num_retries)
  File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 899, in _mexe
    raise e
socket.gaierror: [Errno -2] Name or service not known

please help me to solve this problem there is no problem in bucket name and access key and secret key.

请帮我解决这个问题 存储桶名称和访问密钥和秘密密钥没有问题。

采纳答案by Rico

You can also use the following (boto.s3.connect_to_region):

您还可以使用以下内容 (boto.s3.connect_to_region):

import boto
from boto.s3.key import Key
import boto.s3.connection

AWS_ACCESS_KEY_ID = '<access key>'
AWS_SECRET_ACCESS_KEY = '<my secret key>'
Bucketname = 'Bucket-name' 


conn = boto.s3.connect_to_region('ap-southeast-1',
       aws_access_key_id=AWS_ACCESS_KEY_ID,
       aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
       is_secure=True,               # uncomment if you are not using ssl
       calling_format = boto.s3.connection.OrdinaryCallingFormat(),
       )
bucket = conn.get_bucket(Bucketname)

This way you don't have to care about the 'exact' endpoint with the full hostname. And yes like @garnaat mentioned, use the latest boto API.

这样您就不必关心具有完整主机名的“确切”端点。是的,就像@garnaat 提到的那样,使用最新的 boto API。

回答by vivekagr

The request to the host s3.ap-southeast-1.amazonaws.comis failing. I also cannot resolve it from my end. Check your bucket settings for the correct host.

对主机的请求s3.ap-southeast-1.amazonaws.com失败。我也无法从我的角度解决它。检查正确主机的存储桶设置。

There might also be a problem with your internet connection or the DNS server. Try pinging the host manually from command line and see if it resolves. Alternatively, try using a different DNS.

您的互联网连接或 DNS 服务器也可能有问题。尝试从命令行手动 ping 主机,看看它是否解决。或者,尝试使用不同的 DNS。

Edit: Quick googling suggests that the host might be s3-ap-southeast-1.amazonaws.com.

编辑:快速谷歌搜索表明主机可能是s3-ap-southeast-1.amazonaws.com.

回答by olivecoder

There is a typo in the host parameter. The right one is: s3-ap-southeast-1.amazonaws.com

主机参数中有一个错字。正确的是:s3-ap-southeast-1.amazonaws.com

REFERENCES Amazon Regions and Endpoints

参考 Amazon 区域和终端节点

回答by legers

Gotcha: capture traffic on your Ethernet link and ensure CNAME in DNS queries do NOTcontain '\r' character e.g. in the bucket name.

陷阱:捕获以太网链路上的流量并确保 DNS 查询中的 CNAME包含 '\r' 字符,例如在存储桶名称中。

回答by Abin Thomas Philip

from boto3.session import Session

ACCESS_KEY='your_access_key'

SECRET_KEY='your_secret_key'

session = Session(aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY)

s3 = session.resource('s3')

my_bucket = s3.Bucket('bucket_name')

for s3_file in my_bucket.objects.all():

           print(s3_file.key)