Access Google Cloud Storage From External Server (Python)

June 7, 2019

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 PROJECT_ID@appspot.gserviceaccount.com / 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 or Storage Object Admin or Storage 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 storage

def 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')

datastore_client = init_datastore()

# list buckets
for bucket in storage_client.list_buckets():
    print(bucket)

# write to bucket
BUCKET_NAME = '...'

# check bucket at https://console.cloud.google.com/storage/browser
bucket = 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

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.