簡體   English   中英

將 API JSON 響應直接保存到 Azure Blob 存儲 json 文件

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

我直接在 Azure HTTP 函數中調用第三方 API。 我想將 json 響應保存到 Azure Blob 存儲容器中的文件。 當我嘗試調試 Azure 函數時,我構建的以下代碼(基於微軟文檔)掛起。 當訪問 Azure Function 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)

這很容易解決! 我不得不使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM