The way to do this is to create a Cloud Bub / Sub for new objects and configure the GCS bucket to publish messages in this section when creating new objects.
First create a PHOTOBUCKET bucket:
$ gsutil mb gs:
Now be sure to activate the Cloud Pub / Sub API .
Cloud Pub/Sub GCS gsutil:
$ gsutil notification create \
-t uploadedphotos -f json \
-e OBJECT_FINALIZE gs://PHOTOBUCKET
-t Pub/Sub. , gsutil .
-e , OBJECT_FINALIZE ( ). .
-f , , API JSON.
, gsutil, gcloud gsutil update, gsutil.
, . Pub/Sub:
$gcloud beta pubsub subscriptions processphotos --topic = uploadedphotos
. Python. :
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):
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
, :
https://cloud.google.com/storage/docs/reporting-changes
https://cloud.google.com/storage/docs/pubsub-notifications