Browse documentation SDK Examples
Reference SDK Examples

SDK examples

LocalCloud accepts selected Google Cloud SDK workflows, but compatibility is service- and operation-specific. Start the runtime and load the endpoint values generated for the selected data volume before running any example:

localcloud start
eval "$(localcloud env)"

Do not hard-code canonical ports when using the host CLI: occupied ports may be remapped. Run examples in the same shell or inject the generated variables into the test process. If a client tries to contact real Google Cloud, stop rather than adding real credentials.

Cloud Storage: Python

This example is repeatable against a persistent local volume:

from google.api_core.exceptions import NotFound
from google.cloud import storage

client = storage.Client(project="local-gcp-project")
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())

Cloud Storage: Node.js

const { Storage } = require('@google-cloud/storage');

const storage = new Storage({ projectId: 'local-gcp-project' });
const bucket = storage.bucket('localcloud-sdk-example');

async function main() {
  const [exists] = await bucket.exists();
  if (!exists) await storage.createBucket(bucket.name);
  await bucket.file('data.txt').save('Hello from LocalCloud!');
  const [content] = await bucket.file('data.txt').download();
  console.log(content.toString());
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Pub/Sub: Python

Pub/Sub state is volatile across emulator process restarts. Use stable names and tolerate resources that already exist during one run.

from google.api_core.exceptions import AlreadyExists
from google.cloud import pubsub_v1

project = "local-gcp-project"
publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
topic = publisher.topic_path(project, "localcloud-sdk-topic")
subscription = subscriber.subscription_path(project, "localcloud-sdk-sub")

try:
    publisher.create_topic(request={"name": topic})
except AlreadyExists:
    pass

try:
    subscriber.create_subscription(request={"name": subscription, "topic": topic})
except AlreadyExists:
    pass

publisher.publish(topic, b"Hello, Pub/Sub!").result()
response = subscriber.pull(request={"subscription": subscription, "max_messages": 1})
for received in response.received_messages:
    print(received.message.data.decode())
    subscriber.acknowledge(
        request={"subscription": subscription, "ack_ids": [received.ack_id]}
    )

BigQuery: deterministic query

BigQuery behavior is release-unverified until the dependency source and assembled LocalCloud image are identified and qualified together. Start with a deterministic query rather than undeclared public data:

from google.auth.credentials import AnonymousCredentials
from google.cloud import bigquery

client = bigquery.Client(
    project="local-gcp-project",
    credentials=AnonymousCredentials(),
)
rows = list(client.query("SELECT 1 AS ok").result())
assert rows[0].ok == 1

Feature-specific compatibility remains partial; see the BigQuery guide before relying on scripting, external tables, Storage API behavior, or type semantics.

Selecting another project

Generated environment output uses the configured project. Manual-Docker users can select an explicit project in the request:

eval "$(curl -fsS 'http://localhost:24080/env?format=shell&project=my-local-project')"

Keep client project IDs consistent with the generated GOOGLE_CLOUD_PROJECT value.

Production validation

Local checks cover only documented workflows. Before production deployment:

  1. Start a clean process without LocalCloud emulator or custom-endpoint variables.
  2. Intentionally configure an isolated real Google Cloud project and credentials.
  3. Run production-bound integration tests for IAM, quotas, networking, regional behavior, concurrency, recovery, and unsupported operations.

Do not treat protocol similarity or a passing emulator test as full Google Cloud parity. Organization and team-CI use must also comply with the governing proprietary license.

Maintained by LocalCloud