Upload a file using boto

廉价感情. 提交于 2019-12-21 12:46:35

问题


import boto
conn = boto.connect_s3('',  '')

mybucket = conn.get_bucket('data_report_321')

I can download the file from a bucket using the following code.

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)

But I am not able to upload a file. I get an error: AttributeError: 'str' object has no attribute 'tell'

send_file nor set_contents functions are working as expected.

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)

How do I upload a file from current directory of local server to S3 bucket using boto?


Update: I need to declare the key (filename) of the uploaded file first before calling the set_contents_from_filename function.

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

回答1:


Both send_file and set_contents_from_file take a File object for first argument. If you would like to pass a string you should see set_contents_from_filename.




回答2:


You could use this utility script: https://github.com/TimelyToga/upload_s3

python upload_s3.py <filename> [key_to_upload_as]

That will upload any file to a s3 bucket that you specify.

Although, when you are uploading set_contents_from_file takes a python File object.



来源:https://stackoverflow.com/questions/18952684/upload-a-file-using-boto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!