I am working on a news app and would like to cache the news images on my own google cloud storage.
I am planning to use Flask on GAE. All examples I have found relate to upload a file from user's browser into cloud storage.
What is the best way to obtain an image via an url and upload it to google cloud storage? I hope this makes sense, please feel free to suggest improvements. Many Thanks
def main():
bucket_name = os.environ.get('BUCKET_NAME',
app_identity.get_default_gcs_bucket_name())
bucket = '/' + bucket_name
filename = bucket + '/image_name'
image_url = "http://news.com/crash.jpg"
try:
create_file(image_url, filename)
except Exception, e:
logging.exception(e)
return "Success", 201
def create_file(image_url, filename):
image = cStringIO.StringIO(urllib.urlopen(image_url).read()) // Not sure about this
img = Image.open(image)
write_retry_params = gcs.RetryParams(backoff_factor=1.1)
gcs_file = gcs.open(filename,
'w',
content_type='image/jpeg', // ???? Is MIME type correct?
options={'x-goog-acl': 'public'},
retry_params=write_retry_params)
gcs_file.write(img)
gcs_file.close()