Tomarinator
Tomarinator

Reputation: 782

Uploading a file from python application to appengine

I want to upload a file from a python application that runs on my pc to an appengine app hosted on appengine.

Suppose I have a file foo.txt which I want to upload to appengine, hypothetically I could make this totally ridiculous GET request to this url from my python script on pc.

http://appid.appspot.com/?file=foo.txt 

Since url length is limited to roughly 2000 characters, it wont be possible to pass the file as a GET request, so will have to make a POST request.

How do I make the POST request to appengine app and send the file as a parameter ?

and then handle the request on appengine as follows,

class MainHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        self.response.write(blob_info)

I know this won't work, what is the correct way to do this. Is there any better way to achieve this ?

Upvotes: 0

Views: 238

Answers (2)

dragonx
dragonx

Reputation: 15143

You seem to be aware of the Blobstore ... the question you're asking is specifically addressed in the docs.

https://developers.google.com/appengine/docs/python/blobstore/#Python_Uploading_a_blob

Upvotes: 1

Prahlad Yeri
Prahlad Yeri

Reputation: 3663

Below code works for me for giving a pdf download to the user. Once you read the uploaded file in a StringIO object, you can use this to provide the download.

outputStream = StringIO.StringIO()
output.write(outputStream) #write merged output to the StringIO object

fname = 'filename'
self.response.headers['Content-Disposition'] = 'attachment; filename=' + fname + '.pdf'
self.response.write(outputStream.getvalue())

Then, from your appengine app, you can read this response, store it as a StringIO object and use the google file API to store it as a disk file.

Refer this SO link to learn how to get an uploaded file into a StringIO object.

To know how exactly I generate this pdf before writing the output, read my entire article.

Upvotes: 1

Related Questions