MinioAPI淺入及問題
MinioAPI淺入及問題
bucketExists(BucketExistsArgs args) boolean
判斷桶是否存在 返回boolean
boolean gxshuju = minioClient.bucketExists(BucketExistsArgs.builder() .bucket(bucketName) .build()); if (!gxshuju) { minioClient.makeBucket(MakeBucketArgs.builder() .bucket("gxshuju") // .objectLock(true) .build()); System.out.println(bucketName + "is not exists"); } else System.out.println(bucketName + "is exists");
makeBucket(MakeBucketArgs args) void
建立桶 bucket(bucketName) objectLock(boolean)是否鎖定
minioClient.makeBucket(MakeBucketArgs.builder()
.bucket("gxshuju")
// .objectLock(true)
.build());
uploadObject(UploadObjectArgs args) ObjectWriteResponse
上傳檔案 bucket(bucketName)
object(bucket路徑(路徑斜槓必須為‘/’)->檔名)
filename(上傳檔案路徑,即本地路徑)
contentType(String 檔案型別)
minioClient.uploadObject(UploadObjectArgs.builder()
.bucket(bucketName)
.object("同名測試/111.jpg")
.filename("C:\\Users\\lenovo\\Desktop\\111.jpg")
.build());
deleteBucketEncryption(DeleteBucketEncryptionArgs args) void
刪除桶的加密配置Encryption bucket(bucketName)
minioClient.deleteBucketEncryption(DeleteBucketEncryptionArgs.builder() .bucket("gxshuju") .build());
deleteBucketLifecycle(DeleteBucketLifecycleArgs args) void
刪除桶的生命週期配置 bucket(bucketName)
字面意思 不做解釋
deleteBucketTags(DeleteBucketTagsArgs args) ..
deleteBucketPolicy(DeleteBucketPolicyArgs args) ..
deleteBucketReplication(DeleteBucketReplicationArgs args) ..
deleteBucketNotification(DeleteBucketNotificationArgs args) ..
deleteObjectLockConfiguration(DeleteObjectLockConfigurationArgs args) ..
minioClient.deleteBucketLifecycle(DeleteBucketLifecycleArgs.builder()
.bucket("gxshuju")
.build());
getBucketEncryption(GetBucketEncryptionArgs args) SseConfiguration
獲取桶的加密配置
getBucketLifecycle(GetBucketLifecycleArgs args) LifecycleConfiguration
getBucketNotification(GetBucketNotificationArgs args) NotificationConfiguration
getBucketPolicy(GetBucketPolicyArgs args) String
getBucketReplication(GetBucketReplicationArgs args) ReplicationConfiguration
getBucketTags(GetBucketTagsArgs args) Tags
getBucketVersioning(GetBucketVersioningArgs args) VersioningConfiguration
getObjectLockConfiguration(GetObjectLockConfigurationArgs args) ObjectLockConfiguration
SseConfiguration gxshuju1 = minioClient.getBucketEncryption(GetBucketEncryptionArgs.builder()
.bucket("gxshuju")
.build());
listBuckets() List
獲取所有桶
bucket.creationDate() 桶的建立日期
bucket.name() 桶的名稱
List<Bucket> buckets = minioClient.listBuckets();
buckets.forEach(bucket -> {
System.out.println(bucket.creationDate().toLocalDateTime() + "," + bucket.name());
});
removeBucket(RemoveBucketArgs args) void
刪除桶
minioClient.removeBucket(RemoveBucketArgs.builder().bucket("jjj").build());
setBucketEncryption(SetBucketEncryptionArgs args)
設定一個桶的加密配置
setBucketLifecycle(SetBucketLifecycleArgs args)
setBucketNotification(SetBucketNotificationArgs args)
setBucketPolicy(SetBucketPolicyArgs args)
setBucketReplication(SetBucketReplicationArgs args)
setBucketTags(SetBucketTagsArgs args)
setBucketVersioning(SetBucketVersioningArgs args)
setObjectLockConfiguration(SetObjectLockConfigurationArgs args)
copyObject(CopyObjectArgs args) ObjectWriteResponse
複製物件 sse(加密方式)
下例為複製aaa空間下的Saved Pictures內的001420yp398.jpg到bbb空間給定名稱001420yp398.jpg
minioClient.copyObject(CopyObjectArgs.builder()
.bucket("bbb")
.object("001420yp398.jpg")
.source(CopySource.builder()
.bucket("aaa")
.object("Saved Pictures/001420yp398.jpg")
.build())
.build());
deleteObjectTags(DeleteObjectTagsArgs args) void
刪除物件標籤 刪除aaa空間的xxxx.xls附帶的標籤
minioClient.deleteObjectTags(DeleteObjectTagsArgs.builder()
.bucket("aaa")
.object("xxxx.xls")
.build());
disableObjectLegalHold(DisableObjectLegalHoldArgs args)
enableObjectLegalHold(EnableObjectLegalHoldArgs args)
minioClient.disableObjectLegalHold(DisableObjectLegalHoldArgs.builder()
.bucket("my-bucketName")
.object("my-objectName")
.build());
downloadObject(DownloadObjectArgs args)
下載檔案 sse(加密配置) 加密下載 filename(需指定到檔案)
minioClient.downloadObject(DownloadObjectArgs.builder()
.bucket("aaa")
.object("Saved Pictures/001420yp398.jpg")
.filename("F:/aaa.jpg")
.build());
uploadSnowballObjects(UploadSnowballObjectsArgs args)
多檔案上傳
List<SnowballObject> objects = new ArrayList<>();
objects.add(
new SnowballObject(
"C:\\Users\\lenovo\\Desktop\\xxxx.xls",
new ByteArrayInputStream("hello".getBytes(StandardCharsets.UTF_8)),
5,
null));
objects.add(
new SnowballObject(
"C:\\Users\\lenovo\\Desktop\\新建文字文件.txt",
new ByteArrayInputStream("java".getBytes(StandardCharsets.UTF_8)),
4,
null));
minioClient.uploadSnowballObjects(UploadSnowballObjectsArgs.builder()
.bucket("bbb")
.objects(objects)
.build());
removeObject(RemoveObjectArgs args)
刪除物件
minioClient.removeObject(RemoveObjectArgs.builder().bucket("aaa").object("xxxx.xls").build());
statObject(StatObjectArgs args) StatObjectResponse
獲取物件的物件資訊和元資料
StatObjectResponse bbb = minioClient.statObject(StatObjectArgs.builder()
.bucket("bbb")
.object("xxx.jpg")
.build());
System.out.println(bbb);
listenBucketNotification(ListenBucketNotificationArgs args)
listObjects(ListObjectsArgs args) Iterable<Result
列出桶的物件
composeObject(ComposeObjectArgs args)
removeObjects(RemoveObjectsArgs args)
刪除多個物件
Minio依賴衝突
An attempt was made to call a method that does not exist. The attempt was made from the following location:
io.minio.S3Base.<clinit>(S3Base.java:105)
The following method did not exist:
okhttp3.RequestBody.create([BLokhttp3/MediaType;)Lokhttp3/RequestBody;
The method's class, okhttp3.RequestBody, is available from the following locations:
jar:file:/D:/repository/com/squareup/okhttp3/okhttp/3.14.9/okhttp-3.14.9.jar!/okhttp3/RequestBody.class
The class hierarchy was loaded from the following locations:
okhttp3.RequestBody: file:/D:/repository/com/squareup/okhttp3/okhttp/3.14.9/okhttp-3.14.9.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of okhttp3.RequestBody
因為minio底層要依賴okhttp進行和minio服務端進行通訊,由於專案中多個依賴底層都依賴了不同版本的okhttp,導致這裡的okhttp因版本不同,某些方法不可用,嘗試降級minio的版本。
如果要用最新版,請加入下面依賴:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>