简体   繁体   English

将文件上传到S3存储桶-Python Django

[英]Uploading files to S3 bucket - Python Django

I want to put a string (which is an xml response) into a file and then upload it to a amazon s3 bucket. 我想将一个字符串(这是一个xml响应)放入文件中,然后将其上传到amazon s3存储桶中。 Following is my function in Python3 以下是我在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)

However, when I run this script I keep getting the error - AttributeError: Unable to determine the file's size. 但是,当我运行此脚本时,我不断收到错误AttributeError: Unable to determine the file's size. Any idea how to resolve this? 任何想法如何解决这个问题?

Thanks in advance! 提前致谢!

I am using s3boto (requires python-boto) in my Django projects to connect with S3. 我在Django项目中使用s3boto (需要python-boto)与S3连接。

Using boto is easy to do what you want to do: 使用boto可以轻松完成您想做的事情:

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

See this documentation the section Storing Data. 请参阅本文档的“存储数据”部分。

I hope that it helps with your problem. 希望对您的问题有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM