2015-05-18 22 views
0
import boto 
from boto.s3.connection import S3Connection 
from boto.s3.connection import OrdinaryCallingFormat 

conn = S3Connection(access_key, secret_key, calling_format=OrdinaryCallingFormat()) 
bucket = conn.get_bucket(file_name) 
print(bucket.name) 

和控制檯顯示:Python和博託:403存取遭拒

raise err 
boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden 

我看過很多帖子差不多的問題,但我無法弄清楚如何解決它... 注意我不是存儲桶的所有者,但我成功地使用gui工具連接和下載文件。我需要通過腳本處理它以實現自動化。

編輯: 成功連接,但仍掙扎...... 我開始毫不猶豫地處理它自動而不是手動...

conn = S3Connection(access_key, secret_key, calling_format=OrdinaryCallingFormat()) 

bucket = conn.get_bucket(bucket_name, validate=False) 
print('bucket:', bucket) 
print('bucket.name:', bucket.name) 

key = bucket.get_key(file_name) 
print("key: {name}\t{size}\t{modified}".format(name = key.name,size = key.size,modified = key.last_modified)) 

print('bucket.list():',bucket.list(prefix='GA-Exports/Events_3112/DEV')) 

for key in bucket.list(prefix='DEV/',delimiter='/'): 
    print('bucket list -> key:',key) 

控制檯:

bucket: <Bucket: GA-Exports/Events_3112/> 
bucket.name: GA-Exports/Events_3112/ 
key: DEV/EVENTS_3113_120002892.csv.gz 3826 Sat, 16 May 2015 10:05:44 GMT 
bucket.list(): <boto.s3.bucketlistresultset.BucketListResultSet object at 0x0000000004E9F7F0> 
Traceback (most recent call last): 
    File "D:\Python\lib\xml\sax\expatreader.py", line 207, in feed 
    self._parser.Parse(data, isFinal) 
xml.parsers.expat.ExpatError: no element found: line 1, column 0 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:\Users\Francois\OneDrive\IDE\Workspace\eclipse\Python_test\etltest.py", line 31, in <module> 
    for key in bucket.list(prefix='DEV/',delimiter='/'): 
    File "D:\Python\lib\site-packages\boto\s3\bucketlistresultset.py", line 34, in bucket_lister 
    encoding_type=encoding_type) 
    File "D:\Python\lib\site-packages\boto\s3\bucket.py", line 472, in get_all_keys 
    '', headers, **params) 
    File "D:\Python\lib\site-packages\boto\s3\bucket.py", line 406, in _get_all 
    xml.sax.parseString(body, h) 
    File "D:\Python\lib\xml\sax\__init__.py", line 46, in parseString 
    parser.parse(inpsrc) 
    File "D:\Python\lib\xml\sax\expatreader.py", line 107, in parse 
    xmlreader.IncrementalParser.parse(self, source) 
    File "D:\Python\lib\xml\sax\xmlreader.py", line 125, in parse 
    self.close() 
    File "D:\Python\lib\xml\sax\expatreader.py", line 217, in close 
    self.feed("", isFinal = 1) 
    File "D:\Python\lib\xml\sax\expatreader.py", line 211, in feed 
    self._err_handler.fatalError(exc) 
    File "D:\Python\lib\xml\sax\handler.py", line 38, in fatalError 
    raise exception 
xml.sax._exceptions.SAXParseException: <unknown>:1:0: no element found 
+0

美國西部1區的水桶?我可以看到你沒有設置區域任何地方 – firelynx

+0

其實我不知道這個桶在哪裏。 – exentro

回答

1

默認,當您通過在存儲桶上執行HEAD請求調用get_bucket時,boto將嘗試驗證存儲桶。即使您可能有權從存儲桶中檢索對象,您也可能沒有權限執行此操作。試試這個禁用驗證步驟:

bucket = conn.get_bucket(bucket_name, validate=False) 

此外,請確保您傳遞的是存儲桶的名稱。你的示例代碼傳入file_name這聽起來不對。

+0

確實是bucket_name而不是file_name。 對於validate = False,我嘗試過,但由於我懷疑存儲桶真的被加載了,所以我把它放在一邊,我將它重置爲false。亞馬遜的訪問權限和boto API文檔仍然顯得很模糊。 – exentro

+0

從文檔:「要使用HEAD,您必須具有對該對象的讀取權限。」不可能沒有權限使用HEAD並有權讀取對象。雖然你可能會得到403錯誤,因爲你可能沒有's3:ListBucket'權限。 http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectHEAD.html – user2343996