【Azure 存储服务】Azure Blob Storage SDK 升级失败,遇见 Unsatisfied Dependency Exception 和 Unexpected Length Exception
问题描述
在升级Java Azure Blob Storage SDK的过程中,先后遇见了 UnsatisfiedDependencyException 和 UnexpectedLengthException.
错误一:Org.springframework.beans.factory UnsatisfiedDependencyException: Error creating bean with name 'azureFileServiceImpl': Unsatisfied dependency expressed through field 'blobServiceClient'.
错误二:com.azure.core.exception UnexpectedLengthException Request body emitted 27183 bytes, less than the expected 31671447552 bytes.
问题解答
对于问题一:UnsatisfiedDependencyException 错误,一般来说,都是应用中引入包的版本不兼容导致,最好的办法就是升级到当前最新的版本。如把 springboot 版本升级到了2.5.6,azure storage blob版本升级到12.13.0。
但当问题一解决后,引发了问题二:com.azure.core.exception UnexpectedLengthException Request body emitted 27183 bytes, less than the expected 31671447552 bytes。
经过对代码中涉及到 File Length, Size等代码的调试后,发现问题所在:Azure storage Blob 由12.4.0升级到12.13.0后,获取文件的 length 发生了变化。
由旧版本的 file对象的 getTotalSpace() 变为 FileInputstream对象的 available() 。
代码改动截图如下:
附录一:UploadExportFile 完整代码
@Service public class AzureServiceImpl implements AzureService{ @Autowired private BlobServiceClient blobServiceClient; @Autowired private SysResourceMapper sysResourceMapper; @Autowired private SysOfflineExportMapper sysOfflineExportMapper; @Autowired private SysOfflineExportService sysOfflineExportService; @Override public boolean uploadExportFile(File file, SysOfflineExport export) { BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("uploadblobdata"); if(!blobContainerClient.exists()){ createBlobContainer(); } FileInputStream fileInputStream; try { fileInputStream = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); export.setLog(e.getMessage()); export.setStatus(OperationStatus.Done); export.setResultType(ResultType.Failed); sysOfflineExportService.updateByUuid(export); return false; } //long size = file.getTotalSpace(); long size = fileInputStream.available(); String fileName = export.getFilenames(); BlobClient blobClient = blobContainerClient.getBlobClient(fileName); blobClient.upload(fileInputStream,size); String blobUrl = blobClient.getBlobUrl(); export.setDownloadurl(blobUrl); sysOfflineExportService.updateByUuid(export); if(StringUtils.isNotBlank(blobUrl)){ export.setStatus(OperationStatus.Done); export.setResultType(ResultType.Success); sysOfflineExportService.updateByUuid(export); return true; } export.setStatus(OperationStatus.Done); export.setLog("upload file to blob failed!"); sysOfflineExportService.updateByUuid(export); return false; } }
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2020-11-24 【Azure Redis 缓存 Azure Cache For Redis】使用Redis自带redis-benchmark.exe命令测试Azure Redis的性能