集成
WEB界面创建 order-bucket 桶和 access-key(生成 secret-key)
minio:
endpoint: http://localhost:9010
access-key: 6uajU1xXhYt1X3SvJnoo
secret-key: rePumpyhi8sHaLzqXtBzOHJZ0bUB3JOijhjZY6oX
bucket-name: order-bucket
@Data
@Builder
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioProperty {
private String endpoint;
private String accessKey;
private String secretKey;
private String bucketName;
}
@Configuration
public class MinioConfig {
@Bean
public MinioClient minioClient(@NonNull MinioProperty minioProperty) {
return MinioClient.builder()
.endpoint(minioProperty.getEndpoint())
.credentials(minioProperty.getAccessKey(), minioProperty.getSecretKey())
.build();
}
}
@Service
@RequiredArgsConstructor
public class MinioService {
private final MinioClient minioClient;
private final MinioProperty minioProperty;
public void uploadFile(String objectName, String filePath) throws Exception {
PutObjectArgs args = PutObjectArgs.builder()
.bucket(minioProperty.getBucketName())
.object(objectName)
.stream(new FileInputStream(filePath), Files.size(Paths.get(filePath)), -1)
.build();
minioClient.putObject(args);
}
public void downloadFile(String objectName, String filePath) throws Exception {
GetObjectArgs args = GetObjectArgs.builder()
.bucket(minioProperty.getBucketName())
.object(objectName)
.build();
String content = minioClient.getObject(args).object();
Path path = Paths.get(filePath);
Files.write(path, content.getBytes());
}
}
测试
@SpringBootTest
class MinioServiceTest {
@Autowired
private MinioService minioService;
@Test
void uploadFile() throws Exception {
minioService.uploadFile("order-test", "E:\\work\\jxch-study\\study-springcloud\\study-springcloud\\cloud-order\\src\\main\\resources\\order-test1.txt");
}
@Test
void downloadFile() throws Exception {
minioService.downloadFile("order-test", "E:\\work\\jxch-study\\study-springcloud\\study-springcloud\\cloud-order\\src\\main\\resources\\order-test1-d.txt");
}
}
依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
MinIO 伪分布式部署
version: '3.8'
services:
minio1:
image: minio/minio
ports:
- '9010:9000'
- '9011:9001'
environment:
- MINIO_ROOT_USER=root
- MINIO_ROOT_PASSWORD=minio123456789
volumes:
- './data/minio1-1:/data1'
- './data/minio1-2:/data2'
command: server --console-address ":9001" http://minio{1...2}/data{1...2}
minio2:
image: minio/minio
ports:
- '9020:9000'
- '9021:9001'
environment:
- MINIO_ROOT_USER=root
- MINIO_ROOT_PASSWORD=minio123456789
volumes:
- './data/minio2-1:/data1'
- './data/minio2-2:/data2'
command: server --console-address ":9001" http://minio{1...2}/data{1...2}