Skip to content

Instantly share code, notes, and snippets.

@phil-r
Created May 29, 2014 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phil-r/0aaf8cbbf0d16c5b2386 to your computer and use it in GitHub Desktop.
Save phil-r/0aaf8cbbf0d16c5b2386 to your computer and use it in GitHub Desktop.
from google.appengine.api import urlfetch, files
from google.appengine.api.urlfetch_errors import ResponseTooLargeError, DeadlineExceededError
GS_PICTURES_BUCKET = 's-pictures/'
GS_FULL = 'http://commondatastorage.googleapis.com/'
GS_FULL_PICTURES = GS_FULL + GS_PICTURES_BUCKET
GS_PATH = '/gs/'
GS_PICTURES_PATH = GS_PATH + GS_PICTURES_BUCKET
def create_gs(url, name, mime='application/octet-stream'):
try:
result = urlfetch.fetch(url, deadline=30)
except ResponseTooLargeError:
logging.warning("u fetch fail for %s" % url)
return None
except DeadlineExceededError:
return create_gs(url, name, mime)
# Create a file that writes to Cloud Storage and is readable by everyone
# in the project.
write_path = files.gs.create(GS_PICTURES_PATH + name, mime_type=mime, acl='public-read')
# Write to the file.
with files.open(write_path, 'a') as fp:
fp.write(result.content)
# Finalize the file so it is readable in Google Cloud Storage.
files.finalize(write_path)
return GS_FULL_PICTURES + name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment