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>