简体   繁体   English

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

[英]Unable to connect aws s3 bucket using boto

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.请帮我解决这个问题 存储桶名称和访问密钥和秘密密钥没有问题。

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。

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)

The request to the host s3.ap-southeast-1.amazonaws.com is failing.对主机s3.ap-southeast-1.amazonaws.com的请求失败。 I also cannot resolve it from my end.我也无法从我的角度解决它。 Check your bucket settings for the correct host.检查正确主机的存储桶设置。

There might also be a problem with your internet connection or the DNS server.您的互联网连接或 DNS 服务器也可能有问题。 Try pinging the host manually from command line and see if it resolves.尝试从命令行手动 ping 主机,看看它是否解决。 Alternatively, try using a different DNS.或者,尝试使用不同的 DNS。

Edit: Quick googling suggests that the host might be s3-ap-southeast-1.amazonaws.com .编辑:快速谷歌搜索表明主机可能是s3-ap-southeast-1.amazonaws.com

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 区域和终端节点

The question is answered, but I wanted to include some additional info that helped me.问题已得到解答,但我想包含一些对我有帮助的其他信息。 Keep in mind latest boto is boto3, but I was stuck using Python 2.7 in a legacy environment.请记住,最新的 boto 是 boto3,但我一直在旧环境中使用 Python 2.7。

Authentication验证

There are at least 3 ways to authenticate with boto: First, you can include credentials (access key, secret key) in the connect_to_region() call.至少有 3 种使用 boto 进行身份验证的方法:首先,您可以在 connect_to_region() 调用中包含凭据(访问密钥、秘密密钥)。 A second way is to define the environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY and then don't supply credentials in the connect_to_region() call.第二种方法是定义环境变量 AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY,然后不在 connect_to_region() 调用中提供凭证。 Finally, if using boto 2.5.1 or later, boto can use the IAM role for an instance to create temporary credentials.最后,如果使用 boto 2.5.1 或更高版本,boto 可以使用实例的 IAM 角色来创建临时凭证。

For the first two, you need to use AWS console to create a user with access to a bucket.对于前两个,您需要使用 AWS 控制台创建可以访问存储桶的用户。 In the third method, create an IAM role with access to the bucket and assign it to the instance.在第三种方法中,创建一个可以访问存储桶的 IAM 角色并将其分配给实例。 The 3rd way is often the best because then you don't have to store credentials in source control, or manage credentials in the environment.第 3 种方式通常是最好的,因为这样您就不必在源代码控制中存储凭据,或在环境中管理凭据。

Accessing the Bucket访问存储桶

Now on to the mistake I made that caused the same message as the OP.现在,我犯的错误导致与 OP 相同的消息。 The top level objects in S3 are buckets and everything below are keys. S3 中的顶级对象是桶,下面的所有对象都是键。 In my case the object I wanted to access was at s3:top-level/next-level/object.就我而言,我想要访问的对象位于 s3:top-level/next-level/object。 I tried to access it like this:我试图像这样访问它:

bucket = conn.get_bucket('top-level/next-level')

The point is that next-level is not a bucket but a key, and you'll get the "Name or service not known" message if the bucket doesn't exist.关键是下一级不是存储桶而是键,如果存储桶不存在,您将收到“名称或服务未知”消息。

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

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

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