2013-09-23 118 views
6
import boto 
conn = boto.connect_s3('', '') 

mybucket = conn.get_bucket('data_report_321') 

我可以使用以下代碼從存儲區中下載文件。使用boto上傳文件

for b in mybucket: 
    print b.name 
    b.get_contents_to_filename('0000_part_00', headers=None, cb=None, num_cb=10, torrent=False, version_id=None, res_download_handler=None, response_headers=None) 

但我無法上傳文件。我收到一個錯誤: AttributeError:'str'對象沒有屬性'tell'

send_file或set_contents函數按預期工作。

for b in mybucket: 
    b.send_file('mytest.txt', headers=None, cb=None, num_cb=10, query_args=None, chunked_transfer=False, size=None) 
    b.set_contents_from_file('mytest.txt', headers=None, replace=True, cb=None, num_cb=10, policy=None, md5=None, reduced_redundancy=False, query_args=None, encrypt_key=False, size=None, rewind=False) 

如何使用boto從本地服務器的當前目錄上傳文件到S3存儲桶?


更新: 我需要先調用set_contents_from_filename函數之前申報上傳文件的密鑰(文件名)。

k = boto.s3.key.Key(mybucket) 
k.key = 'uploaded_file.txt' 
k.set_contents_from_filename("myteswt.txt") 

回答

-2

您可以使用此實用程序腳本:https://github.com/TimelyToga/upload_s3

python upload_s3.py <filename> [key_to_upload_as] 

這將上傳任何文件到您指定的S3桶。

雖然,當您上傳set_contents_from_file需要python File對象。