Skip to content

Instantly share code, notes, and snippets.

@rickcrawford
Last active April 27, 2021 15:55
Show Gist options
  • Save rickcrawford/6ca020bd9a26e9233f5fdac98f63e658 to your computer and use it in GitHub Desktop.
Save rickcrawford/6ca020bd9a26e9233f5fdac98f63e658 to your computer and use it in GitHub Desktop.
Creating a global bucket and then creating a logging sink
from google.cloud.logging_v2.types import logging_config
from google.cloud.logging_v2.services.config_service_v2 import ConfigServiceV2Client
from google.protobuf.field_mask_pb2 import FieldMask
#FYI - https://realpython.com/python-f-strings/ for "f"
REGION = "global"
PROJECT_ID = "rjc-testing-project"
BUCKET_ID="my-bucket-id"
BUCKET_PARENT = f"projects/{PROJECT_ID}/locations/{REGION}"
client = ConfigServiceV2Client()
# create bucket
bucket = logging_config.LogBucket(name=f"{BUCKET_PARENT}/buckets/{BUCKET_ID}")
request = logging_config.CreateBucketRequest(parent=bucket_parent, bucket_id=bucket_id, bucket=bucket)
client.create_bucket(request)
# Imports the Google Cloud client library
from google.cloud import logging
# Instantiates a client
logging_client = logging.Client()
destination = f"logging.googleapis.com/projects/{PROJECT_ID}/locations/{REGION}/buckets/{BUCKET_ID}"
filter_ = None
sink = logging_client.sink(sink_name, filter_=filter_, destination=destination)
assert not sink.exists() # API call
sink.create() # API call
assert sink.exists() # API call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment