Azure function with blobtrigger - update metadata

不问归期 提交于 2020-06-01 07:08:50

问题


I am looking for a sample or IP for updating a blob's metadata when it is uploaded. All help is appreciated.

I have following function:

public static void Run([BlobTrigger("types/{name}", Connection = "StorageConnection")]Stream myBlob, string name, ILogger log)
{
}

回答1:


You can use the trigger to get the ICloudBlob instead of the stream.
Check the official documentation on blob triggers for Azure Functions.

Basically, your code will look something like this:

public static void Run(
    [BlobTrigger("types/{name}", Connection = "StorageConnection")] ICloudBlob myBlob, 
    string name, 
    ILogger log)
{
    if (blobTrigger.Metadata.ContainsKey("MyKey"))
        return;

    blobTrigger.Metadata["MyKey"] = "MyValue";
    await blobTrigger.SetMetadataAsync();
}

There is an issue though. After you update your metadata, you are basically uploading the blob again, which in turn, will trigger your function again.

I've added a simple check to see if my metadata key was already added to avoid an infinite loop.
Of course, you will probably have your own way of knowing if you are the one who just updated the metadata or not. Worst case scenario, you'll have to use your own flag to indicate that the upload occurred from your function.

Hope it helps. :)



来源:https://stackoverflow.com/questions/57810745/azure-function-with-blobtrigger-update-metadata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!