@Log4j
public class MinioUtils {
public static void fileUpload(String endpoint ,String accessKey,
String secrtKey,String bucketName,
String filename,String filepath) throws Exception{
try {
MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);
boolean isExist = minioClient.bucketExists(bucketName);
if(isExist) {
System.out.println("Bucket already exists.");
} else {
minioClient.makeBucket(bucketName);
}
minioClient.putObject(bucketName,filename, filepath);
log.debug(filepath + "is successfully uploaded as" + filename +"to `" + bucketName + "` bucket.");
System.out.println("/home/user/Photos/asiaphotos.zip is successfully uploaded as asiaphotos.zip to `asiatrip` bucket.");
} catch(Exception e) {
log.error("Error occurred: "+e.getMessage(), e);
throw new Exception("文件上传失败!", e);
}
}
public static void fileDownload(String endpoint ,String accessKey,
String secrtKey,String bucketName,
String filename,String result) throws Exception{
try {
MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);
boolean isExist = minioClient.bucketExists(bucketName);
if(isExist) {
throw new Exception(bucketName + " does not exist");
}
File file = new File(result);
String parent = file.getParent();
if(!FileUtils.isExists(parent)) {
FileUtils.createDirs(parent);
}
minioClient.getObject(bucketName, filename,result);
} catch(MinioException | NoSuchAlgorithmException | IOException | InvalidKeyException | XmlPullParserException e) {
throw new Exception("Error occurred: " + e);
}
}
public static void download(String endpoint ,String accessKey,
String secrtKey,String bucketName,
String filename,String result){
OutputStream out = null;
InputStream in = null;
try{
MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);
boolean isExist = minioClient.bucketExists(bucketName);
if(!isExist) {
throw new Exception(bucketName + " does not exist");
}
File file = new File(result);
String parent = file.getParent();
if(!FileUtils.isExists(parent)) {
FileUtils.createDirs(parent);
}
in = minioClient.getObject(bucketName,filename);
int len = 0;
byte[] buffer = new byte[1024];
out = new FileOutputStream(result);
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (Exception e){
e.printStackTrace();
}
}
}
public static List<String> read(String endpoint , String accessKey,
String secrtKey, String bucketName,
String filename, long offset, Long length) throws Exception{
InputStream in = null;
List<String> result = new ArrayList<>();
try {
MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);
minioClient.statObject(bucketName,filename);
boolean isExist = minioClient.bucketExists(bucketName);
if(!isExist) {
throw new Exception(bucketName + " does not exist");
}
in = minioClient.getObject(bucketName, filename,offset,length);
byte[] buf = new byte[65535];
int bytesRead;
while ((bytesRead = in.read(buf)) >= 0) {
result.add(new String(buf,0,bytesRead));
}
in.close();
}catch (Exception e) {
throw new Exception(e);
}
return result;
}
public static List<String> readByLine(String endpoint , String accessKey,
String secrtKey, String bucketName,
String filename,int offset, int length) throws Exception{
InputStream in = null;
List<String> result = new ArrayList<>();
List<String> list = new ArrayList<>();
try {
MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);
boolean isExist = minioClient.bucketExists(bucketName);
if(!isExist) {
throw new Exception(bucketName + " does not exist");
}
minioClient.statObject(bucketName, filename);
in = minioClient.getObject(bucketName, filename);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buf)) >= 0) {
result.add(new String(buf, 0, bytesRead));
}
for (String line : result) {
String[] lines = line.split("\r\n", -1);
list.addAll(new ArrayList<String>(Arrays.asList(lines)));
}
in.close();
}catch (Exception e) {
throw new Exception(e);
}
return list.subList(offset-1,length);
}
public static List<String> listBucketNameObject(String endpoint , String accessKey,
String secrtKey, String bucketName) throws Exception{
List<String> list = new ArrayList<>();
try {
MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);
boolean isExist = minioClient.bucketExists(bucketName);
if(isExist) {
System.out.println("Bucket already exists.");
}
boolean found = minioClient.bucketExists(bucketName);
if (found) {
Iterable<Result<Item>> myObjects = minioClient.listObjects(bucketName);
for (Result<Item> result : myObjects) {
Item item = result.get();
list.add(item.objectName());
log.debug(item.lastModified() + ", " + item.size() + ", " + item.objectName());
}
} else {
throw new Exception(bucketName + " does not exist");
}
} catch (Exception e) {
throw new Exception(e);
}
return list;
}
public static List<String> listObject(String endpoint , String accessKey,
String secrtKey, String bucketName,
String prefix,boolean recursive)
throws Exception{
List<String> file = new ArrayList<>();
try {
MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);
boolean found = minioClient.bucketExists(bucketName);
if (found) {
Iterable<Result<Item>> myObjects = minioClient.listObjects(bucketName,prefix,recursive,false);
for (Result<Item> result : myObjects) {
Item item = result.get();
log.debug(item.lastModified() + ", " + item.size() + ", " + item.objectName() + "," + item.objectSize());
file.add(item.objectName());
}
} else {
throw new Exception(bucketName + " does not exist");
}
} catch (Exception e) {
throw new Exception(e);
}
return file;
}
public static void downFiles(String endpoint , String accessKey,
String secrtKey, String bucketName,
String prefix,String resultPath)
throws Exception{
try {
MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);
boolean found = minioClient.bucketExists(bucketName);
if (found) {
Iterable<Result<Item>> myObjects = minioClient.listObjects(bucketName,prefix,true,false);
for (Result<Item> result : myObjects) {
Item item = result.get();
String objectName = item.objectName();
String localfile = resultPath + File.separator +objectName.replaceAll(prefix,"");
File file = new File(localfile);
String parent = file.getParent();
if(!FileUtils.isExists(parent)) {
FileUtils.createDirs(parent);
}
minioClient.getObject(bucketName,objectName,localfile);
}
} else {
throw new Exception(bucketName +" does not exist");
}
} catch (MinioException e) {
throw new MinioException("Error occurred: " + e);
} catch (Exception e) {
throw new Exception(e);
}
}
public static void copy(String endpoint , String accessKey,
String secrtKey, String bucketName,
String object,String destBucketName)
throws Exception{
try {
MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);
if(!minioClient.bucketExists(destBucketName)) {
minioClient.makeBucket(destBucketName);
}
boolean found = minioClient.bucketExists(bucketName);
if (found) {
minioClient.copyObject(bucketName,object,destBucketName);
log.debug("拷贝成功");
} else {
throw new Exception( bucketName + " does not exist");
}
} catch (MinioException e) {
throw new Exception("Error occurred: " + e);
} catch (Exception e) {
throw new Exception(e);
}
}
public static void copy(String endpoint , String accessKey,
String secrtKey, String bucketName,
String object,String destBucketName,
String destBucketObjectName)
throws Exception{
try {
MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);
boolean found = minioClient.bucketExists(bucketName);
if (found) {
minioClient.copyObject(bucketName,object,destBucketName,destBucketObjectName);
System.out.println("拷贝成功");
} else {
throw new Exception( bucketName + " does not exist");
}
} catch (MinioException e) {
throw new Exception("Error occurred: " + e);
} catch (Exception e) {
throw new Exception(e);
}
}
}