【Azure 存储服务】Java Azure Storage SDK V12使用Endpoint连接Blob Service遇见 The Azure Storage endpoint url is malformed

简介: 【Azure 存储服务】Java Azure Storage SDK V12使用Endpoint连接Blob Service遇见 The Azure Storage endpoint url is malformed

问题描述

使用Azure Storage Account的共享访问签名(Share Access Signature) 生成的终结点,连接时遇见  The Azure Storage endpoint url is malformed (Azure 存储终结点 URL 格式不正确)

Storage Account SDK in pom.xml:
    <dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-storage-blob</artifactId>
      <version>12.6.0</version>
    </dependency>

App.Java 文件中,创建 BlobServiceClient 对象代码:

String endpoint ="BlobEndpoint=https://://************...";
BlobServiceClient blobServiceClientbyendpoint = new BlobServiceClientBuilder().endpoint(endpoint).buildClient();

获取Endpoint的方法为(Azure Portal --> Storage Account --> Share access signature)

 

当执行Java 代码时,main函数抛出异常:java.lang.IllegalArgumentException: The Azure Storage endpoint url is malformed

PS C:\LBWorkSpace\MyCode\1-Storage Account - Operation Blob by Connection String - Java> 
 & 'C:\Program Files\Microsoft\jdk-11.0.12.7-hotspot\bin\java.exe'
 '-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:59757' 
'@C:\Users\AppData\Local\Temp\cp_6ux3xmehddi1mc4fanfjupd3x.argfile'
 'com.blobs.quickstart.App' 
Azure Blob storage v12 - Java quickstart sample
2022-05-26 10:24:29 ERROR BlobServiceClientBuilder - The Azure Storage endpoint url is malformed.
Exception in thread "main" java.lang.IllegalArgumentException: The Azure Storage endpoint url is malformed.
        at com.azure.storage.blob.BlobServiceClientBuilder.endpoint(BlobServiceClientBuilder.java:132)
        at com.blobs.quickstart.App.main(App.java:30)

 

问题分析

消息 [The Azure Storage endpoint url is malformed (Azure 存储终结点 URL 格式不正确)] 说明代码中使用的格式不对,回到生成endopoint的页面查看,原来使用的是连接字符串 Connection String.  与直接使用Access Key中的Connection String是相同的代码方式,而 Endpoint 是指当个连接到Blob Service的URL。

 

回到代码中,发现新版本把连接方式进行了区分:

  • 使用Connection String时,用 new BlobServiceClientBuilder().connectionString(connectStr).buildClient();
  • 使用Endpoint时,用 new BlobServiceClientBuilder().endpoint(endpoint).buildClient();

所以,解决 endpoint url malformed 关键就是使用正确的 SAS URL 或者是 Connection String

//使用连接字符串时
String connectStr ="BlobEndpoint=https://:************.blob.core.chinacloudapi.cn/;...SharedAccessSignature=sv=2020-08-0...&sig=**************";
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();
//使用SAS终结点
String endpoint ="https://************.blob.core.chinacloudapi.cn/?sv=2020-08-04...&sig=*********************";
BlobServiceClient blobServiceClientbyendpoint = new BlobServiceClientBuilder().endpoint(endpoint).buildClient();

完整的示例代码:

package com.blobs.quickstart;
/**
 * Azure blob storage v12 SDK quickstart
*/
import com.azure.storage.blob.*;
import com.azure.storage.blob.models.*;
import java.io.*;
public class App
{
    public static void main( String[] args ) throws IOException
    {
         
        System.out.println("Azure Blob storage v12 - Java quickstart sample\n");
        // Retrieve the connection string for use with the application. The storage
        // connection string is stored in an environment variable on the machine
        // running the application called AZURE_STORAGE_CONNECTION_STRING. If the environment variable
        // is created after the application is launched in a console or with
        // Visual Studio, the shell or application needs to be closed and reloaded
        // to take the environment variable into account.
        
        //String connectStr ="DefaultEndpointsProtocol=https;AccountName=******;AccountKey=***********************;EndpointSuffix=core.chinacloudapi.cn";// System.getenv("AZURE_STORAGE_CONNECTION_STRING");
        String connectStr ="BlobEndpoint=https://******* */.blob.core.chinacloudapi.cn/;QueueEndpoint=https://*******.queue.core.chinacloudapi.cn/;FileEndpoint=https://*******.file.core.chinacloudapi.cn/;TableEndpoint=https://*******.table.core.chinacloudapi.cn/;SharedAccessSignature=sv=2020...&sig=*************************";
        BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();
        //Create a unique name for the container
        String containerName = "lina-" + java.util.UUID.randomUUID();
        // Create the container and return a container client object
        BlobContainerClient containerClient = blobServiceClient.createBlobContainer(containerName);
        BlobContainerClient containerClient1 = blobServiceClient.getBlobContainerClient("container-name");
        if(!containerClient1.exists())
        {
            System.out.println("create containerName");
            blobServiceClient.createBlobContainer("container-name");
        }
        
        System.out.println("create containerName .....");
        // // Create a local file in the ./data/ directory for uploading and downloading
        // String localPath = "./data/";
        // String fileName = "quickstart" + java.util.UUID.randomUUID() + ".txt";
        // File localFile = new File(localPath + fileName);
        // // Write text to the file
        // FileWriter writer = new FileWriter(localPath + fileName, true);
        // writer.write("Hello, World!");
        // writer.close();
        // // Get a reference to a blob
        // BlobClient blobClient = containerClient.getBlobClient(fileName);
        // System.out.println("\nUploading to Blob storage as blob:\n\t" + blobClient.getBlobUrl());
        // // Upload the blob
        // blobClient.uploadFromFile(localPath + fileName);
        // System.out.println("\nListing blobs...");
        // // List the blob(s) in the container.
        // for (BlobItem blobItem : containerClient.listBlobs()) {
        //     System.out.println("\t" + blobItem.getName());
        // }
        // // Download the blob to a local file
        // // Append the string "DOWNLOAD" before the .txt extension so that you can see both files.
        // String downloadFileName = fileName.replace(".txt", "DOWNLOAD.txt");
        // File downloadedFile = new File(localPath + downloadFileName);
        // System.out.println("\nDownloading blob to\n\t " + localPath + downloadFileName);
        // blobClient.downloadToFile(localPath + downloadFileName);
        // // Clean up
        // System.out.println("\nPress the Enter key to begin clean up");
        // System.console().readLine();
        // System.out.println("Deleting blob container...");
        // containerClient.delete();
        // System.out.println("Deleting the local source and downloaded files...");
        // localFile.delete();
        // downloadedFile.delete();
        System.out.println("Done");
    }
}

 

参考资料

快速入门:使用 Java v12 SDK 管理 blob: https://docs.azure.cn/zh-cn/storage/blobs/storage-quickstart-blobs-java?tabs=powershell%2Cenvironment-variable-windows

 

相关文章
|
1月前
|
Java Windows
【Azure Function】部署Java Function失败:报错deploy [ERROR] Status code 401和警告 'China North 3' may not be a valid region
1:deploy [ERROR] Status code 401, (empty body). 2: China North 3 may not be a valid region,please refer to https://aka.ms/maven_function_configuration#supported-regions for values. 3:  <azure.functions.maven.plugin.version>1.36.0</azure.functions.maven.plugin.version>
38 11
|
6月前
|
存储 Java API
【Azure 存储服务】Java Storage SDK 调用 uploadWithResponse 代码示例(询问ChatGTP得代码原型后人力验证)
【Azure 存储服务】Java Storage SDK 调用 uploadWithResponse 代码示例(询问ChatGTP得代码原型后人力验证)
|
3月前
|
Java 开发工具 Windows
【Azure App Service】在App Service中调用Stroage SDK上传文件时遇见 System.OutOfMemoryException
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
|
3月前
|
Java Maven Android开发
【Azure Developer】VS Code打包Java maven Project 遇见 BUILD FAILURE
Unknown lifecycle phase "lean". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>
|
4月前
|
Java 程序员 测试技术
Java|让 JUnit4 测试类自动注入 logger 和被测 Service
本文介绍如何通过自定义 IDEA 的 JUnit4 Test Class 模板,实现生成测试类时自动注入 logger 和被测 Service。
65 5
|
4月前
|
Java 数据库
基于java的汽车服务管理系统(Car Service Management System)
基于java的汽车服务管理系统(Car Service Management System)
38 0
|
6月前
|
Java 容器
【Azure Function App】Java Function在运行中遇见内存不足的错误
【Azure Function App】Java Function在运行中遇见内存不足的错误
|
6月前
|
存储 API 开发工具
【Azure Storage Blob】如何通过.NET Azure Storage Blobs SDK获取到Blob的MD5值呢?
【Azure Storage Blob】如何通过.NET Azure Storage Blobs SDK获取到Blob的MD5值呢?
|
6月前
|
Java Maven
【Azure Function App】Java Function部署到Azure后出现中文显示乱码问题
【Azure Function App】Java Function部署到Azure后出现中文显示乱码问题
|
6月前
|
开发工具 iOS开发 容器
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错

热门文章

最新文章

  • 1
    浏览器输入 URL 后的那些事儿
  • 2
    【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 3
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 4
    【01】完整开发即构美颜sdk的uni官方uts插件—让所有开发者可以直接使用即构美颜sdk的能力-优雅草卓伊凡
  • 5
    如何配置 Java 环境变量:设置 JAVA_HOME 和 PATH
  • 6
    【Java若依框架】RuoYi-Vue的前端和后端配置步骤和启动步骤
  • 7
    Java社招面试中的高频考点:Callable、Future与FutureTask详解
  • 8
    自研Java框架 Sunrays-Framework使用教程「博客之星」
  • 9
    接替此文【下篇-服务端+后台管理】优雅草蜻蜓z系统JAVA版暗影版为例-【蜻蜓z系列通用】-2025年全新项目整合搭建方式-这是独立吃透代码以后首次改变-独立PC版本vue版搭建教程-优雅草卓伊凡
  • 10
    Java快速入门之数组、方法