minIO集成springboot

问题

minIO与spring集成。

步骤

创建桶

创建桶

创建key

找到创建账号页面,如下图:
创建账号
点击创建,如下图:
创建key

设置如下权限:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:*"],
      "Resource": ["arn:aws:s3:::<桶名>/*"]
    },
    {
      "Effect": "Deny",
      "Action": ["s3:Delete*"],
      "Resource": ["arn:aws:s3:::*"]
    }
  ]
}

只能操作具体桶,但是,拒绝删除操作。

Spring中使用

MinioConfig.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.minio.MinioClient;

/**
 * Minio 配置信息
 */
@Configuration
public class MinioConfig {

    @Value("${minio.url}")
    private String url;

    @Value("${minio.accessKey}")
    private String accessKey;

    @Value("${minio.secretKey}")
    private String accessSecret;

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

注入client

@Resource
private MinioClient minioClient;

上传

@Override
public String uploadFile(MultipartFile file) throws Exception {
    String fileName = FileUploadUtils.extractFilename(file);
    InputStream inputStream = file.getInputStream();
    PutObjectArgs args = PutObjectArgs.builder()
            .bucket(bucketName)
            .object(fileName)
            .stream(inputStream, file.getSize(), -1)
            .contentType(file.getContentType())
            .build();
    minioClient.putObject(args);
    IoUtils.closeQuietly(inputStream);
    return fileName;
}

下载

@Override
public ResponseEntity<InputStreamResource> downloadFile(String year, String month, String day, String fileName) throws Exception {
    InputStream stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(String.format("%s/%s/%s/%s", year, month, day,fileName)).build());
    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename*=utf-8''" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString()));

    return ResponseEntity.ok()
            .headers(headers)
            .contentType(MediaType.APPLICATION_OCTET_STREAM)
            .body(new InputStreamResource(stream));
}

参考

相关推荐

  1. SpringBoot集成Minio

    2024-07-17 15:10:01       31 阅读
  2. SpringBoot集成Minio(接上文)

    2024-07-17 15:10:01       58 阅读
  3. minio 整合springboot

    2024-07-17 15:10:01       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 15:10:01       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 15:10:01       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 15:10:01       57 阅读
  4. Python语言-面向对象

    2024-07-17 15:10:01       68 阅读

热门阅读

  1. python基础篇(12):继承

    2024-07-17 15:10:01       23 阅读
  2. Spring解决循环依赖问题的四种方法

    2024-07-17 15:10:01       19 阅读
  3. 人工智能与人类社会的共生共荣

    2024-07-17 15:10:01       19 阅读
  4. Catboost 不能做多变量回归?

    2024-07-17 15:10:01       20 阅读
  5. Qt将毫秒转化为时分秒格式

    2024-07-17 15:10:01       22 阅读
  6. 查找json中指定节点的值,替换为指定的值

    2024-07-17 15:10:01       20 阅读
  7. SpringBoot --附包扫描、自动装配原理(面试题)

    2024-07-17 15:10:01       20 阅读