Azure functions java: How to read blob tags as part of blob trigger?

121 Views Asked by At

I am using blob trigger to read and process blobs from Azure storage container. Currently, it works fine, I am able to receive the blob content and process it using the annotations as shown below. Now I need to know how to read the blob tags and metadata as part of the input?

enter image description here

1

There are 1 best solutions below

2
Vivek Vaibhav Shandilya On

I need to know how to read the blob tags and metadata as part of the input?

metadata and tags cannot be read from blob trigger or blob input binding. to get metadata and tag of the file you need to use BlobServiceClient.

This code worked for me. Function.java:

package com.function;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
import com.azure.storage.blob.*;
import com.azure.storage.blob.models.*;

public class Function {
    @FunctionName("BlobTriggerJava1")
    @StorageAccount("AzureWebJobsStorage")
    public void run(
        @BlobTrigger(name = "content", path = "test/{name}",dataType = "String") String content,  
        @BindingName("name") String name,
        final ExecutionContext context
    ) {
        String connectionString = System.getenv("AzureWebJobsStorage");
        String containerName = "test";
        String blobName = name;

        BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectionString).buildClient();

        BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);

        BlobClient blobClient = containerClient.getBlobClient(blobName);

        BlobProperties blobProperties = blobClient.getProperties();

        context.getLogger().info("The details of file "+name+" are:\n{\n\t Etag :"+blobProperties.getETag()+"\n\t metadata :"+blobProperties.getMetadata()+"\n}");
    }
}

Additional dependency in pom.xml:

        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-storage-blob</artifactId>
            <version>12.25.1</version>
        </dependency>

OUTPUT:

enter image description here

Functions:

        BlobTriggerJava1: blobTrigger

For detailed output, run func with --verbose flag.
[2024-01-25T10:02:31.162Z] Worker process started and initialized.
[2024-01-25T10:02:34.927Z] Host lock lease acquired by instance ID '000000000000000000000000AAE5F384'.
[2024-01-25T10:02:57.214Z] Executing 'Functions.BlobTriggerJava1' (Reason='New blob detected(LogsAndContainerScan): metrics/hello.txt', Id=97ca72c1-d357-497c-bbc1-f47fd78308ea)
[2024-01-25T10:02:57.218Z] Trigger Details: MessageId: a6f20214-95da-4b65-ad3f-0322360d8904, DequeueCount: 1, InsertedOn: 2024-01-25T10:02:55.000+00:00, BlobCreated: 2024-01-25T09:52:43.000+00:00, BlobLastModified: 2024-01-25T10:02:50.000+00:00
[2024-01-25T10:02:59.319Z] The details of file hello.txt are:
{
         Etag :0x8DC1D8CCA4B5F11
         metadata :{}
}
[2024-01-25T10:02:59.319Z] Function "BlobTriggerJava1" (Id: 97ca72c1-d357-497c-bbc1-f47fd78308ea) invoked by Java Worker       
[2024-01-25T10:02:59.371Z] Executed 'Functions.BlobTriggerJava1' (Succeeded, Id=97ca72c1-d357-497c-bbc1-f47fd78308ea, Duration=2631ms)

enter image description here