Minio 操作工具類
maven
<properties>
<minio.version>6.0.13</minio.version>
</properties>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>${minio.version}</version>
</dependency>
使用springboot初始化
package com.im.oss;
import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.Result;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
@Component
@RequiredArgsConstructor
public class MinioTemplate implements InitializingBean {
private final String endpoint;
private final String accessKey;
private final String secretKey;
private MinioClient client;
@Override
public void afterPropertiesSet() throws Exception {
Assert.hasText(endpoint, "Minio url 為空");
Assert.hasText(accessKey, "Minio accessKey為空");
Assert.hasText(secretKey, "Minio secretKey為空");
this.client = new MinioClient(endpoint, accessKey, secretKey);
}
/**
* 建立bucket
*
* @param bucketName bucket名稱
*/
@SneakyThrows
public void createBucket(String bucketName) {
if (!client.bucketExists(bucketName)) {
client.makeBucket(bucketName);
}
}
/**
* 擷取全部bucket
* <p>
* https://docs.minio.io/cn/java-client-api-reference.html#listBuckets
*/
@SneakyThrows
public List<Bucket> getAllBuckets() {
return client.listBuckets();
}
/**
* 根據bucketName擷取資訊
*
* @param bucketName bucket名稱
*/
@SneakyThrows
public Optional<Bucket> getBucket(String bucketName) {
return client.listBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
}
/**
* 根據bucketName删除資訊
*
* @param bucketName bucket名稱
*/
@SneakyThrows
public void removeBucket(String bucketName) {
client.removeBucket(bucketName);
}
/**
* 根據檔案前置查詢檔案
*
* @param bucketName bucket名稱
* @param prefix 字首
* @param recursive 是否遞歸查詢
* @return MinioItem 清單
*/
@SneakyThrows
public List getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {
List<Item> list = new ArrayList<>();
Iterable<Result<Item>> objectsIterator = client.listObjects(bucketName, prefix, recursive);
if (objectsIterator != null) {
Iterator<Result<Item>> iterator = objectsIterator.iterator();
if (iterator != null) {
while (iterator.hasNext()) {
Result<Item> result = iterator.next();
Item item = result.get();
list.add(item);
}
}
}
return list;
}
/**
* 擷取檔案外鍊
*
* @param bucketName bucket名稱
* @param objectName 檔案名稱
* @param expires 過期時間 <=7
* @return url
*/
@SneakyThrows
public String getObjectURL(String bucketName, String objectName, Integer expires) {
return client.presignedGetObject(bucketName, objectName, expires);
}
/**
* 擷取檔案
*
* @param bucketName bucket名稱
* @param objectName 檔案名稱
* @return 二進制流
*/
@SneakyThrows
public InputStream getObject(String bucketName, String objectName) {
return client.getObject(bucketName, objectName);
}
/**
* 上傳檔案
*
* @param bucketName bucket名稱
* @param objectName 檔案名稱
* @param stream 檔案流
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
*/
public void putObject(String bucketName, String objectName, InputStream stream) throws Exception {
client.putObject(bucketName, objectName, stream, stream.available(), "application/octet-stream");
}
/**
* 上傳檔案
*
* @param bucketName bucket名稱
* @param objectName 檔案名稱
* @param stream 檔案流
* @param size 大小
* @param contextType 類型
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
*/
public void putObject(String bucketName, String objectName, InputStream stream, long size, String contextType) throws Exception {
client.putObject(bucketName, objectName, stream, size, contextType);
}
/**
* 擷取檔案資訊, 如果抛出異常則說明檔案不存在
*
* @param bucketName bucket名稱
* @param objectName 檔案名稱
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#statObject
*/
public ObjectStat getObjectInfo(String bucketName, String objectName) throws Exception {
return client.statObject(bucketName, objectName);
}
/**
* 删除檔案
*
* @param bucketName bucket名稱
* @param objectName 檔案名稱
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#removeObject
*/
public void removeObject(String bucketName, String objectName) throws Exception {
client.removeObject(bucketName, objectName);
}
}