Java uses minio to upload files in batches

First introduce minio, I have never used it before

MinlO is an object storage service based on the Apache License v2.0 open source protocol. It is compatible with the Amazon S3 cloud storage service interface and is very suitable for storing large-capacity unstructured data. For example, pictures, videos, log files, backup data and container/virtual machine images, etc., and an object file can be of any size, ranging from a few kb to a maximum of 5T. MinlO is a very lightweight service that can be easily integrated with other applications, like NodeJS, Redis or MySQL.
MinIO is native to Kubernetes and is the only object storage suite available on every public cloud, every Kubernetes distribution, private cloud and at the edge. MinIO is software defined and 100% open source under the GNU AGPL v3.

Here first configure minio information in nacos:

insert image description hereFirst import minio dependencies:

        <dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>${
    
    minio.version}</version>
        </dependency>

Inject in the controller:

    @Autowired
    MinioClient minioClient;

    @Autowired
    private MinioConfig minioConfig;

    @Value(value = "${minio.bucketName}")
    private String bucket;

minio configuration class: MinioConfig is as follows:

@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig
{
    
    
    /**
     * 服务地址
     */
    private String url;

    /**
     * 用户名
     */
    private String accessKey;

    /**
     * 密码
     */
    private String secretKey;

    /**
     * 存储桶名称
     */
    private String bucketName;

    public String getUrl()
    {
    
    
        return url;
    }

    public void setUrl(String url)
    {
    
    
        this.url = url;
    }

    public String getAccessKey()
    {
    
    
        return accessKey;
    }

    public void setAccessKey(String accessKey)
    {
    
    
        this.accessKey = accessKey;
    }

    public String getSecretKey()
    {
    
    
        return secretKey;
    }

    public void setSecretKey(String secretKey)
    {
    
    
        this.secretKey = secretKey;
    }

    public String getBucketName()
    {
    
    
        return bucketName;
    }

    public void setBucketName(String bucketName)
    {
    
    
        this.bucketName = bucketName;
    }

    @Bean
    public MinioClient getMinioClient()
    {
    
    
        return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
    }
}

Single file upload interface:

@PostMapping("/uploadFile")
    R upload(MultipartFile file) throws Exception {
    
    
        String fileName = FileUploadUtils.extractFilename(file);
        PutObjectArgs args = PutObjectArgs.builder()
                .bucket(minioConfig.getBucketName())
                .object(fileName)
                .stream(file.getInputStream(), file.getSize(), -1)
                .contentType(file.getContentType())
                .build();
        minioClient.putObject(args);
        System.out.println(fileName);
        return R.ok(fileName);

    }

Multiple file upload interface:

 @ApiOperation("多文件上传minio")
    @PostMapping("/uploadAllFile")
    R upload(MultipartFile[] files) throws Exception {
    
    

        for (MultipartFile file : files) {
    
    
            String fileName = FileUploadUtils.extractFilename(file);
            PutObjectArgs args = PutObjectArgs.builder()
                    .bucket(minioConfig.getBucketName())
                    .object(fileName)
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .contentType(file.getContentType())
                    .build();
            minioClient.putObject(args);
            System.out.println(fileName);
        }
        return R.ok();
    }

Select multiple files in postman:
insert image description here
upload result:
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_42260782/article/details/131128350