Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 610
vargonian Avatar asked Aug 16 '18 18:08

vargonian


People also ask

What is metadata in Azure Blob Storage?

User-defined metadata: User-defined metadata consists of one or more name-value pairs that you specify for a Blob storage resource. You can use metadata to store additional values with the resource. Metadata values are for your own purposes only, and don't affect how the resource behaves.

How do I update blob metadata?

You can therefore either directly overwrite with a new value, or use GetMetadata(Async) first to retrieve the existing value, edit it, and then write it back using SetMetadata(Async). You pass in an IDictionary to SetMetadata with the keys and values you want to set.

Which tool can be used to upload data to Azure Storage account?

You can upload files and directories to Blob storage by using the AzCopy v10 command-line utility.


1 Answers

As juunas said, you could just set the metadata on the 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: enter image description here

like image 96
Joey Cai Avatar answered Sep 16 '22 17:09

Joey Cai