问题
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