Ross Manson
Ross Manson

Reputation: 339

Upload to Google Cloud Storage

I'm trying to upload to cloud storage from my app engine app but it keeps uploading the name of the file instead of its contents.

The html is pretty simple:

<form id="uploadbanner" enctype="multipart/form-data" action="/upload">
    <input id="fileupload" name="myfile" type="file" />
    <input type="submit" value="submit" id="submit" />
</form>

The python element goes like this:

class Uploader(webapp2.RequestHandler):
def get(self):
    objectname = '%s'%str(uuid.uuid4())
    objectpath = '/gs/bucketname/%s' %objectname
    upload = self.request.get('myfile')
    writable = files.gs.create(filename=objectpath, mime_type='application/octet-stream', acl='public-read')
    with files.open(writable, 'a') as f:
        f.write(upload)
    files.finalize(writable)
    self.response.write('done')

I know f.write(upload) is definitely wrong but I have no idea what i'm supposed to replace it with

Upvotes: 2

Views: 269

Answers (2)

Ross Manson
Ross Manson

Reputation: 339

It needs to be POST not GET, so add method="POST" to the form and make the python function post instead of get too, that sorted it out for me.

Upvotes: 2

CMDej
CMDej

Reputation: 294

You should probably let the browser upload directly to GCS, and only get your handler called afterwards.

To get a short overview of how this can be done, look at this post: Upload images/video to google cloud storage using Google App Engine

Even if the blobstore API seems to be called in this example, the files are uploaded to Cloud Storage.

Upvotes: 2

Related Questions