问题
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