Upload and download an object
Upload and download a text object; Cloud Storage stores application files in addressable buckets.
Create a bucket and round-trip a text object locally.
SDK dependencies
Run in a new example directory
python3 -m venv .venv
source .venv/bin/activate
python -m pip install google-cloud-storage Save as hello.py
import os
from google.api_core.exceptions import NotFound
from google.auth.credentials import AnonymousCredentials
from google.cloud import storage
assert os.environ['STORAGE_EMULATOR_HOST']
client = storage.Client(
project=os.environ['GOOGLE_CLOUD_PROJECT'],
credentials=AnonymousCredentials(),
)
bucket = client.bucket("localcloud-sdk-example")
try:
bucket.reload()
except NotFound:
bucket.create()
blob = bucket.blob("data.txt")
blob.upload_from_string("Hello from LocalCloud!")
print(blob.download_as_text()) Run the example
python hello.py Verify the result
The command completes successfully and prints the expected output shown below.
Expected output
Hello from LocalCloud!