Upload Files in Google App Engine

Upload files in Google App Engine

Here is a complete, working file. I pulled the original from the Google site and modified it to make it slightly more real world.

A few things to notice:

  1. This code uses the BlobStore API
  2. The purpose of this line in the
    ServeHandler class is to "fix" the
    key so that it gets rid of any name
    mangling that may have occurred in
    the browser (I didn't observe any in
    Chrome)

    blob_key = str(urllib.unquote(blob_key))
  3. The "save_as" clause at the end of this is important. It will make sure that the file name does not get mangled when it is sent to your browser. Get rid of it to observe what happens.

    self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True)

Good Luck!

import os
import urllib

from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

class MainHandler(webapp.RequestHandler):
def get(self):
upload_url = blobstore.create_upload_url('/upload')
self.response.out.write('<html><body>')
self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit" name="submit" value="Submit"> </form></body></html>""")

for b in blobstore.BlobInfo.all():
self.response.out.write('<li><a href="/serve/%s' % str(b.key()) + '">' + str(b.filename) + '</a>')

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
blob_info = upload_files[0]
self.redirect('/')

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, blob_key):
blob_key = str(urllib.unquote(blob_key))
if not blobstore.get(blob_key):
self.error(404)
else:
self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True)

def main():
application = webapp.WSGIApplication(
[('/', MainHandler),
('/upload', UploadHandler),
('/serve/([^/]+)?', ServeHandler),
], debug=True)
run_wsgi_app(application)

if __name__ == '__main__':
main()

How to upload files to custom folder in AppEngine with blobstore and google cloud storage

It comes out that you can pass a "folder" name with a bucket name into .withGoogleStorageBucketName() method, like:

UploadOptions uploadOptions = UploadOptions.Builder
.withGoogleStorageBucketName("bucket_name/folder_name");

How to upload multipart/form files with Google App Engine?

As described in the public documentation(https://cloud.google.com/appengine/docs/standard/python3/how-requests-are-handled) the maximum request size is 32Mb in App Engine.

What you can actually do and what you should do always is to make the users upload the files directly into Google Cloud Storage. This will remove this “blockage” for you

How to upload file from Google App Engine to Google Cloud Storage using Python Flask?

So the issue is not with uploading to GCS but with the temporary file you create.

The only directory that has write access is /tmp and be aware that it is not shared between GAE instances and it disappears when the instance is restarted...

from google.cloud import storage

def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""
# SOURCE:
# https://cloud.google.com/storage/docs/uploading-objects#uploading-an-object

# The ID of your GCS bucket
# bucket_name = "your-bucket-name"
# The path to your file to upload
# source_file_name = "local/path/to/file"
# The ID of your GCS object
# destination_blob_name = "storage-object-name"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print(
"File {} uploaded to {}.".format(
source_file_name, destination_blob_name
)
)

TMP_PATH = "/tmp/"
TMP_FILE_NAME = f"{TMP_PATH}file.xlsx"
BUCKET_NAME = '<your-bucket-name>'

with open(TMP_FILE_NAME, "w") as outfile:
outfile.write("write file action goes here")
upload_blob(BUCKET_NAME, TMP_FILE_NAME, "Target file path goes here...")

Uploading Files to Google Cloud Storage via Google App Engine using Java: (No such file or directory)

I just came up with a solution so here is my answer to my own question:

Fixed it by using signed url for my Goolge Cloud Storage bucket. I was on this problem for a week and figured that there is no proper way to get to your own file path(like C:/users/) when you are running your project on GAE.

Guess this google doc(uploading objects to GCS is only helpful when your project is running locally. So the solution to my problem was to generate a signed URL for my bucket and access to the bucket by it. Here is the link that was helpful: how to create signed url for GCS

Thank you to all who answered this question anyways!

how do I upload files to my google app engine project without deploying?

As updating the code or part of it will be always needed (even if you only wanat upload statics files), there is no way to upload specific files, what you can do is to set up a Git repositorie to enable the Push-to-Deploy feature [1].

If you'd die for having this functionality, you could give it a try opening a feature request [2].

[1] https://cloud.google.com/tools/repo/push-to-deploy-quickstart

[2] https://code.google.com/p/googleappengine/wiki/FilingIssues?tm=3

Uploading large video file to Google App Engine

Using uploaded_file.read() produces bytes, not a string. You should open the file in binary mode:

with open(tmp_file_path, 'ab') as f:

Is it POSSIBLE for Google App Engine standard app to access local computer's files?

Fixed it by using signed url for my Goolge Cloud Storage bucket. I was on this problem for a week and figured that there is no proper way to get to your own file path(like C:/users/) when you are running your project on GAE.

Guess this google doc(uploading objects to GCS is only helpful when your project is running locally. So the solution to my problem was to generate a signed URL for my bucket and access to the bucket by it. Here is the link that was helpful: how to create signed url for GCS

Thank you to all who answered this question anyways!



Related Topics



Leave a reply



Submit