是否有任何方法可以获取触发Azure Blob触发器的文件内容?

vatpfxk5  于 2022-11-17  发布在  其他
关注(0)|答案(2)|浏览(125)

因此,我创建了一个Azure blob触发器,它工作正常,只要我把一些文件或创建一些目录上的blob触发器发生。

**问题:**现在我不知道如何获取导致blob触发器的相同文件内容。

我可以使用Azure存储库获取文件,但我将在blob上上传大量文件,并希望对刚刚写入blob的文件进行一些处理。
先谢了

j2cgzkjk

j2cgzkjk1#

从示例文档(www.example.com)看起来非常简单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");
}

内容以字节数组的形式传入。

gev0vcfq

gev0vcfq2#

获取文件内容的方法之一是

@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
    ) {

这是工作代码

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);

    }
}

相关问题