【问题标题】:How do I receive multipart form data in Azure function?如何在 Azure 函数中接收多部分表单数据?
【发布时间】:2020-05-14 06:21:01
【问题描述】:

我想在 Azure Function 中接收包含图像和文本的多部分数据。

我可以在 C# 和 nodeNode link 中看到很多示例,但在 python 中找不到任何内容。

在官方文档中也找不到任何内容Docs

【问题讨论】:

  • 你能做到吗?
  • 经过短暂的研究发现:github.com/Azure/azure-functions-host/issues/…
  • @Sidonai 不,我没有找到发送多部分表单数据的方法。所以,我将我的图像作为 json 中的 base64 编码字符串发送。

标签: python azure-functions


【解决方案1】:

我已经尝试了下面的代码,它对我有用:

import logging
import json
import io
from PIL import Image
from azure.storage.blob import BlobServiceClient,BlobClient
import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.form['name']
    interests =json.dumps(req.form["interests"], indent=2)
    imagefile = req.files["file"]
    filename = imagefile.filename
    filestream = imagefile.stream
    filestream.seek(0)

    blob = BlobClient.from_connection_string(conn_str= "<your connection string>", container_name="<container name>", blob_name="<blob name>")

    blob.upload_blob(filestream.read(), blob_type="BlockBlob")

    if name:
        return func.HttpResponse(f"Hello, {name}. Created profile with your interests : {interests}. Uploaded your profile image file :{filename} to the blob. This function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

但是,在这种情况下,您也可以使用 PIL 从图像中获取字节。然后代码看起来像:


import logging
import json
import io
from PIL import Image
from azure.storage.blob import BlobServiceClient,BlobClient
import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.form['name']
    interests =json.dumps(req.form["interests"], indent=2)
    filename = req.files["file"].filename
    image = Image.open(req.files["file"])
    imgByteIO = io.BytesIO()
    image.save(imgByteIO, format=image.format)
    imgByteArr = imgByteIO.getvalue()

    blob = BlobClient.from_connection_string(conn_str= "<your connection string>", container_name="<container name>", blob_name="<blob name>")
    blob.upload_blob(imgByteArr)

    if name:
        return func.HttpResponse(f"Hello, {name}. Created profile with your interests : {interests}. Uploaded your profile image file :{filename} to the blob. This function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )


这是邮递员的要求:

【讨论】:

  • 不清楚:为什么你从 req.form 得到名字和兴趣,而你从 req.files 得到文件?是不是笔误?
猜你喜欢
  • 2020-03-12
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
  • 1970-01-01
相关资源
最近更新 更多