初始化数据库
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
package com.budwk.app;
|
||||
|
||||
import io.minio.*;
|
||||
import io.minio.errors.*;
|
||||
import io.minio.messages.Item;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Map;
|
||||
|
||||
class MinioUtil {
|
||||
MinioClient minioClient = null;
|
||||
String bucketName = "njupt";
|
||||
String endpoint = "http://localhost:9000";
|
||||
String accessKey = "o1MwuzixNqAXh5zqEfMH";
|
||||
String secretKey = "RVnZum91JmGO79qtzWy2gKMBsF6OxAaibGrr7ets";
|
||||
|
||||
public MinioUtil() {
|
||||
//构造方法 minio客户端
|
||||
minioClient = MinioClient.builder()
|
||||
.endpoint(endpoint)
|
||||
.credentials(accessKey, secretKey)
|
||||
.build();
|
||||
System.out.println("Minio客户端链接成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果桶不存在则创建桶
|
||||
*/
|
||||
private void createBucketName() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
|
||||
if (exists) {
|
||||
System.out.println(bucketName + " exists");
|
||||
} else {
|
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
|
||||
System.out.println(bucketName + " is created successfully");
|
||||
}
|
||||
}
|
||||
|
||||
public void fileUpload(String path, String fileUploadPath, String fileName) {
|
||||
try {
|
||||
FileInputStream fileInputStream = new FileInputStream(new File(path));
|
||||
fileUpload(fileInputStream, fileUploadPath, fileName);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void fileUpload(InputStream is, String fileUploadPath, String fileName) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
try {
|
||||
this.createBucketName();
|
||||
} catch (ServerException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
minioClient.putObject(
|
||||
PutObjectArgs
|
||||
.builder()
|
||||
.bucket(bucketName)
|
||||
.object(fileUploadPath + "/" + fileName)
|
||||
.stream(is, -1, 10485760)
|
||||
.build());
|
||||
System.out.println("successfully uploaded as 【" + fileUploadPath + "】 to 【" + bucketName + "】bucket.");
|
||||
}
|
||||
|
||||
private void fileDownload(String filePath, String fileName) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
filePath = filePath.replace("\\", "/");
|
||||
minioClient.downloadObject(
|
||||
DownloadObjectArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.object(filePath)
|
||||
.filename(fileName)
|
||||
.build());
|
||||
System.out.println("successfully uploaded as 【" + filePath + "/" + fileName + "】 to 【" + bucketName + "】bucket.");
|
||||
}
|
||||
|
||||
public InputStream downloadInputSteam(String filePath) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
filePath = filePath.replace("\\", "/");
|
||||
return minioClient.getObject(
|
||||
GetObjectArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.object(filePath)
|
||||
.build());
|
||||
}
|
||||
|
||||
|
||||
public StatObjectResponse getFileInfo(String filePath) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
filePath = filePath.replace("\\", "/");
|
||||
return minioClient.statObject(StatObjectArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.object(filePath)
|
||||
.build());
|
||||
}
|
||||
|
||||
public Iterable<Result<Item>> getFiles(String path) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
return minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(path).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param bucketName 库名字
|
||||
* @param fileName 文件路径
|
||||
* @return
|
||||
*/
|
||||
public boolean delete(String bucketName, String fileName) {
|
||||
/*
|
||||
* String bucketName = "test2";
|
||||
* String fileName = "/2023-04-07/16808560218465670_img.png";
|
||||
* address+bucketName+fileName 就是访问路径,删除需要后两个参数。
|
||||
*/
|
||||
try {
|
||||
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(fileName).build());
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class MinioTest {
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
MinioUtil minioUtil = new MinioUtil();
|
||||
// Iterable<Result<Item>> iterable = minioUtil.getFiles("data/");
|
||||
// for (Result<Item> itemResult : iterable) {
|
||||
// Item item = itemResult.get();
|
||||
// System.out.println(item.objectName());
|
||||
// InputStream inputStream = minioUtil.downloadInputSteam(item.objectName());
|
||||
// String targetPath = "D:\\tmp\\" + item.objectName();
|
||||
// File file = new File(targetPath);
|
||||
// FileUtils.copyInputStreamToFile(inputStream, file);
|
||||
// Map<String, String> map = PdfUtil.readPdf2Txt(file.getAbsolutePath());
|
||||
// for (Map.Entry<String, String> entry : map.entrySet()) {
|
||||
// System.out.println(entry.getKey() + " " + entry.getValue());
|
||||
// }
|
||||
// }
|
||||
String path = "E:\\DevOpsCode\\zhgh_cug_v4\\README.md";
|
||||
String fileUploadPath = "data";
|
||||
minioUtil.fileUpload(path, fileUploadPath, "README.md");
|
||||
// boolean success = minioUtil.delete("njupt", "data/README.md");
|
||||
// System.out.println(success?"成功":"失败");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user