import io.minio.;
import io.minio.errors.;
import io.minio.messages.Item;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Slf4j
public class MinioTest {
private static MinioClient getMinioClient() {
String ak = "xxxx";
String sk = "bbbb";
String endpoint = "http://ccc.ccc.ddd:9000";
String bucketName = "buckbb";
MinioClient minioClient =
MinioClient.builder()
.endpoint(endpoint)
.credentials(ak, sk)
.build();
boolean exists = false;
try {
exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
if (exists) {
log.info("{}桶存在", bucketName);
} else {
try {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
log.info("{}桶不存在,重建成功", bucketName);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
return minioClient;
}
public static void main(String[] args) {
MinioClient minioClient = getMinioClient();
String COPY_FILE_SOURCE_PATH = "nf/mofile/v3/x/x/glt/x.gltf";
List<String> list = new ArrayList();
getCopyFileList(minioClient, COPY_FILE_SOURCE_PATH, list);
for (String path : list) {
CopySource copySource = CopySource.builder()
.bucket("buckname")
.object("nf/mofile/v3/x/x/glt/x.jpg").build();
CopyObjectArgs copyObjectArgs = CopyObjectArgs.builder()
.source(copySource)
.bucket("buckname")
.object("nf/mofile/v3/glt/b.jpg")
.build();
try {
minioClient.copyObject(copyObjectArgs);
} catch (ErrorResponseException | InsufficientDataException | InternalException
| InvalidKeyException | InvalidResponseException | IOException |
NoSuchAlgorithmException | XmlParserException | ServerException e) {
log.error(e.getMessage(), e);
}
}
}
private static void getCopyFileList(MinioClient minioClient1, String path, List<String> list) {
//复制文件源路径
ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder()
.bucket("nnicepack")
.prefix(path)
.recursive(false)
.build();
// 获取所有文件
Iterable<Result<Item>> result = minioClient1.listObjects(listObjectsArgs);
for (Iterator<Result<Item>> it = result.iterator(); it.hasNext(); ) {
Result<Item> item = it.next();
try {
if (item.get().isDir()) {
getCopyFileList(minioClient1, item.get().objectName(), list);
}else {
log.debug("+" + item.get().objectName());
list.add(item.get().objectName());
}
} catch (ErrorResponseException | InsufficientDataException | InternalException
| InvalidKeyException | InvalidResponseException | IOException |
NoSuchAlgorithmException | XmlParserException | ServerException e) {
log.error(e.getMessage(), e);
}
}
}
}