2016-07-29 156 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部分存儲數據。

我希望它對您的問題有所幫助。