将API JSON响应直接保存到Azure Blob存储JSON文件

jei2mxaa  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(173)

我正在Azure HTTP函数中直接调用第三方API。我希望将json响应保存到Azure Blob存储容器内的文件中。当我尝试调试Azure函数时,我生成的以下代码(基于Microsoft文档)挂起。当命中Azure函数URL终结点时,上述进程挂起并且永远无法完成任务。我的代码是否缺少某些内容?

import os
import logging
import requests
import azure.functions as func
from azure.storage.blob import BlobServiceClient,BlobClient
import json

def main(req: func.HttpRequest) -> func.HttpResponse:
    connection_string = os.getenv([Azure Blob Storage Connection String])
    file_name = 'SurveySchema.json'
    blob = BlobClient.from_connection_string(conn_str=connection_string, container_name=[container name], blob_name=[name of json file])
    request = requests.get('[The API endpoint that returns json response body]')
    try:
        logging.info(request.text)
        blob.set(str(request.text))
    except ValueError as err:
        logging.info("Error getting data from endpoint, %s", err)

    return func.HttpResponse('Request processed successfully.', status_code=200)
5n0oy7gb

5n0oy7gb1#

这是一个简单的修复!我不得不使用“upload_blob”方法从Blob客户端库

import os
import logging
import requests
import azure.functions as func
from azure.storage.blob import BlobServiceClient,BlobClient
import json

def main(req: func.HttpRequest) -> func.HttpResponse:
    connection_string = os.getenv([Azure Blob Storage Connection String])
    file_name = 'SurveySchema.json'
    blob = BlobClient.from_connection_string(conn_str=connection_string, container_name=[container name], blob_name=[name of json file])
    request = requests.get('[The API endpoint that returns json response body]')
    try:
        logging.info(request.text)
        **blob.upload_blob(str(request.text))**
    except ValueError as err:
        logging.info("Error getting data from endpoint, %s", err)

    return func.HttpResponse('Request processed successfully.', status_code=200)

相关问题