HUH函數(shù)
2022-07-19 15:26:14
場(chǎng)景:存儲(chǔ)在 GCP 存儲(chǔ)桶中的圖像文件需要通過(guò) POST 發(fā)送到第三方 REST 端點(diǎn)問(wèn)題:這真的是最好的模式嗎?有沒(méi)有更有效、更簡(jiǎn)潔的方法?我們有通過(guò)移動(dòng)應(yīng)用上傳到 GCP 存儲(chǔ)桶的圖像。當(dāng)圖像上傳的 finalize 事件觸發(fā)時(shí),我們有一個(gè) GCP 云函數(shù) (Python 3),它通過(guò)獲取上傳圖像的 ref 來(lái)對(duì)此做出反應(yīng),將其下載到臨時(shí)文件,然后使用該臨時(shí)文件作為 POST 的圖像源. open這是我們當(dāng)前的代碼,它可以工作,但在我看來(lái),這似乎與多個(gè)命令令人費(fèi)解。更具體地說(shuō):有沒(méi)有更好的方法來(lái)簡(jiǎn)單地從 GCP 存儲(chǔ)中獲取圖像 blob 并將其附加到 POST 調(diào)用,而無(wú)需先將其保存為本地文件然后打開(kāi)它以便將其附加到 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)更新:一些評(píng)論者提到簽名 URL 是一種可能性,我同意它們是一個(gè)很好的選擇。但是,我們堅(jiān)持要求將圖像二進(jìn)制文件包含為 POST 正文。在這種情況下,簽名 URL 將不起作用。
2 回答

紅糖糍粑
TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊
HTTP 方法 POST 需要數(shù)據(jù)。您必須在 HTTP 請(qǐng)求中提供該數(shù)據(jù)。除了讀取之外,沒(méi)有什么神奇的方法可以獲取 Cloud Storage 數(shù)據(jù)。該過(guò)程是從 Cloud Storage 讀取數(shù)據(jù),然后將該數(shù)據(jù)提供給 POST 請(qǐng)求。

狐的傳說(shuō)
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊
如果您能夠?qū)?URL 發(fā)送到第三方端點(diǎn)而不是實(shí)際的圖像內(nèi)容,則可以使用簽名 URL 提供對(duì)圖像的限時(shí)訪(fǎng)問(wèn),而無(wú)需提供對(duì)存儲(chǔ)桶的第三方訪(fǎng)問(wèn)權(quán)限或制作存儲(chǔ)桶上市。
更多信息在這里:https ://cloud.google.com/storage/docs/access-control/signed-urls
添加回答
舉報(bào)
0/150
提交
取消