使用boto上传文件 [英] Upload a file using boto
问题描述
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)
但是我无法上传文件.我收到一个错误: AttributeError:"str"对象没有属性"tell"
But I am not able to upload a file. I get an error: AttributeError: 'str' object has no attribute 'tell'
send_file或set_contents函数均按预期工作.
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)
如何使用Boto将文件从本地服务器的当前目录上传到S3存储桶?
How do I upload a file from current directory of local server to S3 bucket using boto?
更新: 在调用set_contents_from_filename函数之前,我需要先声明上载文件的密钥(文件名).
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")
推荐答案
set_contents_from_file 将File
对象用作第一个参数.如果您想传递字符串,则应该看到 set_contents_from_filename .
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.
这篇关于使用boto上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!