azure - 与 Azure Cosmos DB 的 Java 无密码连接的构建问题

标签 azure azure-cosmosdb azure-java-sdk

我正在尝试使用与 Azure Cosmos DB for NoSQL 的 Java 无密码连接来构建 Azure 客户端。我已按照此 Microsoft Learn article 中提供的说明进行操作并根据建议创建了自定义角色。但是,我收到以下错误:

Exception in thread "main" java.lang.RuntimeException: Client initialization failed. Check if the endpoint is reachable and if your auth token is valid. More info: https://aka.ms/cosmosdb-tsg-service-unavailable-java
    at com.azure.cosmos.implementation.RxDocumentClientImpl.initializeGatewayConfigurationReader(RxDocumentClientImpl.java:512)
    at com.azure.cosmos.implementation.RxDocumentClientImpl.init(RxDocumentClientImpl.java:547)
    at com.azure.cosmos.implementation.AsyncDocumentClient$Builder.build(AsyncDocumentClient.java:295)
    at com.azure.cosmos.CosmosAsyncClient.<init>(CosmosAsyncClient.java:171)
    at com.azure.cosmos.CosmosClientBuilder.buildAsyncClient(CosmosClientBuilder.java:1052)
    at com.azure.cosmos.CosmosClient.<init>(CosmosClient.java:38)
    at com.azure.cosmos.CosmosClientBuilder.buildClient(CosmosClientBuilder.java:1086)
    at com.azure.cosmos.sample.sync.CosmoDBClientPasswordless.main(CosmoDBClientPasswordless.java:25)

这是我正在使用的代码:

public class CosmoDBClientPasswordless {  
  public static void main(String[] args) {   
     // Set up your Cosmos DB client   
     String endpoint = "<endpoint>";
     DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()  
.build();   
     CosmosClient client = new CosmosClientBuilder().endpoint(endpoint)
                           .credential(credential)
                           .consistencyLevel(ConsistencyLevel.EVENTUAL)
                           .buildClient();  
       
}

}

我已经设置了我的环境变量 AZURE_SUBSCRIPTION_ID AZURE_CLIENT_ID AZURE_CLIENT_SECRET AZURE_TENANT_ID

我已验证这些 key ,并且使用相同的 key 我能够访问 Azure Blob 存储。

按照文档中的建议创建了自定义角色,它曾经有效,但现在不起作用。

如果我使用宇宙 key 和相同的端点,那么我的代码就可以工作。

 String endpoint = "<endpoint>";
        String key = "<key>";
        CosmosClientBuilder clientBuilder = new CosmosClientBuilder()
                .endpoint(endpoint)
                .key(key);

        CosmosClient client = clientBuilder.buildClient();

最佳答案

您可以使用启用的托管标识连接到 Cosmos DB,无需任何密码或连接字符串。详细信息在这里:https://learn.microsoft.com/en-us/azure/cosmos-db/managed-identity-based-authentication

关于azure - 与 Azure Cosmos DB 的 Java 无密码连接的构建问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77249395/

相关文章:

azure - 如何使用 ARM Powershell 在 Azure DocumentDB 中上传 JSON 文件

azure - 单例 Azure Cosmos DB 客户端

Azure IoT Edge 在电源周期内实时存储和转发

Python 通过 SSH 身份验证克隆私有(private) GitHub 存储库,无需访问 ssh 二进制文件(在 Azure Functions 下)

azure - 如何在 Azure 中 key 轮换后满足客户端对存储 Blob 的请求

asp.net - 什么是 SkipExtraFilesOnServer?部署 azure 。 ASP.NET vNext

c# - documentdb 与 linq 连接

azure - 使用 spring boot 在 azure 中从 v8 升级到 v12 java sdk 后无法连接到 azure-blob-storage

java - 如何使用 java sdk 在 Azure Blob 存储中上传单个视频文件的多个 block ?

Azure documentDB - 具有指定 ID 或名称的资源已存在