我只能通过S3访问S3存储桶中的特定目录。
例如,s3cmd
如果我尝试列出整个存储桶,请使用命令:
$ s3cmd ls s3://bucket-name
我收到一个错误: Access to bucket 'my-bucket-url' was denied
但是,如果我尝试访问存储桶中的特定目录,则可以看到以下内容:
$ 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
默认情况下,当您get_bucket
在Boto中进行调用时,它会通过HEAD
对存储桶URL 执行请求来尝试验证您是否实际上有权访问该存储桶。在这种情况下,您不希望boto那样做,因为您无权访问存储桶本身。因此,请执行以下操作:
bucket = conn.get_bucket('my-bucket-url', validate=False)
然后您应该能够执行以下操作以列出对象:
for key in bucket.list(prefix='dir-in-bucket'):
<do something>
如果仍然收到403 Errror,请尝试在前缀末尾添加斜杠。
for key in bucket.list(prefix='dir-in-bucket/'):
<do something>
注意:此答案是关于boto版本2模块的,该模块现在已过时。目前(2020年),boto3是用于AWS的标准模块。有关更多信息,请参阅此问题:AWS boto和boto3有什么区别
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句