2017-08-17 102 views
1
>>> import boto 
>>> s3 = boto.connect_s3('<access_key>', '<secret_key>') 
>>> bucket = s3.lookup('donebox-static') 
>>> key = bucket.new_key('testkey') 
>>> key.set_contents_from_string('This is a test') 
>>> key.exists() 
>>> key.delete() 

雖然刪除,我得到以下錯誤。我正在使用Linux機器,但我可以從Windows機器上刪除文件。 錯誤是:我想從S3存儲桶中使用python刪除文件,但我無法刪除文件

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/python2.7/site-packages/boto-2.48.0-py2.7.egg/boto/s3/key.py", line 558, in delete 
    headers=headers) 
    File "/usr/lib/python2.7/site-packages/boto-2.48.0-py2.7.egg/boto/s3/bucket.py", line 762, in delete_key 
    query_args_l=None) 
    File "/usr/lib/python2.7/site-packages/boto-2.48.0-py2.7.egg/boto/s3/bucket.py", line 781, in _delete_key_internal 
    response.reason, body) 
boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden 
<?xml version="1.0" encoding="UTF-8"?> 
<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>6CF28CE4F8227FAE</RequestId><HostId>mrS5DIDHvXgweWSlwmQYVE0H05jsnepXd+3PiMqHcjXhWPkfo8ibeWA9rBcm7fKkdAO2f/fUTjo=</HostId></Error 

>

回答

0

你的代碼是完全正常的。 (我在我自己的桶上測試過,它運行良好。)

錯誤消息是Access Denied。這意味着與您的AWS憑證相關聯的實體(很可能是您的IAM用戶)不允許刪除該對象。或者,最有可能的是,該桶中的任何對象。

檢查事項:

  • 只看你的IAM用戶的政策(或任何實體與您正在使用該憑證相關聯)
  • 看那桶政策donebox-static

檢查這兩個策略以確定您不允許刪除對象的原因。請記住 - 默認情況下,您不允許執行任何操作,因此您正在尋找確實授予您DeleteObject權限的政策。

+0

相同的代碼也適用於我,當我嘗試與不同區域的不同桶,不知道爲什麼會發生這種情況。 –