我想在 gcp 存储桶更新时收到 angular 的通知。有人可以帮我吗?
答案 0 :(得分:1)
这个想法是:每当更新存储桶时,都会向您的 Python Web 应用程序发送一个 http post 请求。你只需要在 python 中处理这个 post 请求。
例如
import http.server
import socketserver
from http import HTTPStatus
class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(HTTPStatus.OK)
self.end_headers()
self.wfile.write(b'Hello world')
def post(self):
if 'X-Goog-Resource-State' in self.request.headers:
resource_state = self.request.headers['X-Goog-Resource-State']
if resource_state == 'sync':
logging.info('Sync message received.')
else:
an_object = json.loads(self.request.body)
bucket = an_object['bucket']
object_name = an_object['name']
logging.info('%s/%s %s', bucket, object_name, resource_state)
else:
logging.info("Other post.")
httpd = socketserver.TCPServer(('', 8093), Handler)
httpd.serve_forever()
上面的网络服务器启动后,在 Cloud Console 中将通知重定向到您的网址
gsutil notification watchbucket yourULR gs://BucketName
def post(self) 是从 webapp2 示例中复制过来的
https://cloud.google.com/storage/docs/object-change-notification
Web 服务器代码从这里复制
https://gist.github.com/davidbgk/b10113c3779b8388e96e6d0c44e03a74