0

So I have created a Azure blob trigger, and it is working fine as soon as I put some file or create some directory on the blob the trigger happens.

Question: Now I can not figure out how can I get the same file content which causes the blob trigger.

I can get the files using Azure storage library, but I am going to upload lots of files on the blob and want to do some processing on the file which has just written on the blob.

Thanks in advance

Raman Mishra
  • 2,635
  • 2
  • 15
  • 32

2 Answers2

0

It looks pretty straightforward from the example documentation - https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob#trigger---java-example

@FunctionName("blobprocessor")
public void run(
    @BlobTrigger(name = "file",
                 dataType = "binary",
                 path = "myblob/{name}",
                 connection = "MyStorageAccountAppSetting") byte[] content,
    @BindingName("name") String filename,
    final ExecutionContext context
) {
    context.getLogger().info("Name: " + filename + " Size: " + content.length + " bytes");
}

The content gets passed in as a byte array.

David C
  • 664
  • 1
  • 8
  • 21
0

One of the way we can get the file content is

@FunctionName("BlobTrigger-Java")
    public void blobTriggerWhenUploadFile(
            @BlobTrigger(name = "content", path = "data/{name}", dataType = "binary", connection = "StorageConnection") String content,
            @BindingName("name") String name,
            final ExecutionContext context
    ) {

This is working code

import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;

/**
 * Azure Functions with Azure Blob trigger.
 */
public class BlobTriggerFunction {
    /**
     * This function will be invoked when a new or updated blob is detected at the specified path. The blob contents are provided as input to this function.
     *
     * @return
     */
    @FunctionName("BlobTrigger-Java")
    public void blobTriggerWhenUploadFile(
            @BlobTrigger(name = "content", path = "data/{name}", dataType = "binary", connection = "StorageConnection") byte[] content,
            @BindingName("name") String name,
            final ExecutionContext context
    ) {
        
        context.getLogger().info("Java Blob trigger function processed a blob. Name: " + name + "\n  Size: " + content.length + " Bytes");
//String result=new String(content,StandardCharsets.UTF_8);
        InputStream inputStream = new ByteArrayInputStream(content);
        String result = null;
        try {
            result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        context.getLogger().info("The upload file content is: " + result);

    }
}
Madhura.M
  • 23
  • 5