Created
May 29, 2014 11:04
-
-
Save phil-r/0aaf8cbbf0d16c5b2386 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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