I\'m sure the answer to this question is easy, but for me it\'s proven to be very frustrating since I can\'t put any solution I\'ve found into practical code for my own use.
I have created a gist showing how to upload and use GCS files: https://gist.github.com/voscausa/9541133
This is the code to handle the multipart form post:
class GcsUpload(webapp2.RequestHandler):
def post(self):
field_storage = self.request.POST.get("file", None)
if isinstance(field_storage, cgi.FieldStorage):
file_name = field_storage.filename
dyn = gcs_data.Dynamics(id=file_name, filename=file_name)
gcs_file_name = gcs_data.gcs_write_blob(dyn, field_storage.file.read())
gcs_data.gcs_serving_url(dyn)
dyn.put()
logging.info('Uploaded and saved in default GCS bucket : ' + gcs_file_name)
self.response.headers[b'Content-Type'] = gcs_data.gcs_content_type(dyn)
self.response.write(gcs_data.gcs_read_blob(dyn))
else:
logging.error('GCS Upload failed')
If I understand correctly, what you're trying to do is serve a form from App Engine that allows a user to choose a file to upload. Since the uploaded file may be large, you don't want to handle the upload in App Engine, but have the file uploaded directly to Google Cloud Storage.
This can be done, and it's not too difficult. In fact, this is exactly the example given in the App Engine Python Blobstore docs. It might be confusing that the example refers to Blobstore while you want the file in Cloud Storage - but it's OK - it appears that since version 1.7.0 you can do this:
upload_url = blobstore.create_upload_url('/upload', gs_bucket_name='my_bucket')
(instead of step 1 in the example I linked), and the upload URL will upload straight to Cloud Storage.
Now, your form action should be the string upload_url
that was returned by the blobstore.create_upload_url
function.
After the form completes processing (meaning the file is uploaded), it will redirect to the argument you passed to that function (in the example- to /upload
).
You do not need to write your own POST processing function, as you described in the question.