Azure功能blob存储文件名

时间:2018-03-05 09:01:48

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

当使用具有blob存储输出绑定的azure函数时,如何获取创建的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()

2 个答案:

答案 0 :(得分:0)

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

enter image description here

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

绑定: -

{
  "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);
}

答案 1 :(得分:0)

查看Python脚本的处理空间中的环境变量。你应该有一个包含blob绑定路径的文件。