【问题标题】:Uploading files to S3 bucket - Python Django将文件上传到 S3 存储桶 - Python Django
【发布时间】:2016-07-29 13:47:10
【问题描述】:

我想将一个字符串(这是一个 xml 响应)放入一个文件中,然后将它上传到一个 amazon s3 存储桶。以下是我在 Python3 中的函数

def excess_data(user, resume):
    res_data = BytesIO(bytes(resume, "UTF-8"))
    file_name = 'name_of_file/{}_resume.xml'.format(user.id)
    s3 = S3Storage(bucket="Name of bucket", endpoint='s3-us-west-2.amazonaws.com')
    response = s3.save(file_name, res_data)
    url = s3.url(file_name)
    print(url)
    print(response)
    return get_bytes_from_url(url)

但是,当我运行此脚本时,我不断收到错误 - AttributeError: Unable to determine the file's size. 知道如何解决这个问题吗?

提前致谢!

【问题讨论】:

    标签: python django amazon-web-services amazon-s3


    【解决方案1】:

    我在我的 Django 项目中使用 s3boto(需要 python-boto)来连接 S3。

    使用boto很容易做你想做的事:

    >>> from boto.s3.key import Key
    >>> k = Key(bucket)
    >>> k.key = 'foobar'
    >>> k.set_contents_from_string('This is a test of S3')
    

    请参阅此documentation 存储数据部分。

    希望对您的问题有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-25
      • 2018-03-25
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 2018-04-04
      • 1970-01-01
      相关资源
      最近更新 更多