【问题标题】:IllegalStateException while creating BlobServiceClient for azure-storage-blob为 azure-storage-blob 创建 BlobServiceClient 时出现 IllegalStateException
【发布时间】:2021-11-09 11:49:00
【问题描述】:

我想使用 azure-storage-blob 客户端 SDK 在我的 Spring 5 Web 应用程序中从 Azure 获取 blob。我在创建 BlobServiceClient 以下载 blob 时收到 IllegalStateException。下面是我用来创建客户端的代码。

var accountName = "account";
var accountKey = "accountKey";
var credential = new StorageSharedKeyCredential(accountName, accountKey);
var endpoint = String.format(Locale.ROOT, "https://%s.blob.core.windows.net", accountName);
BlobServiceClient storageClient = new BlobServiceClientBuilder().endpoint(endpoint).credential(credential).buildClient();

我收到消息异常:在类路径上找不到任何 HttpClient 提供程序 - 无法创建默认 HttpClient 实例。通过调试代码,我发现在创建默认 HTTPClient 时引发了异常,我尝试手动提供 HTTP 客户端,如下所示

HttpClient httpClient = new NettyAsyncHttpClientBuilder().build();
BlobServiceClient storageClient = new BlobServiceClientBuilder().endpoint(endpoint).credential(credential).httpClient(httpClient).buildClient(); 

这样我得到错误消息java.lang.NoClassDefFoundError: reactor/netty/http/client/HttpClient

我已经添加了依赖com.azure:azure-storage-blob;V12.3.0,还手动添加了com.azure:azure-core-http-netty;V1.2.0。我没有编译错误,并且我还验证了 com.azure:azure-storage-blob 中提到的所有依赖项都存在于我的 WAR 的 lib 目录中。有人可以提供一些关于我在这里做错了什么的指针

【问题讨论】:

  • 我发布了我如何使用 BlobServiceClient 的答案。也许它有帮助。否则,您可以发布您的 pom.xml 吗?
  • 我已经尝试过下面发布的解决方案,但没有成功。我的 pom 文件的链接pastebin.com/7BUh3jhx

标签: java spring azure azure-blob-storage


【解决方案1】:

对我来说,它使用连接字符串:

    @Bean
    public BlobContainerClient blobContainerClient(
            @Value("${azure.storage.connection.string}") String connectionString,
            @Value("${azure.storage.containername}") String containerName) {
        final var blobServiceClient = new BlobServiceClientBuilder().connectionString(connectionString).buildClient();
        return blobServiceClient.getBlobContainerClient(containerName);
    }

【讨论】:

    猜你喜欢
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    相关资源
    最近更新 更多