GoogleCloudPlatformのローカルエミュレータ(bigtable,pubsub,storage)の使い方メモ
GoogleCloudPlatformのエミュレータを
ローカルPC上でdockerを使って動かす時のメモです。
エミュレータを起動して、Pythonのコードでアクセスしてみます。
ここでは、以下3サービスを動かしてみます。
- bigtable
- pubsub
- storage
datastore, firestore, spanner も同様の手順で利用できると思います。
エミュレータの起動
ここでは、docker composeを使って動かします。
次のようなdocker-compose.yml
を用意します。
docker-compose.yml
version: "3.9"
services:
bigtable:
image: "google/cloud-sdk:430.0.0"
command: gcloud beta emulators bigtable start --host-port 0.0.0.0:8086
ports:
- 8086:8086
pubsub:
image: "google/cloud-sdk:430.0.0"
command: gcloud beta emulators pubsub start --host-port 0.0.0.0:8085 --project=dummy-project
ports:
- 8085:8085
storage:
image: "oittaa/gcp-storage-emulator:v2022.06.11"
command: start --host=0.0.0.0 --port=8080 --in-memory --default-bucket=dummy-bucket
ports:
- 8080:8080
以下のコマンドで起動します。
docker compose up
bigtableエミュレータの利用
bigtableエミュレータにアクセスしてみます。
次のサンプルコードを用意します。
bigtable-sample.py
from google.auth.credentials import AnonymousCredentials
from google.cloud import bigtable
from google.cloud.bigtable import column_family
if __name__=='__main__':
project_id = "dummy-project"
instance_id = "quick-start"
client: bigtable.Client = bigtable.Client(
project=project_id,
credentials=AnonymousCredentials(),
admin=True)
instance: bigtable.instance.Instance = client.instance(instance_id)
# create table
table = instance.table("tableA")
if not table.exists():
table.create(column_families={"cfA": column_family.MaxVersionsGCRule(2)})
# list table
tables = [e.table_id for e in instance.list_tables()]
print(tables)
以下のコマンドで実行します。
環境変数BIGTABLE_EMULATOR_HOST
の指定で、接続先をエミュレータに向けます。
BIGTABLE_EMULATOR_HOST=localhost:8086 python bigtable-sample.py
pubsubエミュレータの利用
pubsubエミュレータにアクセスしてみます。
次のサンプルコードを用意します。
pubsub-sample.py
from google.auth.credentials import AnonymousCredentials
from google.api_core.exceptions import AlreadyExists
from google.cloud import pubsub_v1
if __name__=='__main__':
project_id = "dummy-project"
# create topic
publisher = pubsub_v1.PublisherClient()
try:
topic = publisher.create_topic(
name=publisher.topic_path(project_id, "topicA"))
except AlreadyExists as ex:
print(ex)
# list topics
client = pubsub_v1.PublisherClient(credentials=AnonymousCredentials())
topics = [
e.name
for e
in client.list_topics(
request={"project": client.common_project_path(project_id)})
]
print(topics)
以下のコマンドで実行します。
環境変数PUBSUB_EMULATOR_HOST
の指定で、接続先をエミュレータに向けます。
PUBSUB_EMULATOR_HOST=localhost:8085 python pubsub-sample.py
storageエミュレータの利用
storageエミュレータにアクセスしてみます。
次のサンプルコードを用意します。
storage-sample.py
from google.auth.credentials import AnonymousCredentials
from google.cloud import storage
if __name__=='__main__':
project_id = "dummy-project"
client: storage.Client = storage.Client(
project=project_id,
credentials=AnonymousCredentials())
# create bucket
bucket: storage.Bucket = client.get_bucket("dummy-bucket")
# list buckets
buckets = [e.name for e in client.list_buckets()]
print(buckets)
以下のコマンドで実行します。
環境変数STORAGE_EMULATOR_HOST
の指定で、接続先をエミュレータに向けます。
STORAGE_EMULATOR_HOST=http://localhost:8080 python storage-sample.py
GoogleCloudStorageは公式エミュレータが無いのですが、
SDK側には環境変数の指定でエミュレータを利用する事ができるようになっています。
以上。