2015-03-02 233 views
0

我試圖用博託代碼下載從AWS S3的.zip文件,但得到以下錯誤:蟒蛇,AWS S3下載的zip文件

「海峽」對象沒有屬性「寫」

代碼

s3Conn = S3Connection(aws_access_key_id='ABCD', 
         aws_secret_access_key='xyz') 
s3bucket = s3Conn.get_bucket(mybucket) 

key = s3bucket.new_key("2015/02/20/TestFile.zip") 
try: 
    key.get_contents_to_file(backupdir) 
except: 
    print("{0}".format(sys.exc_info())) 

回答

2

我假定backupdir是字符串,但預計博託文件對象(如open(backupdir)

+1

您可以使用''get_contents_to_filename''作爲字符串路徑,但它必須是完全限定的路徑,而不僅僅是目錄的路徑。 – garnaat 2015-03-02 15:05:04

+0

謝謝Andrey和garnaat:發佈了下面的代碼。 – 2015-03-02 15:09:02

0
def downloadBackupFile(backupdir): 
    key = s3bucket.new_key("2015/02/20/TestFile.zip") 
    try: 
     if (os.path.exists(backupdir)): 
     key.get_contents_to_file(open(backupdir + "TestFile.zip", "w+")) 
    except: 
     print("{0}".format(sys.exc_info()))