python - Azure 函数 blob 存储文件名

标签 python azure azure-functions azure-blob-storage

将 Azure 函数与 Blob 存储输出绑定(bind)结合使用时,如何获取创建的 Blob 的名称、路径或 URL?我想在保存后将其保存到数据库中。

我使用 Python,但任何示例都可以:

blob = open(os.environ['outputBlob'], 'wb')
blob.write(attachment.get_payload(decode=True))

print blob.name # this is not the correct name, but actually the temp file name I think

blob.close()

最佳答案

对于 C# : 正如https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob#input中所讨论的您可以使用以下类型的 blob 绑定(bind)

enter image description here

下面是绑定(bind)json文件和代码的示例。我在http返回中返回outpuBlob.Uri以获取Blob的主要位置路径。

绑定(bind):-

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "blob",
      "name": "outputBlob",
      "path": "outcontainer/{rand-guid}",
      "connection": "AzureWebJobsDashboard",
      "direction": "inout"
    }
  ],
  "disabled": false
}

函数代码(C#):

#r "Microsoft.WindowsAzure.Storage"

using System.Net;
using Microsoft.WindowsAzure.Storage.Blob;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log,CloudBlockBlob outputBlob)
{
    log.Info("C# HTTP trigger function processed a request.");

    // parse query parameter
    string name = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
        .Value;

    if (name == null)
    {
        // Get request body
        dynamic data = await req.Content.ReadAsAsync<object>();
        name = data?.name;
    }



    return name == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, "Hello " + outputBlob.Uri);
}

关于python - Azure 函数 blob 存储文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49106697/

相关文章:

python - 我无法使 event.key 语句起作用

azure - 在存储帐户中授予应用程序角色 "storage blob data contributor"

azure - 在 Azure DevOps Powershell 管道任务中获取自己的服务主体名称

c# - 当 URL 包含缩略图时,Blob 触发器绑定(bind)应排除

python - 在 n 个不相等的列表中找到最大值

python - 明显的 rpy2 用法会引发不支持的操作数类型异常

python - 如何在 session 中运行多个图形 - Tensorflow API

node.js - Azure 移动服务上的 NodeMailer 无法正常工作

azure - 获取上次运行 Azure 函数的时间

azure - 使用 Azure 函数和 API 连接器 Azure B2C 生成显示名称