【问题标题】:Add Azure Storage Blob Container input binding Azure Functions Java添加 Azure 存储 Blob 容器输入绑定 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 中做到这一点吗?

【问题讨论】:

    标签: azure-functions azure-java-sdk


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2018-05-17
      • 2017-04-06
      • 2021-08-27
      • 2020-04-03
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      相关资源
      最近更新 更多