Upload image from external link to google cloud storage with google python

I am looking for a solution on how to upload an image from an external url like http://example.com/image.jpg

google cloud storage using the python appengine,

Now I am using

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

      

for users who want to upload an image from their computer by calling

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

      

to uploadSuccess and save that as their profile image. I am trying to allow users to use their facebook or google image after login using oauth2. I have access to their profile picture link and I would just like to copy it for consistency. Pease help :)

+5


source to share


3 answers


To load an external image, you must retrieve and save it. To get the image, you 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 GCS client code of the program 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))

      

+11


source


If you're looking for an updated way to do this, relying on a package storages

, I've written these two functions:

import requests
from storages.backends.gcloud import GoogleCloudStorage


def download_file(file_url, file_name):
    response = requests.get(file_url)
    if response.status_code == 200:
        upload_to_gc(response.content, file_name)


def upload_to_gc(content, file_name):
    gc_file_name = "{}/{}".format("some_container_name_here", file_name)
    with GoogleCloudStorage().open(name=gc_file_name, mode='w') as f:
        f.write(content)

      

Then usually call download_file()

and pass to url

and prefered_file_name

from anywhere in your system.



The class GoogleCloudStorage

came from a package django-storages

.

pip install django-storages

Django Vaults

0


source


Here is my new solution (2019) using only library google-cloud-storage

and function upload_from_string()

(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())

      

0


source







All Articles