2016-07-29 71 views
1

我想把一个字符串(这是一个XML响应)到一个文件,然后将其上传到亚马逊S3桶。以下是我在Python3上传文件到S3桶 - Python Django

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.不知道如何解决这个问题?

在此先感谢!

回答

2

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

使用博托是很容易做你想做的事:

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

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

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

相关问题