Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Azure function – Setting CloudBlockBlob metadata on a new blob

I’ve created an Azure function in which I’d like to retrieve information from an input blob and set metadata on a newly created output blob. I don’t think I’m setting the metadata correctly however, as I am unable to retrieve it in another part of my application.

Azure function code:

    public static async Task Run(
        [BlobTrigger("inputcontainer/{name}")] Stream inputBlob,
        [Blob("outputcontainer/{name}.jpeg", FileAccess.Write)] CloudBlockBlob outputBlob,
        string name,
        ExecutionContext executionContext,
        ILogger log)
    {

        // code that processes input and extracts some info - including senderId
        ....
        ....

        // task to generate a thumbnail from a video, returns a stream representing thumbnail
        var thumbnailTask = GenerateThumbnail(inputBlob);
        Stream ms = thumbnailTask.Result;
        ms.Position = 0;
        outputBlob.Metadata["SenderId"] = senderId;
        outputBlob.UploadFromStream(ms);
    }

Here is how I’m reading the CloudBlockBlob elsewhere in my application. At this point I can read the stream I uploaded fine, but the blob.Metadata is empty:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        BlobResultSegment blobResultSegment = await outputContainer.ListBlobsSegmentedAsync(null);
        foreach (IListBlobItem item in blobResultSegment.Results)
        {
            CloudBlockBlob blob = (CloudBlockBlob)item;
            // count = 0
            Console.WriteLine($"Blob info {blob.Metadata.Count}");

I can’t figure out why the Metadata is not being set on the new blob. Any help much appreciated. Thanks.

>Solution :

There’s nothing wrong with your code for setting the metadata, it is being set properly. The issue is with the code for fetching the list of blobs. When you list the blobs, by default the metadata for the blob is not fetched.

Please use ListBlobsSegmentedAsync(String, Boolean, BlobListingDetails, Nullable<Int32>, BlobContinuationToken, BlobRequestOptions, OperationContext, CancellationToken) override for listing blobs and provide BlobListingDetails. Metadata for BlobListingDetails parameter. Once you do that, you should be able see metadata for the blob.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading