【问题标题】:Upload a file using boto使用 boto 上传文件
【发布时间】:2023-04-07 08:11:01
【问题描述】:
import boto
conn = boto.connect_s3('',  '')

mybucket = conn.get_bucket('data_report_321')

我可以使用以下代码从存储桶下载文件。

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")

【问题讨论】:

    标签: python boto


    【解决方案1】:

    send_fileset_contents_from_file 都将 File 对象作为第一个参数。如果你想传递一个字符串,你应该看到set_contents_from_filename

    【讨论】:

    • set_contents_from_filename 没有出现任何错误,但文件并未真正上传到该存储桶。我错过了什么吗?
    【解决方案2】:

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

    python upload_s3.py <filename> [key_to_upload_as]
    

    这会将任何文件上传到您指定的 s3 存储桶。

    虽然,当您上传 set_contents_from_file 时需要一个 python File 对象。

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 2017-08-18
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多