Can you set metadata on an Azure CloudBlockBlob at the same time as uploading it?

落爺英雄遲暮 提交于 2019-12-24 01:16:24

问题


I have a situation in which I currently upload a CloudBlockBlob using CloudBlockBlob.UploadFromStreamAsync, and then immediately afterward I set a bunch of user metadata on it.

The issue is: I have an Event Grid event which is triggered when the blob is uploaded, but the event handler requires the metadata. Long story short, it's a race condition wherein I have to "hope" that the metadata has been set before my event handler responds to the block blob upload.

Is there some way to both upload the block blob (file) and set its metadata in a single operation?


回答1:


As juunas said, you could just set the metadata on hte blob before calling Upload. I make a little demo with .Net Console you could refer to.

public static void Main(string[] args)
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = cloudBlobClient.GetContainerReference("container");
    CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference("hello.txt");
    MemoryStream msWrite = new MemoryStream(Encoding.UTF8.GetBytes("aaaaaa"));
    msWrite.Position = 0;
    cloudBlockBlob.Metadata["category"] = "guidance";
    using (msWrite)
    {
        cloudBlockBlob.UploadFromStream(msWrite);
    }
 }

The output in blob on portal:



来源:https://stackoverflow.com/questions/51883367/can-you-set-metadata-on-an-azure-cloudblockblob-at-the-same-time-as-uploading-it

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