简体   繁体   中英

Uploading file from external URL on Google App Engine with Python

Is it possible to upload an image from an external site (ie www.example.com/image.png) to the Google App Engine? This is not the same as uploading a file from a user submitted form. If it's possible, any solutions? I'm also using Google Cloud Storage, so if anybody has found a way to accomplish the same thing by uploading straight to Google Cloud Storage, please let me know.

-- UPDATE ---

I've followed the example from here - https://developers.google.com/appengine/docs/python/googlecloudstorageclient/getstarted and replaced the text write "abcd" with this:

url="example.com/image.jpeg"
opener1 = urllib2.build_opener()
page1 = opener1.open(url)

write_retry_params = gcs.RetryParams(backoff_factor=1.1)
gcs_file = gcs.open(filename,
                        'w',
                        content_type='image/jpeg',
                        options={'x-goog-meta-foo': 'foo',
                                 'x-goog-meta-bar': 'bar'},
                        retry_params=write_retry_params)
gcs_file.write(page1.read())
gcs_file.close()

The problem is - when I run this code, it tries to download the image to my computer (the client) instead of downloading to the gcs_file. I get the download file popup.... That's not what I'm trying to do. What am I doing wrong?

Yes, you are looking at using the google cloud storage client library. Specifically this: https://developers.google.com/appengine/docs/python/googlecloudstorageclient/functions#open

Open it for writing then write the url fetched image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM