【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

 

相关文章
|
21天前
|
Java API 开发工具
百宝箱开放平台 ✖️ Java SDK
百宝箱提供Java SDK,支持开发者集成其开放能力。需先发布应用,准备Java 8+及Maven环境,通过添加依赖安装SDK,并初始化客户端调用对话型或生成型智能体,实现会话管理、消息查询与文件上传等功能。
1151 0
百宝箱开放平台 ✖️ Java SDK
|
5月前
|
存储 Java API
MinIO Java SDK 7.1.4 升级到 8.5.17 需要注意什么
现在我需要你帮我分析对比这个两个sdk在对外的接口设计上是否有不兼容的变更
412 5
|
8月前
|
存储 XML 开发工具
【Azure Storage Account】利用App Service作为反向代理, 并使用.NET Storage Account SDK实现上传/下载操作
本文介绍了如何在Azure上使用App Service作为反向代理,以自定义域名访问Storage Account。主要内容包括: 1. **设置反向代理**:通过配置`applicationhost.xdt`和`web.config`文件,启用IIS代理功能并设置重写规则。 2. **验证访问**:测试原生URL和自定义域名的访问效果,确保两者均可正常访问Storage Account。 3. **.NET SDK连接**:使用共享访问签名(SAS URL)初始化BlobServiceClient对象,实现通过自定义域名访问存储服务。
129 1
|
存储 Java API
【Azure 存储服务】Java Storage SDK 调用 uploadWithResponse 代码示例(询问ChatGTP得代码原型后人力验证)
【Azure 存储服务】Java Storage SDK 调用 uploadWithResponse 代码示例(询问ChatGTP得代码原型后人力验证)
138 0
|
存储 Java 开发工具
【Azure 存储服务】Azure Blob Storage SDK 升级失败,遇见 Unsatisfied Dependency Exception 和 Unexpected Length Exception
【Azure 存储服务】Azure Blob Storage SDK 升级失败,遇见 Unsatisfied Dependency Exception 和 Unexpected Length Exception
131 0
|
Java 开发工具
通过Java SDK调用阿里云模型服务
在阿里云平台上,可以通过创建应用并使用模型服务完成特定任务,如生成文章内容。本示例展示了一段简化的Java代码,演示了如何调用阿里云模型服务生成关于“春秋战国经济与文化”的简短文章。示例代码通过设置系统角色为历史学家,并提出文章生成需求,最终处理并输出生成的文章内容。在实际部署前,请确保正确配置环境变量中的密钥和ID,并根据需要调整SDK导入语句及类名。更多详情和示例,请参考相关链接。
|
存储 API 开发工具
【Azure Storage Blob】如何通过.NET Azure Storage Blobs SDK获取到Blob的MD5值呢?
【Azure Storage Blob】如何通过.NET Azure Storage Blobs SDK获取到Blob的MD5值呢?
145 3
|
开发工具 iOS开发 容器
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错
132 0
|
存储 API 网络架构
【Azure 存储服务】MP4视频放在Azure的Blob里面,用生成URL在浏览器中打开之后,视频可以正常播放却无法拖拽视频的进度
【Azure 存储服务】MP4视频放在Azure的Blob里面,用生成URL在浏览器中打开之后,视频可以正常播放却无法拖拽视频的进度
178 0
|
2月前
|
开发工具 Android开发
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
424 11
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡