How to send custom data with Cloud Pub/Sub when GCS object is uploaded via a Signed URL

China☆狼群 提交于 2019-12-23 05:44:10

问题


I was able to set up Google Cloud Storage Cloud Pub/Sub notifications using:

gsutil notification create -t [TOPIC_NAME] -m my-key:my-value -f json gs://[BUCKET_NAME]

My App Engine servlet correctly gets a message every time an object is uploaded to GCS. I upload my object to GCS with a Signed URL.

However, I'm not sure how to set custom key-value pairs from my client when uploading an object with the Signed URL. The above gsutil command lets you set a key:value pair but it hard-codes it so that is not useful. In my client I want to set some key:value pair like user : some-user so then in my servlet I can do some extra App Engine stuff like write to a database.

I tried uploading some headers in the metadata tag as shown here but getting those headers from the HttpServletRequest in the servlet didn't seem to work.

Also, how would I sent the subscriptionUniqueToken as well, since there is no explanation on how to do that.

Note: using Java


回答1:


The notion of a unique token is no longer necessary in most cases. Object change notifications offered them because they worked by sending unauthenticated HTTPS calls to a configurable endpoint. If that endpoint were discovered, a malicious user could also send such calls. However, Cloud Pub/Sub notifications publish messages to a topic as a known service account, and so long as no malicious third party is also granted publishing permission to that topic, they cannot interfere. If you want, you can include a unique token as a second protection mechanism, but it's generally not necessary.

"Client tokens" are a feature specific to Object Change Notifications. The equivalent with Cloud Pub/Sub integration are "custom_attributes", user-specified properties of a notification config that are attached as extra attributes to each notification. You could add a "unique_token" attribute and attach a value, if you wish.

When using signed URLs, setting custom metadata is done with HTTP headers beginning x-goog-meta-. For example, x-goog-meta-stuff: Foo will create a custom attribute pair "stuff: Foo".



来源:https://stackoverflow.com/questions/43360536/how-to-send-custom-data-with-cloud-pub-sub-when-gcs-object-is-uploaded-via-a-sig

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