简体   繁体   中英

Python Django : Creating file object in memory without actually creating a file

I have an endpoint where I want to collect the response data and dump it into a file on S3 like this - https://stackoverflow.com/a/18731115/4824482

This is how I was trying to do it -

file_obj = open('/some/path/log.csv', 'w+')
file_obj.write(request.POST['data'])

and then passing file_obj to the S3 related code as in the above link.

The problem is that I don't have permissions to create a file on the server. Is there any way I can create a file object just in memory and then pass it to the S3 code?

Probably that's duplicate question of How to upload a file to S3 without creating a temporary local file . You would find best suggestion by checking out answers to that question.

Shortly the answer is code below:

from boto.s3.key import Key
k = Key(bucket)
k.key = 'yourkey'
k.set_contents_from_string(request.POST['data'])

Try tempfile https://docs.python.org/2/library/tempfile.html

f = tempfile.TemporaryFile()
f.write(request.POST['data'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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