gpt4 book ai didi

Python/GCP 健全性检查 : is this correct means of referencing image stored in GCP Storage in a POST call

转载 作者:太空宇宙 更新时间:2023-11-03 19:46:44 29 4
gpt4 key购买 nike

场景:存储在 GCP 存储桶中的图像文件需要通过 POST 发送到第三方 REST 端点

问题:这真的是最好的模式吗?有没有更有效、更简洁的方法?

我们有移动应用将图像上传到 GCP 存储桶。当图像上传的 Finalize 事件触发时,我们有一个 GCP 云函数 (Python 3),它通过获取上传图像的引用来对此使用react,将其下载到临时文件,然后使用该临时文件作为 POST 的图像源。这是我们当前的代码,它可以工作,但在我看来,多个 open 命令似乎很复杂。更具体地说:是否有更好的方法可以简单地从 GCP 存储获取图像 blob 并将其附加到 POST 调用,而无需先将其保存为本地文件,然后打开它以便将其附加到 POST?

def third_party_upload(data, context):
# get image from bucket
storage_client = storage.Client()
bucket = storage_client.bucket(data['bucket'])
image_blob = bucket.get_blob(data['name'])
download_path = '/tmp/{}.jpg'.format(str(uuid.uuid4())) #temp image file download location

# save GCP Storage blob as a temp file
with open(download_path, 'wb') as file_obj:
image_blob.download_to_file(file_obj)

# open temp file and send to 3rd-party via rest post call
with open(download_path, 'rb') as img:
files = {'image': (data['name'], img, 'multipart/form-data', {'Expires': '0'}) }

headers = {
'X-Auth-Token': api_key,
'Content-Type': 'image/jpg',
'Accept': 'application/json'
}

# make POST call
response = requests.post(third_party_endpoint, headers=headers, files=files)
print('POST response:', response)

更新:一些评论者提到签名 URL 是一种可能性,我同意它们是一个很好的选择。然而,我们仍然坚持将图像二进制文件作为 POST 正文包含在内的要求。在这种情况下,签名 URL 将不起作用。

最佳答案

HTTP 方法 POST 需要数据。您必须在 HTTP 请求中提供该数据。除了读取之外,没有任何神奇的方法可以获取 Cloud Storage 数据。该过程是从 Cloud Storage 读取数据,然后将该数据提供给 POST 请求。

关于Python/GCP 健全性检查 : is this correct means of referencing image stored in GCP Storage in a POST call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60082999/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com