[英]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.