Cloud Pub/Sub Notifications for Cloud Storage on sub directory

本秂侑毒 提交于 2020-07-06 19:48:06

问题


I want to listen to changes on GCS in sub bucket

I tried this

gsutil notification create -t  my-topic -f json gs://my-bucket

but it gives me notification for changes in all objects

is there a way to get only from sub directory, something like this:

gsutil notification create -t  my-topic -f json gs://my-bucket/sub-dir

回答1:


There is indeed a way to limit the notifications to a "directory" inside a bucket. Bear in mind that Cloud Storage is a "flat" storage system, where the concept of directory does not exist; instead, GCS interprets blobs that have a name ending in / as a folder, but the reality is that when an object is created inside a "folder", the only difference is that it has the folder name as a prefix in the object name. Then, a structure like:

gs://my-bucket
|_objectA
|_folder
  |_objectB
  |_subfolder
    |_objectC

Would translate into the following in terms of object names:

# Object names
gs://my-bucket/objectA
gs://my-bucket/folder/
gs://my-bucket/folder/objectB
gs://my-bucket/folder/subfolder/
gs://my-bucket/folder/subfolder/objectC

Knowing that, you can use the -p option with the gsutil notification create command in order to specify a prefix path filter for the objects that you want to get notifications from. It would be something like:

gsutil notification create -t my-topic -f json -p folder/ gs://my-bucket

Note that the -p flag just sets the path prefix for an object, so you can use it also to create a notification alert for all objects whose name starts by a given string. In this case, if this given string ends with a /, you will be indicating that you want notifications for objects in a folder in your bucket.



来源:https://stackoverflow.com/questions/49025377/cloud-pub-sub-notifications-for-cloud-storage-on-sub-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!