结果:失败异常:TypeError:参数应该是类似字节的对象或ASCII字符串,而不是'dict' [英] Result: Failure Exception: TypeError: argument should be a bytes-like object or ASCII string, not 'dict'

查看:103
本文介绍了结果:失败异常:TypeError:参数应该是类似字节的对象或ASCII字符串,而不是'dict'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做这个作业时遇到了问题,我要发送一个带有编码图像base64的发布请求作为postman中的json对象.我应该对json主体进行解码,并将其另存为azure blob存储中的图像.我已经成功地将blob创建为txt,但是这次我没有太多的运气.任何帮助将不胜感激

I'm having a problem with this homework where I'm sending a post request with an encoded image base64 as a json object in postman. I'm supposed to decode the json body and save it as an image on azure blob storage. I was successfully creating the blobs as txt but I haven't got much luck this time. Any help will be much appreciated

结果:失败异常:TypeError:参数应为类似字节的对象或ASCII字符串,而不是'dict'

Result: Failure Exception: TypeError: argument should be a bytes-like object or ASCII string, not 'dict'

推荐答案

如果要使用Azure函数将图像文件上传到Azure blob存储,可以尝试使用 form 发送图像到Azure功能.例如

If you want to upload image file to Azure blob storage with Azure function, you can try to use the form to send your image to Azure function. For example

  1. local.settings.json
  2. 中添加存储连接字符串
  1. add Storage connection string in local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "ConnectionString" : "",
    "ContainerName": ""
  }
}

  1. 代码

import logging
import os
import azure.functions as func
from azure.storage.blob import BlobServiceClient, BlobClient
def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    try:
        file=  req.files.get('the key value in your form')
        logging.info(file.filename)
        
        connect_str=os.environ["ConnectionString"]
        container=os.environ["ContainerName"]
        
        blob_service_client = BlobServiceClient.from_connection_string(connect_str)
        blob_client =blob_service_client.get_blob_client(container=container,blob=file.filename)
        blob_client.upload_blob(file)
    except Exception as ex:
        logging.info(ex.args)
       
    return func.HttpResponse("ok")
    

  1. 邮递员测试


更新

根据我的测试,如果我们使用 base64.b64decode()进行解码,我们将获得bytes对象.因此,我们需要使用 create_blob_from_bytes 进行上传.例如


Update

According to my test, if we use base64.b64decode() to decode, we will get bytes object. So we need to use create_blob_from_bytes to upload. For example

我的代码

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    # get image base64 string
    file=req.get_json()
    image=file['image'].replace(' ', '+')
    #decode base64 string
    data=base64.b64decode(image)
    logging.info(type(data))
    #upload
    block_blob_service = BlockBlobService(account_name='blobstorage0516', account_key='')
    container_name='test'
    blob_name='test.jpeg'
    block_blob_service.create_blob_from_bytes(container_name, blob_name, data)

  
    return func.HttpResponse(f"OK!")

这篇关于结果:失败异常:TypeError:参数应该是类似字节的对象或ASCII字符串,而不是'dict'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆