当对象上传到我的 GCS 存储桶时,我如何收到通知? [英] How do I get notified when an object is uploaded to my GCS bucket?

查看:18
本文介绍了当对象上传到我的 GCS 存储桶时,我如何收到通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用可以定期将照片上传到 GCS 存储桶.上传这些照片后,我需要添加缩略图并进行一些分析.如何为存储分区设置通知?

I have an app that uploads photos regularly to a GCS bucket. When those photos are uploaded, I need to add thumbnails and do some analysis. How do I set up notifications for the bucket?

推荐答案

这样做的方法是为新对象创建一个 Cloud Pub/Sub 主题,并配置您的 GCS 存储桶以在新对象出现时向该主题发布消息已创建.

The way to do this is to create a Cloud Pub/Sub topic for new objects and to configure your GCS bucket to publish messages to that topic when new objects are created.

首先,让我们创建一个存储桶 PHOTOBUCKET:

First, let's create a bucket PHOTOBUCKET:

$ gsutil mb gs://PHOTOBUCKET

现在,请确保您已激活云发布/订阅 API.

接下来,让我们创建一个 Cloud Pub/Sub 主题并使用 gsutil 将其连接到我们的 GCS 存储桶:

Next, let's create a Cloud Pub/Sub topic and wire it to our GCS bucket with gsutil:

$ gsutil notification create 
    -t uploadedphotos -f json 
    -e OBJECT_FINALIZE gs://PHOTOBUCKET

-t 指定 Pub/Sub 主题.如果该主题尚不存在,gsutil 将为您创建它.

The -t specifies the Pub/Sub topic. If the topic doesn't already exist, gsutil will create it for you.

-e 指定您只对 OBJECT_FINALIZE 消息(正在创建的对象)感兴趣.否则,您将收到主题中的各种消息.

The -e specifies that you're only interested in OBJECT_FINALIZE messages (objects being created). Otherwise you'll get every kind of message in your topic.

-f 指定您希望消息的有效负载为 JSON API 的对象元数据.

The -f specifies that you want the payload of the messages to be the object metadata for the JSON API.

请注意,这需要最新版本的 gsutil,因此请务必更新到最新版本的 gcloud,如果您使用独立的 gsutil,请运行 gsutil update.

Note that this requires a recent version of gsutil, so be sure to update to the latest version of gcloud, or run gsutil update if you use a standalone gsutil.

现在我们已经配置并发送通知,但我们希望看到它们.让我们创建一个 Pub/Sub 订阅:

Now we have notifications configured and pumping, but we'll want to see them. Let's create a Pub/Sub subscription:

$ gcloud beta pubsub 订阅创建 processphotos --topic=uploadedphotos

$ gcloud beta pubsub subscriptions create processphotos --topic=uploadedphotos

现在我们只需要阅读这些消息.这是一个 Python 示例就是这样做.以下是相关位:

Now we just need to read these messages. Here's a Python example of doing just that. Here are the relevant bits:

def poll_notifications(subscription_id):
    client = pubsub.Client()
    subscription = pubsub.subscription.Subscription(
        subscription_id, client=client)
    while True:
        pulled = subscription.pull(max_messages=100)
        for ack_id, message in pulled:
            print('Received message {0}:
{1}'.format(
                message.message_id, summarize(message)))
            subscription.acknowledge([ack_id])

def summarize(message):
    # [START parse_message]
    data = message.data
    attributes = message.attributes

    event_type = attributes['eventType']
    bucket_id = attributes['bucketId']
    object_id = attributes['objectId']
    return "A user uploaded %s, we should do something here." % object_id

这里有更多关于这个系统如何工作的阅读:

Here is some more reading on how this system works:

https://cloud.google.com/storage/docs/reporting-changeshttps://cloud.google.com/storage/docs/pubsub-notifications

这篇关于当对象上传到我的 GCS 存储桶时,我如何收到通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆