Libraries
pip install --upgrade google-cloud-storage
or edit requirements.txt
# https://pypi.org/project/google-cloud-storage/
google-cloud-storage==1.16.1
Create Service Account
Option 1: Download App Engine default service account
Access Google Cloud Console -> Service accounts.
Select [email protected]
/ App Engine default service account
, click on the 3-vertical dots of Actions
and select Create key
. Select JSON
for Key type
the click Create
. Save the JSON file PROJECT_ID-appengine.json
.
NOTE: This key have access to all App Engine resources, so be extra careful with it.
Option 2: Create Cloud Storage only service account key
Access Google Cloud Console -> Create service account key
- Service account: New service account
- Service account name:
storage
- Role: Storage ->
Storage Admin
orStorage Object Admin
orStorage Object Creator
- Key type: JSON
Click create. Save the JSON file PROJECT_ID-storage.json
.
Option 3: Project Owner key
Assuming you want to use the same key for all google cloud resources (App Engine, Cloud Functions, Datastore, Storage, etc.)
Access Google Cloud Console -> Create service account key
- Service account: New service account
- Service account name:
owner
- Role: Project ->
Owner
- Key type: JSON
Click create. Save the JSON file PROJECT_ID-owner.json
.
Python Storage
from google.cloud import storagedef init_storage(): # export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/[FILE_NAME].json" # os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "/home/user/Downloads/[FILE_NAME].json" # return storage.Client() return storage.Client.from_service_account_json('keys/PROJECT_ID-storage.json')storage_client = init_storage()# list bucketsfor bucket in storage_client.list_buckets(): print(bucket)# write to bucketBUCKET_NAME = '...'# check bucket at https://console.cloud.google.com/storage/browserbucket = storage_client.get_bucket('BUCKET_NAME')filename = "DIRECTORY_NAME/FILENAME"blob = bucket.blob(filename)# blob.upload_from_string('Hello World', content_type='text/plain', predefined_acl='publicRead')blob.upload_from_string('Hello World')
References