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

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

问题描述

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

解决方案

执行此操作的方法是创建Cloud Pub /新对象,并配置您的GCS存储桶以在创建新对象时将消息发布到该主题。

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

  $ gsutil mb gs:// PHOTOBUCKET 

现在,请确保激活了Cloud Pub / Sub API
$ b 接下来,让我们创建一个Cloud Pub / Sub主题并使用 gsutil将其连接到我们的GCS存储桶:

  $ gsutil通知创建\ 
-t uploadedphotos -f json \
-e OBJECT_FINALIZE gs:// PHOTOBUCKET

-t 指定发布/订阅主题。如果该主题尚不存在, gsutil 会为您创建。



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



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



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



<现在我们已经配置了通知和抽取,但我们希望看到它们。让我们创建一个Pub / Sub订阅:

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

现在我们只需要阅读这些消息。 下面是一个Python示例这样做。这里有一些相关的部分:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ def $ b订阅= pubsub.subscription.Subscription(
subscription_id,客户端=客户端)
,而True:
拉= subscription.pull(max_messages = 100)
为ack_id, :
print('Received message {0}:\\\
{1}'。format(
message.message_id,summarize(message)))
subscription.acknowledge([ack_id])

def总结(消息):
#[START parse_message]
data = message.data
attributes = message.attributes

event_type = attributes ['eventType']
bucket_id = attributes ['bucketId']
object_id = attributes ['objectId']
return用户上传%s,我们应该在这里做点什么。 %object_id

以下是关于这个系统如何工作的更多信息:



https://cloud.google.com/storage/docs/报告变更
https://cloud.google.com/ storage / docs / pubsub-notifications


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?

解决方案

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.

First, let's create a bucket PHOTOBUCKET:

$ gsutil mb gs://PHOTOBUCKET

Now, make sure you've activated the Cloud Pub/Sub API.

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

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

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.

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

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.

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

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

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}:\n{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-changes https://cloud.google.com/storage/docs/pubsub-notifications

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

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