【Azure 存储服务】Azure Blob Storage SDK 升级失败,遇见 Unsatisfied Dependency Exception 和 Unexpected Length Exception

Azure ,存储,Blob ,服务 · 浏览次数 : 59

小编点评

## Problem Analysis **Problem 1: UnsatisfiedDependencyException** * Cause: The application is using an incompatible version of the Azure Storage Blob SDK, causing the `azureFileServiceImpl` bean to be unable to be created. * Solution: Upgrade to the latest SDK version compatible with the application. In this case, upgrade from 2.5.6 to 12.13.0. **Problem 2: UnexpectedLengthException** * Cause: The file size obtained from the Azure Blob client is smaller than expected, causing the `com.azure.core.exception UnexpectedLengthException` error. * Possible cause: The Azure Storage Blob service changed the way it calculates the file size, which is now obtained using `available()` instead of `getTotalSpace()`. * Solution: Review the updated code that retrieves the file size and ensure it reflects the actual size received from Azure Blob. ## Code Modifications **Problem 1:** * The code initially used `file.getTotalSpace()` to calculate the file size. * After the upgrade to 12.13.0, the code uses `fileInputStream.available()` to get the available bytes in the `FileInputStream`. * The `available()` method might return a different value than `getTotalSpace()` due to the way it calculates the size. **Problem 2:** * The code previously used `file.available()` to get the file size before uploading it to Azure Blob. * After the upgrade, the code uses `fileInputStream.available()` to get the available bytes in the `FileInputStream`, which might be different than the previously calculated size. * The code should be updated to use the same approach as before the upgrade to ensure the correct file size is retrieved. ## Additional Notes * Review the updated code and ensure all related components are also upgraded to the latest compatible versions. * Consider adding logging statements throughout the code to track the file size calculations and compare them to ensure they match the expected values. * Investigate further if the issue persists and consult the official Azure Storage Blob documentation or community forums for further assistance.

正文

问题描述

在升级Java Azure Blob Storage SDK的过程中,先后遇见了 UnsatisfiedDependencyExceptionUnexpectedLengthException.

错误一: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;
    }

}

 

与【Azure 存储服务】Azure Blob Storage SDK 升级失败,遇见 Unsatisfied Dependency Exception 和 Unexpected Length Exception 相似的内容:

【Azure 存储服务】Azure Blob Storage SDK 升级失败,遇见 Unsatisfied Dependency Exception 和 Unexpected Length Exception

问题描述 在升级Java Azure Blob Storage SDK的过程中,先后遇见了 UnsatisfiedDependencyException 和 UnexpectedLengthException. 错误一:Org.springframework.beans.factory Unsati

【Azure 存储服务】.NET7.0 示例代码之上传大文件到Azure Storage Blob (一)

问题描述 在使用Azure的存储服务时候,如果上传的文件大于了100MB, 1GB的情况下,如何上传呢? 问题解答 使用Azure存储服务时,如果要上传文件到Azure Blob,有很多种工具可以实现。如:Azure 门户, Azure Storage Explorer, 命令行工具 az copy

【Azure 存储服务】使用 AppendBlobClient 对象实现对Blob进行追加内容操作

问题描述 在Azure Blob的官方示例中,都是对文件进行上传到Blob操作,没有实现对已创建的Blob进行追加的操作。如果想要实现对一个文件的多次追加操作,每一次写入的时候,只传入新的内容? 问题解答 Azure Storage Blob 有三种类型: Block Blob, Append Bl

【Azure 存储服务】MP4视频放在Azure的Blob里面,用生成URL在浏览器中打开之后,视频可以正常播放却无法拖拽视频的进度

问题描述 把MP4视频放在Azure的Blob里面,用生成URL在浏览器中打开之后,视频可以正常播放却无法拖拽视频的进度,这是什么情况呢? 问题解答 因为MP4上传到Azure Blob后,根据公开的权限,可以直接通过Storage Blob URL +/ Blob Container + / Bl

【Azure 存储服务】记一次调用Storage Blob API使用 SharedKey Authorization出现的403错误

问题描述 使用Azure Storag Blob REST API上传文件,用SharedKey作为Authorization出现403错误。 错误消息 b'\xef\xbb\xbfAuthenti

【Azure 存储服务】多设备并发往 Azure Storage Blob 的 Container 存数据是否可以

问题描述 多设备并发往 Azure Storage Blob 的 Container 存数据是否可以? 问题解答 可以! Azure Storage 是支持的并发存储数据的,Blob 可以使用乐观并发或悲观并发模型的,具体实现可以参考文档:https://docs.microsoft.com/zh-

【Azure 存储服务】.NET7.0 示例代码之上传大文件到Azure Storage Blob (二)

问题描述 在上一篇博文(【Azure 存储服务】.NET7.0 示例代码之上传大文件到Azure Storage Blob (一):https://www.cnblogs.com/lulight/p/17061631.html)中,介绍了第一种分片的方式上传文件。 本文章接着介绍第二种方式,使用 M

【Azure 存储服务】如何查看Storage Account的删除记录,有没有接口可以下载近1天删除的Blob文件信息呢?

问题描述 如何查看Storage Account的删除记录,有没有接口可以下载近1天删除的Blob文件信息呢?因为有时候出现误操作删除了某些Blob文件,想通过查看删除日志来定位被删除的文件信息。 问题解答 如果没有启用Storage Account的软删除功能,则没有办法直接查看近期有删除的Blo

【Azure 应用服务】Function App / App Service 连接 Blob 报错

问题描述 因 Blob 启用了防火墙功能,但是当把App Service 或 Function App的出站IP地址都加入到Blob的白名单中,为什么访问还是403错误呢? 问题解答 Azure Storage的IP网络规则不适用于同一数据中心的客户端。 存储帐户部署在同一区域中的服务使用专用的 A

【Azure 存储服务】访问Azure Blob File遇见400-Condition Headers not support错误的解决之路

This XML file does not appear to have any style information associated with it. The document tree is shown below.