gpt4 book ai didi

Python boto,列出桶中特定目录的内容

转载 作者:IT老高 更新时间:2023-10-28 20:29:47 24 4
gpt4 key购买 nike

我只能通过 S3 访问 S3 存储桶中的特定目录。

例如,如果我尝试列出整个存储桶,则使用 s3cmd 命令:

    $ s3cmd ls s3://bucket-name

我收到一个错误:对存储桶“my-bucket-url”的访问被拒绝

但是如果我尝试访问存储桶中的特定目录,我可以看到内容:

    $ s3cmd ls s3://bucket-name/dir-in-bucket

现在我想用 python boto 连接到 S3 存储桶。类似:

    bucket = conn.get_bucket('bucket-name')

我得到一个错误:boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden

但如果我尝试:

    bucket = conn.get_bucket('bucket-name/dir-in-bucket')

脚本暂停大约 10 秒,然后打印出错误。波纹管是完整的跟踪。知道如何进行吗?

注意问题是关于 boto 版本 2 模块,而不是 boto3。

Traceback (most recent call last):
File "test_s3.py", line 7, in <module>
bucket = conn.get_bucket('bucket-name/dir-name')
File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 471, in get_bucket
return self.head_bucket(bucket_name, headers=headers)
File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 490, in head_bucket
response = self.make_request('HEAD', bucket_name, headers=headers)
File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 633, in make_request
retry_handler=retry_handler
File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 1046, in make_request
retry_handler=retry_handler)
File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 922, in _mexe
request.body, request.headers)
File "/usr/lib/python2.7/httplib.py", line 958, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py", line 992, in _send_request
self.endheaders(body)
File "/usr/lib/python2.7/httplib.py", line 954, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 814, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 776, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 1157, in connect
self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

最佳答案

对于boto3

import boto3

s3 = boto3.resource('s3')
my_bucket = s3.Bucket('my_bucket_name')

for object_summary in my_bucket.objects.filter(Prefix="dir_name/"):
print(object_summary.key)

关于Python boto,列出桶中特定目录的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27292145/

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