如何使用Python从Google Cloud Function上的POST请求中接收图像



我正在努力重新组装通过POST请求发送到GCP云函数的映像。

我在这里看到了关于如何使用POST请求打包文件的建议。

我希望函数从字节中重建图像以进行进一步处理,每次我发送请求时,我都会收到"失败"。任何帮助都将不胜感激!

### client_side.py
import requests
url = 'https://region-project.cloudfunctions.net/function' # Generic GCP functions URL
files = {'file': ('my_image.jpg', open('my_image.jpg', 'rb').read(), 'application/octet-stream')}
r = requests.post(url, files=files)
### gcp_function.py
from io import BytesIO
def handler(request):
try:
incoming = request.files.get('file')
bytes = BytesIO(incoming)
image = open_image(bytes)
message = 'Success'
except:
message = 'Failure'
return message

排序。

需要读取方法将FileStorage对象转换为字节。

### gcp_function.py
from io import BytesIO
import logging
def handler(request):
try:
incoming = request.files['file'].read()
bytes = BytesIO(incoming)
image = open_image(bytes)
message = 'Success'
except Exception as e:
message = 'Failure'
logging.critical(str(e))
return message