Python 创建脚本以检索上载到Google云存储桶的图像
我已经设置了一个云存储bucket、一个云发布/子主题和一个订阅,以便在文件上载到bucket时创建一个通知Python 创建脚本以检索上载到Google云存储桶的图像,python,google-cloud-platform,Python,Google Cloud Platform,我已经设置了一个云存储bucket、一个云发布/子主题和一个订阅,以便在文件上载到bucket时创建一个通知 我想在服务器上创建一个脚本,当一个新图像上传到bucket时订阅该消息,并将该图像本地下载到服务器。我不知道该怎么做——有人能给我提供正确方向的资源或指针吗?答案是几个方面的组合。我必须做的事情的要点可以在以下方法中找到: from google.cloud import pubsub_v1, storage def retrieve_image(message): attr
我想在服务器上创建一个脚本,当一个新图像上传到bucket时订阅该消息,并将该图像本地下载到服务器。我不知道该怎么做——有人能给我提供正确方向的资源或指针吗?答案是几个方面的组合。我必须做的事情的要点可以在以下方法中找到:
from google.cloud import pubsub_v1, storage
def retrieve_image(message):
attributes = message.attributes
bucket_id = attributes['bucketId']
source_blob_name = attributes['objectId']
destination_file_name = 'images/' + source_blob_name
download_blob(bucket_id, source_blob_name, destination_file_name)
def download_blob(bucket_name, source_blob_name, destination_file_name):
"""Downloads a blob from the bucket."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)
print('Blob {} downloaded to {}.'.format(
source_blob_name,
destination_file_name))
def poll_notifications(project, subscription_name):
"""Polls a Cloud Pub/Sub subscription for new GCS events for display."""
# [BEGIN poll_notifications]
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
project, subscription_name)
def callback(message):
print('Received message:\n{}'.format(summarize(message)))
retrieve_image(message)
# run_openpose(message)
message.ack()
subscriber.subscribe(subscription_path, callback=callback)
# The subscriber is non-blocking, so we must keep the main thread from
# exiting to allow it to process messages in the background.
print('Listening for messages on {}'.format(subscription_path))
while True:
time.sleep(60)
# [END poll_notifications]
答案是几件事的结合。我必须做的事情的要点可以在以下方法中找到:
from google.cloud import pubsub_v1, storage
def retrieve_image(message):
attributes = message.attributes
bucket_id = attributes['bucketId']
source_blob_name = attributes['objectId']
destination_file_name = 'images/' + source_blob_name
download_blob(bucket_id, source_blob_name, destination_file_name)
def download_blob(bucket_name, source_blob_name, destination_file_name):
"""Downloads a blob from the bucket."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)
print('Blob {} downloaded to {}.'.format(
source_blob_name,
destination_file_name))
def poll_notifications(project, subscription_name):
"""Polls a Cloud Pub/Sub subscription for new GCS events for display."""
# [BEGIN poll_notifications]
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
project, subscription_name)
def callback(message):
print('Received message:\n{}'.format(summarize(message)))
retrieve_image(message)
# run_openpose(message)
message.ack()
subscriber.subscribe(subscription_path, callback=callback)
# The subscriber is non-blocking, so we must keep the main thread from
# exiting to allow it to process messages in the background.
print('Listening for messages on {}'.format(subscription_path))
while True:
time.sleep(60)
# [END poll_notifications]
请向我们展示您的脚本到目前为止的样子,以便我们可以看到我们可以如何帮助您。我不确定从哪里开始。我已经使用
gsutil mb gs://image\u upload\u bucket
创建了一个新bucket,并且gsutil notification create-t upload\u images-f json-e OBJECT\u FINALIZE gs gs://image\u upload\u bucket
将发布/订阅主题写入bucket,我还使用gcloud beta pubsub sub sub subscriptions create analyze\u image创建了一个订阅--topic=上传图片以查看通知。我想做的是检索上传到bucket的图像,并在本地下载,这可能吗?获取检索图像的命令是gsutil cp gs://bucketname/imagefilename。请指定文件夹名而不是“.”我解决了我的问题。将在下面发布。是Inder的link+的组合。请向我们展示您的脚本到目前为止的外观,以便我们可以看到我们可以如何帮助您。我不确定从哪里开始。我已经使用gsutil mb gs://image\u upload\u bucket
创建了一个新bucket,并且gsutil notification create-t upload\u images-f json-e OBJECT\u FINALIZE gs gs://image\u upload\u bucket
将发布/订阅主题写入bucket,我还使用gcloud beta pubsub sub sub subscriptions create analyze\u image创建了一个订阅--topic=上传图片以查看通知。我想做的是检索上传到bucket的图像,并在本地下载,这可能吗?获取检索图像的命令是gsutil cp gs://bucketname/imagefilename。请指定文件夹名而不是“.”我解决了我的问题。将在下面发布。是Inder链接的组合+