首页 > 编程语言 >Azure Blob Storage Java SDK使用SAS Token授权读取文件403报错

Azure Blob Storage Java SDK使用SAS Token授权读取文件403报错

时间:2023-06-18 21:55:53浏览次数:74  
标签:Java Storage System SAS 报错 Azure SDK

问题描述

代码如下,内容十分简单,只是list path的操作。

点击查看代码
        DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClientBuilder().endpoint(blob).sasToken(sasToken).buildClient();
        DataLakeFileSystemClient testFs = dataLakeServiceClient.getFileSystemClient("test");
        DataLakeDirectoryClient testDir = testFs.getDirectoryClient("test");
        testDir.listPaths().forEach(
                s-> System.out.println(s.getName())
        );

报错信息

点击查看报错信息
Exception in thread "main" com.azure.storage.file.datalake.models.DataLakeStorageException: If you are using a StorageSharedKeyCredential, and the server returned an error message that says 'Signature did not match', you can compare the string to sign with the one generated by the SDK. To log the string to sign, pass in the context key value pair 'Azure-Storage-Log-String-To-Sign': true to the appropriate method call.
If you are using a SAS token, and the server returned an error message that says 'Signature did not match', you can compare the string to sign with the one generated by the SDK. To log the string to sign, pass in the context key value pair 'Azure-Storage-Log-String-To-Sign': true to the appropriate generateSas method call.
Please remember to disable 'Azure-Storage-Log-String-To-Sign' before going to production as this string can potentially contain PII.
Status code 403, "{"error":{"code":"AuthenticationFailed","message":"Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:b44da7b7-a01f-002d-70e3-a12a79000000\nTime:2023-06-18T12:48:32.9335073Z","detail":{"AuthenticationErrorDetail":"Signature fields not well formed."}}}"
	at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:732)
	at com.azure.core.implementation.http.rest.ResponseExceptionConstructorCache.invoke(ResponseExceptionConstructorCache.java:56)

调查过程

检查SAS Token

SAS Token确认有效。

检查SDK和存储账户设置

调查发现,storage account并没有启用HNS(ADLS Gen2),然而使用的类DataLakeServiceClient和包Azure File Data Lake client library for Java均为ADLS Gen2设计。且SDK文档中明确写明不支持未开启HNS的storage account。

抓取网络请求包

云服务SDK最终都是将代码转换为REST API,而ADLS和普通的Blob使用不同REST API。因此只要能抓取实际的HTTP请求查看便能确定问题所在。

实际抓包结果表明,尽管代码中配置endpoint为https://account-name.blob.core.windows.net,最终SDK发送出去的请求去往了account-name.dfs.core.windows.net。
image

JVM抓包方式
参考fiddler官方文档:Configure a Java Application to Use Fiddler
Fiddler证书安装到本机,直接通过Java代码配置。

配置JVM使用Fiddler作为代理代码
        // Set fiddler as proxy server for JVM
        System.setProperty("http.proxyHost", "127.0.0.1");
        System.setProperty("https.proxyHost", "127.0.0.1");
        System.setProperty("http.proxyPort", "8888");
        System.setProperty("https.proxyPort", "8888");
        // set trusted certificate store of JVM to Windows-ROOT
        System.setProperty("javax.net.ssl.trustStoreType","Windows-ROOT");

结论

报错信息不能反映实际错误原因。但考虑到客户用例为文档明确写明的不支持的用法,因此也属正常情况。解决方式就是使用正确的类,可以参考官方在Github上给出的示例用法。Azure Blob Java SDK - ListContainersExample

标签:Java,Storage,System,SAS,报错,Azure,SDK
From: https://www.cnblogs.com/dereksworkshop/p/17489830.html

相关文章

  • linux java调用sh脚本
    1、2、importorg.jeecg.zhongyi.auto_dep.util.CommandStreamGobbler;importorg.jeecg.zhongyi.util.LogbackUtil;importorg.jeecg.zhongyi.util.vo.Result;importjava.io.IOException;importjava.io.InputStreamReader;importjava.util.LinkedList;importjava.......
  • linux sh脚本一键自动部署 前端项目、docker项目、java项目
    1、2、静态前端,admin_xx_auto_deployment.sh#!/bin/bashsource/etc/profile#.~/.bash_profilecd/data/yyyyy_temp/xx_admin_code/yyyyy2-admingitpullPATH=$PATH:./node_modules/.binecho$PATHnpmrunbuild:test1cp-r/data/yyyyy_temp/xx_admin_code/yyyyy2-ad......
  • SQL调优:让Java内存分担计算
    作者: 剽悍一小兔我们在工作中,经常会因为一条慢sql调半天。这一节,我给大家介绍一种提升查询效率的思路,那就是让Java内存帮我们分担一些运算。案例还是采用springBoot日记本系统,源码下载地址和教程在文末。先改一个BUG之前遗留了一个BUG需要我们解决,就是在日记的详情页,日记类型没......
  • Java百炼成仙1.1 他叫叶小凡
    第1章陨铁山脉篇1.1他叫叶小凡“这里是哪里?”男子揉了揉有些迷糊的脑袋,环顾四周,只见一望无际的平原。他的白色服饰在强烈的阳光下发出闪亮的光芒,仿佛是一抹清新的色彩点缀在这沉寂的荒野之中。天空中阳光透过透明的大气层倾泻而下,温暖的光线照耀着男子苍白的脸庞。空气中的尘埃让......
  • Java代码精简之道-10条代码精简方案
    场景一、Java中利用try-with-resource语句JDK7开始新增了对需要关闭资源处理的特殊语法try-with-resource。所有实现Closeable接口的“资源”,均可采用try-with-resource进行简化。例如InputStream,OutputStream,Connection,Statement,ResultSet等接口都实现了,使用try-with-reso......
  • Java多线程-Lesson01-线程的创建
    线程创建的三种方式继承Thread类步骤:继承Thread类重写run()方法调用start()开启线程重写run()方法:@Overridepublicvoidrun(){for(inti=0;i<200;i++){System.out.println("run():"+i);}} run()方法里面就是我们多......
  • 必知必会:Java基础
    创建对象有几种方式(1)new创建对象;(2)反射创建对象;(3)采用clone机制;(4)序列化机制。创建反射对象的几种方式(1)类.class:通过 类名.class 创建反射获取对象; 类.class 是静态加载,是JVM编译时就要加载。Class<ClassDemo>oClass=ClassDemo.class;(2) object.getClass() :以 实......
  • java--多态
       ......
  • 基于JAVA物品租赁系统设计与实现
    物品租赁市场像是一块大蛋糕,现在网络购物趋势发展势头迅猛,网上物品租赁的市场开拓现在也在发展中,任何商家都需要抢占先机,开拓网上物品租赁市场,抢占网上交易市场份额。 所以本人设计了物品租赁系统,实现网上的物品租赁。根据本系统的研究现状和发展趋势,系统从需求分析、结构设计、数......
  • 基于JAVA公交路线查询系统
    进入21世纪以来,计算机通讯得到迅速发展,计算机的信息技术全面地普及到人类社会的各个方面,信息化成为世界经济和社会发展的大趋势。随着信息时代的发展,快捷高效的处理信息已成为各行各业的基本需求。在公交路线查询系统中,为满足其系统化,自动化,高效化的要求,需要设计一个有效的公交路......