Home > Net >  Azure function - Setting CloudBlockBlob metadata on a new blob
Azure function - Setting CloudBlockBlob metadata on a new blob

Time:05-27

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:

        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.

CodePudding user response:

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.

  • Related