简体   繁体   English

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

[英]Save API JSON Response directly to Azure Blob Storage json file

I am calling to a 3rd party API directly in an Azure HTTP Function.我直接在 Azure HTTP 函数中调用第三方 API。 I would like to save the json response to a file inside Azure Blob Storage container.我想将 json 响应保存到 Azure Blob 存储容器中的文件。 The below code I built (based on microsoft documentation ) hangs when I try debugging the Azure Function.当我尝试调试 Azure 函数时,我构建的以下代码(基于微软文档)挂起。 When hitting the Azure Function URL endpoint, the above process hangs and never finishes the tasks.当访问 Azure Function URL 端点时,上述进程挂起并且永远不会完成任务。 Is my code missing something?我的代码缺少什么吗?

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)

This was an easy fix!这很容易解决! I had to use "upload_blob" method from Blob Client library我不得不使用 Blob Client 库中的“upload_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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 excel 文件 stream 保存到 azure blob 存储 - save excel file stream to azure blob storage 如何使用 Azure 函数从 Blob 存储中读取 json 文件 Python - How to read json file from blob storage using Azure Functions Blob Trigger with Python How can I append JSON data to an existing JSON file stored in Azure blob storage through python? - How can I append JSON data to an existing JSON file stored in Azure blob storage through python? 将 json 保存到 Azure Data Lake Storage Gen 2 中的文件 - Save json to a file in Azure Data Lake Storage Gen 2 如何在 Python 中使用 Azure Functions 的 Azure Blob 存储绑定将 JSON 数据上传到 Azure 存储 blob - How to upload JSON data to Azure storage blob using Azure Blob storage bindings for Azure Functions in Python 使用SDK在Azure函数中将Azure Blob存储到JSON - Azure blob storage to JSON in azure function using SDK 如何使用Python将Azure Blob存储中的大型JSON文件拆分为每个记录的单个文件? - How can I split large JSON file in Azure Blob Storage into individual files for each record using Python? 将 API 响应(Json 格式)作为图像文件本地保存到系统 - Save the API response (Json format ) as image file locally to system 将文件保存到 Azure Blob 存储 - Saving a file into Azure Blob storage App Engine - 将数据存储中API的响应保存为文件(blob) - App Engine - Save response from an API in the data store as file (blob)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM