将 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)
下面是绑定(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/