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));
}

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值