Skip to content

Instantly share code, notes, and snippets.

@cleberjsantos
Created August 20, 2019 15:57
Show Gist options
  • Select an option

  • Save cleberjsantos/05d6907b69e7914cd35c56210175688b to your computer and use it in GitHub Desktop.

Select an option

Save cleberjsantos/05d6907b69e7914cd35c56210175688b to your computer and use it in GitHub Desktop.
Using Google Cloud Storage API client library to store data on Google infrastructure
""" Using: google-cloud-storage """
import os
from google.cloud import storage
# https://cloud.google.com/storage/docs/authentication#generating-a-private-key
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'PATH TO YOUR FILE JSON'
client = storage.Client()
# https://console.cloud.google.com/storage/browser/[bucket-id]/
bucket = client.get_bucket('bucket-id')
bucket_path = 'app/users'
# Download an image eg.
# https://www.dictionary.com/e/wp-content/uploads/2018/03/42.jpg
actual_path = os.getcwd()
img = '42.jpg'
if bucket.get_blob('{}/{}'.format(bucket_path, img)):
print('Overwriting Existing Data!')
blob = bucket.blob('{}/{}'.format(bucket_path, img))
blob.upload_from_filename(filename='{}/{}'.format(actual_path, img))
print(blob.__dict__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment