You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Functions输出JSON时自定义Blob存储文件名问题求助

解决Azure Functions输出Blob时自定义文件名的问题

问题背景

创建一个HTTP触发的Azure Functions,从randomuser.me获取用户数据,生成包含随机城市、ID的JSON文件,并以随机ID为文件名输出到Blob存储的bronze容器中。运行时遇到两个错误:

  1. 调用outputBlob.set(data_str, file_name=f"{random_id}.json")时提示TypeError: set() got an unexpected keyword argument 'file_name'
  2. 将function.json中Blob路径改为bronze/{random_id}.json时提示No value for named parameter 'random_id'

错误原因

  • set()方法不支持file_name参数:Python的Blob输出绑定func.Out[str]的set()方法仅接收要写入的内容,没有file_name这个可选参数。
  • 绑定路径占位符无法直接引用局部变量:function.json中的路径占位符(如{random_id})只能识别触发上下文自带的变量(如HTTP查询参数),或者通过context.binding_data显式传递的变量,无法直接读取函数内的局部变量或全局变量。

解决方案

方案一:使用动态输出绑定(基于原生绑定)

通过修改绑定配置和上下文传递变量来实现自定义文件名:

  1. 更新function.json:将Blob路径改为带占位符的动态路径
{
  "bindings": [
    {
        "authLevel": "anonymous",
        "type": "httpTrigger",
        "direction": "in",
        "name": "req",
        "methods": ["get","post"]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
    {
      "name": "outputBlob",
      "direction": "out",
      "type": "blob",
      "path": "bronze/{randId}.json",
      "connection": "storageaccountstevendas_STORAGE"
    }
  ]
}
  1. 修改__init__.py:添加context参数,将随机ID传入绑定上下文后写入Blob
import json
import random
import requests
import azure.functions as func

cities = ["London", "Bath", "York", "Canterbury", "Oxford", "Durham", "Salisbury", "Liverpool", "Brighton", "Bristol", "Cambridge", "Manchester", "Nottingham", "Dover", "Birmingham", "Hastings"]

def main(req: func.HttpRequest, outputBlob: func.Out[str], context: func.Context) -> func.HttpResponse:
    url = "https://randomuser.me/api/"
    response = requests.get(url)
    data = response.json()
    
    first_name = data['results'][0]['name']['first']
    last_name = data['results'][0]['name']['last']
    dob = data['results'][0]['dob']['date']
    email = data['results'][0]['email']
    
    random_name = f"{first_name} {last_name}"
    random_city = random.choice(cities)
    random_id = random.randint(1, 10000)
    
    output_data = {
        "name": random_name,
        "dob": dob,
        "email": email,
        "city": random_city,
        "id": random_id
    }
    data_str = json.dumps(output_data)
    
    # 将随机ID传入绑定上下文,匹配function.json中的{randId}占位符
    context.binding_data['randId'] = random_id
    outputBlob.set(data_str)

    return func.HttpResponse(data_str, status_code=201)

方案二:使用Azure Storage SDK直接上传(更灵活)

绕过原生绑定限制,直接调用Storage SDK实现自定义文件名上传:

  1. 添加依赖:在requirements.txt中加入azure-storage-blob,确保部署时安装该包
azure-functions
requests
azure-storage-blob
  1. 修改__init__.py:移除outputBlob绑定,使用SDK上传Blob
import json
import random
import requests
import azure.functions as func
from azure.storage.blob import BlobServiceClient
import os

cities = ["London", "Bath", "York", "Canterbury", "Oxford", "Durham", "Salisbury", "Liverpool", "Brighton", "Bristol", "Cambridge", "Manchester", "Nottingham", "Dover", "Birmingham", "Hastings"]

def main(req: func.HttpRequest) -> func.HttpResponse:
    url = "https://randomuser.me/api/"
    response = requests.get(url)
    data = response.json()
    
    first_name = data['results'][0]['name']['first']
    last_name = data['results'][0]['name']['last']
    dob = data['results'][0]['dob']['date']
    email = data['results'][0]['email']
    
    random_name = f"{first_name} {last_name}"
    random_city = random.choice(cities)
    random_id = random.randint(1, 10000)
    
    output_data = {
        "name": random_name,
        "dob": dob,
        "email": email,
        "city": random_city,
        "id": random_id
    }
    data_str = json.dumps(output_data)
    
    # 通过Storage SDK上传Blob
    connection_string = os.environ["storageaccountstevendas_STORAGE"]
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)
    blob_client = blob_service_client.get_blob_client(container="bronze", blob=f"{random_id}.json")
    blob_client.upload_blob(data_str, overwrite=True)

    return func.HttpResponse(data_str, status_code=201)
  1. 更新function.json:移除outputBlob的绑定配置
{
  "bindings": [
    {
        "authLevel": "anonymous",
        "type": "httpTrigger",
        "direction": "in",
        "name": "req",
        "methods": ["get","post"]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

内容的提问来源于stack exchange,提问作者steviedas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 02:21:02