gpt4 book ai didi

python - 使用 python 的 boto 从 S3 检索数据时出错

转载 作者:行者123 更新时间:2023-11-30 22:53:06 26 4
gpt4 key购买 nike

我正在尝试使用 python 的 boto 从 Amazon S3 获取数据。

from boto.s3.connection import S3Connection

AWS_KEY = 'MY_KEY'
AWS_SECRET = 'MY_SECRET'
aws_connection = S3Connection(AWS_KEY, AWS_SECRET)
bucket = aws_connection.get_bucket('s3://mybucket.buckets.com/')
for file_key in bucket.list():
print file_key.name

我正在传递有效的 key 、secret_key 和存储桶名称。

当我尝试执行上述代码时,出现以下错误 -

Traceback (most recent call last):
File "MyPython_Script.py", line 7, in <module>
bucket = aws_connection.get_bucket('s3://mybucket.buckets.com/')
File "/usr/local/lib/python2.7/site-packages/boto/s3/connection.py", line 506, in get_bucket
return self.head_bucket(bucket_name, headers=headers)
File "/usr/local/lib/python2.7/site-packages/boto/s3/connection.py", line 525, in head_bucket
response = self.make_request('HEAD', bucket_name, headers=headers)
File "/usr/local/lib/python2.7/site-packages/boto/s3/connection.py", line 668, in make_request
retry_handler=retry_handler
File "/usr/local/lib/python2.7/site-packages/boto/connection.py", line 1071, in make_request
retry_handler=retry_handler)
File "/usr/local/lib/python2.7/site-packages/boto/connection.py", line 1030, in _mexe
raise ex
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

知道如何克服这个错误吗?我用于传递存储桶名称的格式正确吗?

最佳答案

您只需传递存储桶的名称,而不是无缝 URL(请注意,s3 端点将是 http://s3-aws-region.amazonaws.com/bucket )

如果您使用 boto2

from boto.s3.connection import S3Connection

AWS_KEY = 'MY_KEY'
AWS_SECRET = 'MY_SECRET'
aws_connection = S3Connection(AWS_KEY, AWS_SECRET)
bucket = aws_connection.get_bucket('bucket_name', validate=False)
for file_key in bucket.list():
print file_key.name

如果您使用 boto3

import boto3

s3 = boto3.resource('s3')
bucket = s3.Bucket('name')

关于python - 使用 python 的 boto 从 S3 检索数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38321360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com