GCS Notification
概要
GCSへの操作をpub/subへイベントとして流せるので、GCS+イベントドリブンな仕様を実現するときに使える
Pub/subの後にFunctionだったりCloudRunだったりを繋げる
ファイルのpath_prefixでイベントの発火をフィルタできる
イベントの種類
OBJECT_FINALIZE
オブジェクト作成が成功
OBJECT_METADATA_UPDATE
OBJECT_DELETE
OBJECT_ARCHIVE
全てのGCS操作はこれらの組み合わせである
通知構成作成
gsutil notification create -t TOPIC_NAME -f json gs://BUCKET_NAME
code:txt
gsutil notification create \
-p <プロジェクト名> \
-f json \
-t projects/<プロジェクト名>/topics/<トピック名> \
-p path_prefix/ \ # パスのプレフィックスを指定する場合
-e OBJECT_FINALIZE \
gs://<バケット名>
-eでイベントタイプを指定
バケットに設定されている通知構成を確認
gsutil notification list gs://hiroki-notification
Terraformでの構成
hiroki.icon裏ではこのマネージドサービスアカウントを使ってGoogleのロジックがバケット置かれたタイミングでpubsubへイベント登録しているということなのだろう
code:terraform
resource "google_storage_notification" "capi_data_notification" {
bucket = var.src_bucket
object_name_prefix = var.src_bucket_prefix
payload_format = "JSON_API_V1"
topic = google_pubsub_topic.gcs_notification.id
custom_attributes = {
targetName = var.target_name
}
}
// Enable notifications by giving the correct IAM permission to the unique service account.
data "google_storage_project_service_account" "gcs_account" {
project = var.project_name
}
resource "google_pubsub_topic_iam_member" "member" {
topic = google_pubsub_topic.gcs_notification.id
role = "roles/pubsub.publisher"
member = "serviceAccount:${data.google_storage_project_service_account.gcs_account.email_address}"
}
// End enabling notifications