在升级Java Azure Blob Storage SDK的过程中,先后遇见了 UnsatisfiedDependencyException 和 UnexpectedLengthException.
对于问题一: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() 。
代码改动截图如下:
@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; } }
问题描述 使用Azure Storag Blob REST API上传文件,用SharedKey作为Authorization出现403错误。 错误消息 b'\xef\xbb\xbfAuthenti