添加 Azure 存储 Blob 容器输入绑定 Azure Functions Java

Posted

技术标签:

【中文标题】添加 Azure 存储 Blob 容器输入绑定 Azure Functions Java【英文标题】:Add Azure Storage Blob Container input binding Azure Functions Java 【发布时间】:2021-11-02 00:50:52 【问题描述】:

我有一个 EventHubTriggered Function 应用程序。这里是方法签名的代码示例:

@FunctionName("foo") @StorageAccount("foostorageread") 公共 HttpResponseMessage 运行( @HttpTrigger( 名称=“请求”, 方法 = HttpMethod.POST, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage 请求, @BlobInput( 名称=“test1”, 数据类型 = "字符串", path = "blobStorageContainer/test1.json") 字符串 test1,

        @BlobInput(
                  name = "test2", 
                  dataType = "string", 
                  path = "blobStorageContainer/test2.json") String test2,
       
        final ExecutionContext context) 

我想在方法签名中将输入绑定添加到 CloudBlobContainer(上面的方法签名中的 blobStorageContainer),这样我就不需要在我的方法中显式连接到这个容器(因为我需要从这个容器)。有什么注释/方法我可以在 Java 中做到这一点吗?

【问题讨论】:

【参考方案1】:

我按照blog连接Blob Storage容器获取文件。

请参考下面的代码以使用CloudBlobContainer 连接容器并在 blob 中不存在时创建新容器。

CloudStorageAccount storageAccountDest;
CloudBlobClient blobClientDest = null;
CloudBlobContainer containerDest = null;
String storageConnectionStringDest = System.getenv("AzureStorageDemoConnectionStringDest");

// Parse the connection string and create a blob client to interact with Blob
// storage
storageAccountDest = CloudStorageAccount.parse(storageConnectionStringDest);
blobClientDest = storageAccountDest.createCloudBlobClient();  
containerDest = blobClientDest.getContainerReference("files2");

// Create the container if it does not exist with public access.
context.getLogger().info("Creating container: " + containerDest.getName());
containerDest.createIfNotExists(BlobContainerPublicAccessType.CONTAINER, new BlobRequestOptions(),
new OperationContext());
CloudBlob blobDest = containerDest.getBlockBlobReference(filename);
try 
    context.getLogger().info("Start Uploading blob: " + filename);
    blobDest.uploadFromByteArray(content, 0, content.length);
    context.getLogger().info("Finished Uploading blob: " + filename);
 
catch (IOException e) 
    // TODO Auto-generated catch block
    e.printStackTrace();

请看here

【讨论】:

以上是关于添加 Azure 存储 Blob 容器输入绑定 Azure Functions Java的主要内容,如果未能解决你的问题,请参考以下文章

Azure Functions - Blob 流动态输入绑定

如何将静态文件添加/上传到 Azure Blob 存储容器的特定路径

我们可以使用 Azure Active Directory 提供对 blob/容器/存储帐户的访问吗?

具有输入绑定的 Azure 函数的 Azure 事件中心存储容器配置

如何监视 Azure 存储容器/子文件夹中 Blob 的创建并触发逻辑应用发送电子邮件

Azure Functions:如何在 Azure 存储队列的绑定表达式中使用 POCO?