7 changed files with 75 additions and 3 deletions
@ -0,0 +1,8 @@
|
||||
package com.biutag.exception; |
||||
|
||||
public class MinioOperateException extends RuntimeException { |
||||
|
||||
public MinioOperateException(Throwable cause) { |
||||
super(cause); |
||||
} |
||||
} |
||||
@ -0,0 +1,46 @@
|
||||
package com.biutag.util; |
||||
|
||||
import com.biutag.exception.MinioOperateException; |
||||
import io.minio.BucketExistsArgs; |
||||
import io.minio.MakeBucketArgs; |
||||
import io.minio.MinioClient; |
||||
import io.minio.UploadObjectArgs; |
||||
import io.minio.errors.MinioException; |
||||
|
||||
import java.io.IOException; |
||||
import java.security.InvalidKeyException; |
||||
import java.security.NoSuchAlgorithmException; |
||||
|
||||
public class MinioUtil { |
||||
|
||||
public static void main(String[] args) { |
||||
// 设置MinIO服务器的端点、Access Key和Secret Key
|
||||
String endpoint = "http://172.31.217.20:31813"; |
||||
String accessKey = "your-access-key"; |
||||
String secretKey = "your-secret-key"; |
||||
try { |
||||
// 创建MinIO客户端
|
||||
MinioClient minioClient = MinioClient.builder() |
||||
.endpoint(endpoint) |
||||
.credentials(accessKey, secretKey) |
||||
.build(); |
||||
// 检查存储桶是否存在,如果不存在则创建
|
||||
String bucketName = "mailbox"; |
||||
BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder() |
||||
.bucket(bucketName) |
||||
// 添加额外的选项
|
||||
.build(); |
||||
if (!minioClient.bucketExists(bucketExistsArgs)) { |
||||
MakeBucketArgs makeBucketArgs = MakeBucketArgs.builder().bucket(bucketName).build(); |
||||
minioClient.makeBucket(makeBucketArgs); |
||||
} |
||||
// 上传文件到MinIO服务器
|
||||
String objectName = "your-object-name"; |
||||
String filePath = "path/to/your/file.txt"; |
||||
UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder().bucket(bucketName).object(objectName).filename(filePath).build(); |
||||
minioClient.uploadObject(uploadObjectArgs); |
||||
} catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) { |
||||
throw new MinioOperateException(e.getCause()); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue