问题描述
在升级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;
}
}
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!