【发布时间】:2020-10-19 02:41:10
【问题描述】:
在我的云功能中,我需要在云存储中获取文件并通过 HTTP POST 请求将文件发送到 API。我尝试了以下代码:
storage_client = storage.Client()
bucket = storage_client.bucket(BUCKET_NAME)
source_blob_name = "/compressed_data/file_to_send.7z"
blob = bucket.blob(source_blob_name)
url = UPLOADER_BACKEND_URL
files = {'upload_file': blob}
values = {'id': '1', 'ouid': OUID}
r = requests.post(url, files=files, data=values)
它给出了一个错误提示:
Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", ...
\ line 90, in encode_multipart_formdata body.write(data) TypeError: a bytes-like object is required, not 'Blob'
如果此代码要在实际 VM 上运行,则以下内容将起作用:
url = UPLOADER_BACKEND_URL
files = {'upload_file': open('/tmp/file_to_send.7z','rb')}
values = {'id': '1', 'name': 'John'}
r = requests.post(url, files=files, data=values)
所以问题是:在云函数中,如何从云存储中加载文件,使其具有与 python open(filename, 'rb') 函数相同的输出?
我知道我可以先blob.download_to_file() 然后open() 文件,但我想知道是否有更快的方法。
【问题讨论】:
标签: python file-io google-cloud-functions google-cloud-storage