springboot gets the file size uploaded to the minio server

springboot gets the file size uploaded to the minio server

Preface

Generally, when minio uploads files, it will get the file size when uploading and save it in the database. If you want to get it directly from minio, how to get it?

Explore ways to get file size

  • pom dependency
        <dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>8.4.5</version>
        </dependency>
  • MinioClient class

From the MinioClient provided by minio, you can see that there is no method to directly obtain the file size.
Insert image description here
But you can see that there is a size field in the StatObjectResponse class returned by statObject:
Insert image description here
then you can get the StatObjectResponse class first, and then get the size.

Practice and examples

/**
* 获取文件大小
*/
public long getFileSize(String fileName) throws Exception {
    
    
        try {
    
    
            StatObjectResponse fileInfo = getFileInfo(minioConfig.getBucketName(), fileName);
            return fileInfo.size();
        } catch (Exception e) {
    
    
            log.error("获取文件大小失败:{}", e.getMessage(), e);
            throw new BusinessException("文件不存在或信息有误,获取文件失败");
        }
    }

/**
* 获取文件信息
*/
public StatObjectResponse getFileInfo(@NonNull String bucketName, String fileName) throws Exception {
    
    
        return minioClient.statObject(StatObjectArgs.builder()
                .bucket(bucketName)
                .object(fileName)
                .build());
    }

Guess you like

Origin blog.csdn.net/weixin_46505978/article/details/131631804