问题
minIO与spring集成。
步骤
创建桶
创建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;@Beanpublic 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));
}
参考
- 访问管理
- Minio Custom Authorization scenarios