Created
August 20, 2019 15:57
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ 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