2014-12-04 85 views
8

我只有S3訪問S3存儲桶中的特定目錄。Python boto,列出桶中特定目錄的內容

例如,與s3cmd命令,如果我嘗試列出全鬥:

$ s3cmd ls s3://my-bucket-url 

我得到一個錯誤:Access to bucket 'my-bucket-url' was denied

但是,如果我嘗試在鬥訪問特定的目錄,我可以看內容:

$ s3cmd ls s3://my-bucket-url/dir-in-bucket 

現在我想用python boto連接到S3存儲桶。與之相似有:

bucket = conn.get_bucket('my-bucket-url') 

我得到一個錯誤:boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden

但如果我嘗試:

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

腳本攤位約10秒鐘,之後打印出一個錯誤。波紋管是完整的痕跡。任何想法如何繼續這個?

Traceback (most recent call last): 
    File "test_s3.py", line 7, in <module> 
    bucket = conn.get_bucket('my-bucket-url/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 
+0

也許你應該在你的腳本中使用my-bucket-url/dir-in-bucket而不是'my-bucket-url/my-bucket-url'? – 2014-12-04 10:57:38

+0

抱歉,嘗試刪除實際的存儲分區名稱和目錄名稱時出錯。 – 2014-12-04 12:22:00

回答

16

默認情況下,當你在博託做一個get_bucket調用它試圖驗證你確實有訪問該桶通過對鬥網址HEAD請求。在這種情況下,您不希望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> 
+0

謝謝,這對我有用,我只需要在桶名稱末尾添加一個斜槓('/'),否則我仍然有403錯誤。 – 2014-12-04 13:04:45

+0

是的,這是有道理的。我批准你的編輯到我的例子。很高興爲你工作。 – garnaat 2014-12-04 13:18:59

+0

爲什麼需要尾隨「/」?我可以證實,在我的例子中它是必需的,但我找不到它的文檔。 – dbn 2016-12-13 00:34:53

0

如果要列出存儲桶中文件夾的所有對象,可以在列表中指定它。

import boto 
conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) 
bucket = conn.get_bucket(AWS_BUCKET_NAME) 
for file in bucket.list("FOLDER_NAME/", "/"): 
    <do something with required file> 
+0

OP提到'get_bucket'給他一個403 – ChrisWue 2017-03-28 01:06:13