2

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

admdrew
  • 3,790
  • 4
  • 27
  • 39
Ross Manson
  • 339
  • 1
  • 3
  • 14

2 Answers2

2

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.

Community
  • 1
  • 1
CMDej
  • 294
  • 2
  • 7
2

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.

Ross Manson
  • 339
  • 1
  • 3
  • 14