0

Important: There's a similar question which discusses using Microsoft.Azure.Storage.Blob which is deprecated. I am looking for a solution which does not rely on deprecated packages.

I am creating a blob in an Azure Storage Account using Azure.Storage.Blobs, and I would like to set metadata on that blob at the time of creation.

It's important that this is done at the same time; blob creation triggers an EventGrid event which in turn calls a function to perform an operation using that blob and its metadata. It would be problematic if the function is triggered before metadata is set.

Is there a way to do this using Azure.Storage.Blobs or similar? (I do not want to use https://www.nuget.org/packages/Microsoft.Azure.Storage.Blob/).

Aligma
  • 562
  • 5
  • 29
  • Please edit your question and include the code you’ve written. It should be possible to set metadata which creating the blob. – Gaurav Mantri Mar 23 '23 at 03:06
  • Please note that the [other question](https://stackoverflow.com/questions/75818384/is-there-a-way-to-set-metadata-on-a-blob-at-the-time-of-creation-using-current-a) you asked (that's now been closed and deleted) had a [valid duplicate](https://stackoverflow.com/questions/51883367/can-you-set-metadata-on-an-azure-cloudblockblob-at-the-same-time-as-uploading-it). Even if a package is deprecated, that doesn't mean that underlying API support is deprecated. Please show your work, and ask a specific question. – David Makogon Mar 23 '23 at 03:20
  • @GauravMantri Currently using `await blobClient.UploadAsync(content);` and await `blobClient.SetMetadataAsync(metadata)` Can you please provide more information on your statement "It should be possible"? Does this mean there is an API that can achieve this? If so, I would mark that response as the answer. – Aligma Mar 23 '23 at 05:42
  • @DavidMakogon Thank you, I will make this question more specific to indicate that I am looking for a solution using the new APIs, and that I do not want to use CloudBlockBlob, rather than implying that I am open to using it depending on the level of support offered. – Aligma Mar 23 '23 at 05:43

1 Answers1

1

Assuming your content is of type Stream, you can use UploadAsync(Stream, BlobUploadOptions, CancellationToken) method. You can set the metadata in Metadata property in BlobUploadOptions.

Your code could be something like:

var metadata = new Dictionary<string, string>
{
    {"key1", "value1"},
    {"key2", "value2"}
};

var blobUploadOptions = new BlobUploadOptions()
{
    Metadata = metadata
};

await blobClient.UploadAsync(content, blobUploadOptions);

Here's a more detailed example of the same: https://github.com/Azure/azure-sdk-for-net/blob/47ea075bca473fe6e9928ff9893fbaa8a552f3a5/sdk/storage/Azure.Storage.Blobs/samples/Sample03_Migrations.cs#L630.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • We'd missed this overload and couldn't find other examples - it works as described. Thanks Gaurav. – Aligma Mar 23 '23 at 22:16