Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading an Image from an external link to google cloud storage using google app engine python

I'm looking for a solution on how to upload a picture from an external url like http://example.com/image.jpg to google cloud storage using appengine python,

I am now using

blobstore.create_upload_url('/uploadSuccess', gs_bucket_name=bucketPath)

for users that want to upload a picture from their computer, calling

images.get_serving_url(gsk,size=180,crop=True)

on uploadSuccess and storing that as their profile image. I'm trying to allow users to use their facebook or google profile picture after they login with oauth2. I have access to their profile picture link, and I would just like to copy it for consistency. Pease help :)

like image 399
Rob Avatar asked Aug 20 '14 18:08

Rob


2 Answers

To upload an external image you have to get it and save it. To get the image you van use this code:

from google.appengine.api import urlfetch

file_name = 'image.jpg'
url = 'http://example.com/%s' % file_name
result = urlfetch.fetch(url)
if result.status_code == 200:
    doSomethingWithResult(result.content)

To save the image you can use the app engine GCS client code shown here

import cloudstorage as gcs
import mimetypes

doSomethingWithResult(content):

    gcs_file_name = '/%s/%s' % ('bucket_name', file_name)
    content_type = mimetypes.guess_type(file_name)[0]
    with gcs.open(gcs_file_name, 'w', content_type=content_type,
                  options={b'x-goog-acl': b'public-read'}) as f:
        f.write(content)

    return images.get_serving_url(blobstore.create_gs_key('/gs' + gcs_file_name))
like image 113
voscausa Avatar answered Nov 16 '22 13:11

voscausa


Here is my new solution (2019) using the google-cloud-storage library and upload_from_string() function only (see here):

from google.cloud import storage
import urllib.request

BUCKET_NAME = "[project_name].appspot.com" # change project_name placeholder to your preferences
BUCKET_FILE_PATH = "path/to/your/images" # change this path

def upload_image_from_url_to_google_storage(img_url, img_name):
    """
    Uploads an image from a URL source to google storage.
    - img_url: string URL of the image, e.g. https://picsum.photos/200/200
    - img_name: string name of the image file to be stored
    """
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(BUCKET_NAME)
    blob = bucket.blob(BUCKET_FILE_PATH + "/" + img_name + ".jpg")

    # try to read the image URL
    try:
        with urllib.request.urlopen(img_url) as response:
            # check if URL contains an image
            info = response.info()
            if(info.get_content_type().startswith("image")):
                blob.upload_from_string(response.read(), content_type=info.get_content_type())
                print("Uploaded image from: " + img_url)
            else:
                print("Could not upload image. No image data type in URL")
    except Exception:
        print('Could not upload image. Generic exception: ' + traceback.format_exc())
like image 27
Timo Wagner Avatar answered Nov 16 '22 14:11

Timo Wagner