Reputation: 785
I've been beating my head trying to figure out how to upload images/videos using Blobstore API to Google Cloud Storage for my App Engine Python web app. Here's is where I am so far:
I use the following code to create an action URL for my HTML form.
upload_url = create_upload_url('/upload', gs_bucket_name='bucket')
I use webapp2/Jinja templates and pass the upload_url into my HTML template.
<form action="{{upload_url}}" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit" value="SUBMIT">
</form>
I know that this will upload the file directly to Google Cloud Storage and then call my /upload
handler with the blob keys.
I'm confused on what to do after this. This is my handler code for /upload
:
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload = self.get_upload()[0]
blob_key = upload.key()
Is this the right way to get the blob_key when uploading images/videos to Google Cloud Storage?
The docs say to use blobstore.create_gs_key('/gs' + 'bucketname' + 'filename')
to create a blob_key. Where does the filename come from? Should this code go into my post function for the upload handler that is called after the object is stored in GCS?
Also, from the Google docs, I'm confused as to what this code does or fits in to the picture:
with gcs.open(filename, 'w') as f:
f.write('abcde\n')
Upvotes: 2
Views: 346
Reputation: 126
Check out this sample here that I just created for you in GitHub:
https://github.com/lucena/gae-upload-gcs
Please let me know if you have any questions on it.
Upvotes: 2