Azure Function EventHub Trigger Blob output with Managed Identity auth

Azure Function EventHub Trigger Blob output with Managed Identity auth

我正在尝试在 JAVA 中创建一个具有 EventHub 触发器的 Azure 函数。在函数接收的每个事件的有效负载中,有一个 blob 的路径,并将该 blob 移动到另一个存储帐户中的另一个容器。

为此,我没有使用任何类型的绑定,只使用了触发器。

到目前为止,我已经能够毫无问题地使其工作,并且我使用 Managed Identity 而不是通过连接字符串 [=27] 对 EventHub 进行身份验证=].但是,为了移动 blob,我确实使用连接字符串,因为我使用 com.microsoft.azure.storage 包。

Azure 存储客户端代码片段:

        CloudStorageAccount storageAccountDest;
        CloudBlobClient blobClientDest = null;
        CloudBlobContainer containerDest = null;

        String storageConnectionStringDest = System.getenv("AzureStorageDemoConnectionStringDest");

        storageAccountDest = CloudStorageAccount.parse(storageConnectionStringDest);
        blobClientDest = storageAccountDest.createClientCloudBlob();
        containerDest = blobClientDest.getContainerReference("<DEST-CONTAINER>");

有什么方法可以使用托管身份轻松连接到存储帐户?

这里是函数代码:

import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
import java.util.*;
import org.json.JSONObject;  
import org.json.JSONArray;
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*;  
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;

/**
 * Azure Functions with Event Hub trigger.
 */
public class EventHubTriggerBlobStorageSink {
    /**
     * This function will be invoked when an event is received from Event Hub.
     */
    @FunctionName("EventHubTriggerBlobStorageSink")
    public void run(
        @EventHubTrigger(name = "message", eventHubName = "<EH-NAME>", 
        connection = "<EH-CONNECTION-STRING-MI>", consumerGroup = "$Default", cardinality = Cardinality.MANY) List<String> message,
        final ExecutionContext context)
        throws InvalidKeyException, URISyntaxException, StorageException
        {
        CloudStorageAccount storageAccountDest;
        CloudBlobClient blobClientDest = null;
        CloudBlobContainer containerDest = null;

        String storageConnectionStringDest = System.getenv("AzureStorageDemoConnectionStringDest");

        storageAccountDest = CloudStorageAccount.parse(storageConnectionStringDest);
        blobClientDest = storageAccountDest.createCloudBlobClient();
        containerDest = blobClientDest.getContainerReference("<CONTAINER-NAME>");


        message.forEach(singleMessage -> {

            /* 1. COPY BLOB INTO DEST */

          /* 2. DELETE BLOB INTO SOURCE */

        });
        
        
    }
}

从此线程中找到代码片段:Provide some Java code sample for using Managed Identity

有趣的部分:

import com.azure.identity.*;
...
String endpoint = "https://<storageAccount>.blob.core.windows.net";
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
  .endpoint(endpoint)
  .credential(new DefaultAzureCredentialBuilder().build())
  .buildClient();

可以找到完整的示例应用程序here