赞
踩
maven项目引入jar 依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.3.5</version>
</dependency>
一、链接客户端
//构造方法 minio客户端
MinioClient minioClient = MinioClient.builder()
.endpoint("http://youduk1:9029")
.credentials("admin", "admin456789")
.build();
System.out.println("客户端链接成功");
MinioClient minioClient =
MinioClient.builder()
.endpoint("https://play.min.io:9000")
.credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
.region("eu-east-1")
.httpClient(customHttpClient)
.build();
二、桶操作
1、判断桶是否存在
boolean found =
minioClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build());
if (found) {
System.out.println("my-bucketname exists");
} else {
System.out.println("my-bucketname does not exist");
}
2、创建桶
// 创建桶
boolean boo = minioClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname1").build());
if (!boo) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket("my-bucketname1").build());
System.out.println("my-bucketname1 is created successfully");
}
//创建桶指定区域
if (!minioClient.bucketExists(
BucketExistsArgs.builder().bucket("my-bucketname-in-eu").build())) {
minioClient.makeBucket(
MakeBucketArgs.builder().bucket("my-bucketname-in-eu").region("eu-west-1").build());
System.out.println("my-bucketname-in-eu is created successfully");
}
// Create bucket 'my-bucketname-in-eu-with-object-lock' in 'eu-west-1' with object lock
// functionality enabled.
//创建桶,加入锁
if (!minioClient.bucketExists(
BucketExistsArgs.builder().bucket("my-bucketname-in-eu-with-object-lock").build())) {
minioClient.makeBucket(
MakeBucketArgs.builder()
.bucket("my-bucketname-in-eu-with-object-lock")
.region("eu-west-1")
.objectLock(true)
.build());
System.out.println("my-bucketname-in-eu-with-object-lock is created successfully");
}
3、列出所有桶
// 列出所有桶
List<Bucket> bucketList = minioClient.listBuckets();
for (Bucket bucket : bucketList) {
System.out.println(bucket.creationDate() + ", " + bucket.name());
}
//列出所有桶,包括桶的信息。
List<Bucket> bucketList = minioClient.listBuckets(ListBuckets.builder().extraHeaders(headers).build());
for (Bucket bucket : bucketList) {
System.out.println(bucket.creationDate() + ", " + bucket.name());
}
//获取加密通getBucketEncryption
SseConfiguration config =
minioClient.getBucketEncryption(
GetBucketEncryptionArgs.builder().bucket("my-bucketname").build());
//获取生命周期桶getBucketLifecycle
LifecycleConfiguration config =
minioClient.getBucketLifecycle(
GetBucketLifecycleArgs.builder().bucket("my-bucketname").build());
System.out.println("Lifecycle configuration: " + config);
//获取通知桶信息getBucketNotification
NotificationConfiguration config =
minioClient.getBucketNotification(
GetBucketNotificationArgs.builder().bucket("my-bucketname").build());
//获取策略桶信息getBucketPolicy
String config =
minioClient.getBucketPolicy(GetBucketPolicyArgs.builder().bucket("my-bucketname").build());
//获取复制桶getBucketReplication
ReplicationConfiguration config =
minioClient.getBucketReplication(
GetBucketReplicationArgs.builder().bucket("my-bucketname").build());
//获取标签桶getBucketTags
Tags tags = minioClient.getBucketTags(GetBucketTagsArgs.builder().bucket("my-bucketname").build());
获取版本桶getBucketVersioning
VersioningConfiguration config =
minioClient.getBucketVersioning(
GetBucketVersioningArgs.builder().bucket("my-bucketname").build());
//获取对象锁的桶信息getObjectLockConfiguration
ObjectLockConfiguration config =
minioClient.getObjectLockConfiguration(
GetObjectLockConfigurationArgs.builder().bucket("my-bucketname").build());
System.out.println("Mode: " + config.mode());
System.out.println("Duration: " + config.duration().duration() + " " + config.duration().unit());
4、删除一个桶。
注意: - removeBucket不会删除存储桶里的对象,你需要通过removeObject API来删除它们。
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build());
if (found) {
minioClient.removeBucket(RemoveBucketArgs.builder().bucket("my-bucketname").build());
System.out.println("my-bucketname is removed successfully");
} else {
System.out.println("my-bucketname does not exist");
}
//删除加密桶
minioClient.deleteBucketEncryption(DeleteBucketEncryptionArgs.builder().bucket("my-bucketname").build());
//删除桶生命周期
minioClient.deleteBucketLifecycle(DeleteBucketLifecycleArgs.builder().bucket("my-bucketname").build());
//删除桶标签
minioClient.deleteBucketTags(DeleteBucketTagsArgs.builder().bucket("my-bucketname").build());
//删除桶策略
minioClient.deleteBucketTags(DeleteBucketPolicyArgs.builder().bucket("my-bucketname").build());
//删除桶复制配置
minioClient.deleteBucketReplication(DeleteBucketReplicationArgs.builder().bucket("my-bucketname").build());
//删除桶通知
minioClient.deleteBucketNotification(DeleteBucketNotificationArgs.builder().bucket("my-bucketname").build());
//删除桶对象锁object-lock配置在
minioClient.deleteObjectLockConfiguration(DeleteObjectLockConfigurationArgs.builder().bucket("my-bucketname").build());
5、监听通信息
String[] events = {"s3:ObjectCreated:*", "s3:ObjectAccessed:*"};
try (CloseableIterator<Result<NotificationRecords>> ci =
minioClient.listenBucketNotification(
ListenBucketNotificationArgs.builder()
.bucket("bucketName")
.prefix("")
.suffix("")
.events(events)
.build())) {
while (ci.hasNext()) {
NotificationRecords records = ci.next().get();
for (Event event : records.events()) {
System.out.println("Event " + event.eventType() + " occurred at " + event.eventTime()
+ " for " + event.bucketName() + "/" + event.objectName());
}
}
}
6、列出桶中所有对象
//列出桶中对象
Iterable<Result<Item>> results =
minioClient.listObjects(ListObjectsArgs.builder().bucket("my-bucketname").build());
for (Result<Item> result : results) {
Item item = result.get();
System.out.println(item.lastModified() + "\t" + item.size() + "\t" + item.objectName());
}
minioClient.listObjects(
ListObjectsArgs.builder()
.bucket("my-bucketname")
.recursive(true) //是否递归查找,如果是false,就模拟文件夹结构查找。
.useApiVersion1(false) //boolean 如果是true, 使用版本1 REST API
.startAfter("ExampleGuide.pdf") //在ExampleGuide.pdf之后
.prefix("新建") //对象名称的前缀
.maxKeys(100)//列出最多100个
.build()
);
for (Result<Item> result : results1) {
Item item = result.get();
System.out.println(item.lastModified() + "\t" + item.size() + "\t" + item.objectName());
}
7、设置桶信息
//设置加密
minioClient.setBucketEncryption(
SetBucketEncryptionArgs.builder().bucket("my-bucketname").config(SseConfiguration.newConfigWithSseS3Rule()).build());
//设置生命周期
List<LifecycleRule> rules = new LinkedList<>();
rules.add(
new LifecycleRule(
Status.ENABLED,
null,
null,
new RuleFilter("documents/"),
"rule1",
null,
null,
new Transition((ZonedDateTime) null, 30, "GLACIER")));
rules.add(
new LifecycleRule(
Status.ENABLED,
null,
new Expiration((ZonedDateTime) null, 365, null),
new RuleFilter("logs/"),
"rule2",
null,
null,
null));
LifecycleConfiguration config = new LifecycleConfiguration(rules);
minioClient.setBucketLifecycle(
SetBucketLifecycleArgs.builder().bucket("my-bucketname").config(config).build());
//设置通知
List<EventType> eventList = new LinkedList<>();
eventList.add(EventType.OBJECT_CREATED_PUT);
eventList.add(EventType.OBJECT_CREATED_COPY);
QueueConfiguration queueConfiguration = new QueueConfiguration();
queueConfiguration.setQueue("arn:minio:sqs::1:webhook");
queueConfiguration.setEvents(eventList);
queueConfiguration.setPrefixRule("images");
queueConfiguration.setSuffixRule("pg");
List<QueueConfiguration> queueConfigurationList = new LinkedList<>();
queueConfigurationList.add(queueConfiguration);
NotificationConfiguration config = new NotificationConfiguration();
config.setQueueConfigurationList(queueConfigurationList);
minioClient.setBucketNotification(
SetBucketNotificationArgs.builder().bucket("my-bucketname").config(config).build());
//设置规则
// Assume policyJson contains below JSON string;
// {
// "Statement": [
// {
// "Action": [
// "s3:GetBucketLocation",
// "s3:ListBucket"
// ],
// "Effect": "Allow",
// "Principal": "*",
// "Resource": "arn:aws:s3:::my-bucketname"
// },
// {
// "Action": "s3:GetObject",
// "Effect": "Allow",
// "Principal": "*",
// "Resource": "arn:aws:s3:::my-bucketname/myobject*"
// }
// ],
// "Version": "2012-10-17"
// }
//
minioClient.setBucketPolicy(
SetBucketPolicyArgs.builder().bucket("my-bucketname").config(policyJson).build());
//设置副本
Map<String, String> tags = new HashMap<>();
tags.put("key1", "value1");
tags.put("key2", "value2");
ReplicationRule rule =
new ReplicationRule(
new DeleteMarkerReplication(Status.DISABLED),
new ReplicationDestination(
null, null, "REPLACE-WITH-ACTUAL-DESTINATION-BUCKET-ARN", null, null, null, null),
null,
new RuleFilter(new AndOperator("TaxDocs", tags)),
"rule1",
null,
1,
null,
Status.ENABLED);
List<ReplicationRule> rules = new LinkedList<>();
rules.add(rule);
ReplicationConfiguration config =
new ReplicationConfiguration("REPLACE-WITH-ACTUAL-ROLE", rules);
minioClient.setBucketReplication(
SetBucketReplicationArgs.builder().bucket("my-bucketname").config(config).build());
//设置标签
Map<String, String> map = new HashMap<>();
map.put("Project", "Project One");
map.put("User", "jsmith");
minioClient.setBucketTags(SetBucketTagsArgs.builder().bucket("my-bucketname").tags(map).build());
//设置新版本
minioClient.setBucketVersioning(
SetBucketVersioningArgs.builder().bucket("my-bucketname").config(config).build());
//设置对象锁配置
ObjectLockConfiguration config = new ObjectLockConfiguration(RetentionMode.COMPLIANCE, new RetentionDurationDays(100));
minioClient.setObjectLockConfiguration(SetObjectLockConfigurationArgs.builder().bucket("my-bucketname").config(config).build());
三、对象操作
1、对象合并
List<ComposeSource> sourceObjectList = new ArrayList<ComposeSource>();
sourceObjectList.add(
ComposeSource.builder().bucket("my-bucketname").object("新建文本文档.txt").build()
);
sourceObjectList.add(
ComposeSource.builder().bucket("my-bucketname").object("新建文本文档1.txt").build()
);
sourceObjectList.add(
ComposeSource.builder().bucket("my-bucketname").object("新建文本文档2.txt").build()
);
System.out.println(sourceObjectList.size());
//创建对象my-bucketname/composeObject1 组合列表合并的数据
// minioClient.composeObject(
// ComposeObjectArgs.builder()
// .bucket("my-bucketname")
// .object("composeObject1")
// .sources(sourceObjectList)
// .build());
//创建对象my-bucketname/composeObject2 组合列表合并的数据, 用户结合源对象的元数据
Map<String, String> userMetadata = new HashMap<>();
userMetadata.put("My-Project", "ProjectOne");
// minioClient.composeObject(
// ComposeObjectArgs.builder()
// .bucket("my-bucketname")
// .object("composeObject2")
// .sources(sourceObjectList)
// .userMetadata(userMetadata)
// .build());
//加密
ServerSideEncryptionCustomerKey srcSsec =
new ServerSideEncryptionCustomerKey(
new SecretKeySpec(
"01234567890123456789012345678901".getBytes(StandardCharsets.UTF_8), "AES"));
minioClient.composeObject(
ComposeObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.sources(sourceObjectList)
.userMetadata(userMetadata)
// .ssec(srcSsec)
.build());
ServerSideEncryption sse =
new ServerSideEncryptionCustomerKey(
new SecretKeySpec(
"12345678912345678912345678912345".getBytes(StandardCharsets.UTF_8), "AES"));
minioClient.composeObject(
ComposeObjectArgs.builder()
.bucket("my-destination-bucket")
.object("my-destination-object")
.sources(sourceObjectList)
.userMetadata(userMetadata)
.sse(sse)
.build());
2、对象复制
//创建一个对象my-bucketname/my-objectname 数据从my-source-bucketname中复制过来
minioClient.copyObject(
CopyObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
//.srcBucket("my-source-bucketname")
.source(
CopySource.builder()
.bucket("my-source-bucketname")
.build())
.build());
//创建一个对象my-bucketname/my-objectname 数据从my-source-bucketname/my-source-objectname对象中复制过来
minioClient.copyObject(
CopyObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.source(
CopySource.builder()
.bucket("my-source-bucketname")
.object("my-source-objectname")
.build())
.build());
// //创建一个对象my-bucketname/my-objectname 数据从my-source-bucketname中复制过来 数据sse加密
ServerSideEncryption sse =
new ServerSideEncryptionCustomerKey(
new SecretKeySpec(
"12345678912345678912345678912345".getBytes(StandardCharsets.UTF_8), "AES"));
minioClient.copyObject(
CopyObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.source(
CopySource.builder()
.bucket("my-source-bucketname")
.build())
.sse(sse)
.build());
Map<String, String> headersCopy = new HashMap<String,String>();
headersCopy.put("Content-Type","json/plain");
Map<String, String> tag = new HashMap<String,String>();
tag.put("test","miniotest");
tag.put("label","miniotest");
//创建一个对象my-bucketname/my-objectname 数据从my-source-bucketname中复制过来 用户自定义headers信息
minioClient.copyObject(
CopyObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.source(
CopySource.builder()
.bucket("my-source-bucketname")
.build())
.headers(headersCopy)
.tags(tag)
.build());
3、删除对象标签
minioClient.deleteObjectTags(
DeleteObjectTagsArgs.builder().bucket("my-bucketname").object("my-objectname").build());
1
2
4、禁用对象
// Disables legal hold on an object.
minioClient.disableObjectLegalHold(
DisableObjectLegalHoldArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.build());
1
2
3
4
5
6
5、启用对象
// Disables legal hold on an object.
minioClient.enableObjectLegalHold(
EnableObjectLegalHoldArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.build());
6、获取对象
//获得对象流
try (InputStream stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.build())) {
// Read data from stream
}
// 获得对象流 。设置偏移量 seek用法相同
try (InputStream stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.offset(1024L)
.build())) {
// Read data from stream
}
// 获取读取流长度
try (InputStream stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.offset(1024L)
.length(4096L)
.build())) {
// Read data from stream
}
ServerSideEncryptionCustomerKey srcSsec =
new ServerSideEncryptionCustomerKey(
new SecretKeySpec(
"01234567890123456789012345678901".getBytes(StandardCharsets.UTF_8), "AES"));
// 获取读取 sse-c 加密文件
try (InputStream stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.ssec(srcSsec)
.build())) {
// Read data from stream
}
// 获取读取 sse-c 加密文件 带偏移量 带长度
try (InputStream stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.offset(1024L)
.length(4096L)
.ssec(srcSsec)
.build())) {
// Read data from stream
}
7、下载对象
//下载文件到输出流my-filename
minioClient.downloadObject(
DownloadObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.filename("my-filename")
.build());
System.out.println("my-objectname is successfully downloaded to my-filename");
//下载带有密码的文件
ServerSideEncryptionCustomerKey srcSsec =
new ServerSideEncryptionCustomerKey(
new SecretKeySpec(
"01234567890123456789012345678901".getBytes(StandardCharsets.UTF_8), "AES"));
minioClient.downloadObject(
DownloadObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.ssec(srcSsec)
.filename("my-object-file")
.build());
8、获取对象副本
// Object with version id.
Retention retention =
minioClient.getObjectRetention(
GetObjectRetentionArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.versionId("object-version-id")
.build());
System.out.println("mode: " + retention.mode() + "until: " + retention.retainUntilDate());
9、获取对象标签
Tags tags = minioClient.getObjectTags(
GetObjectTagsArgs.builder().bucket("my-bucketname").object("my-objectname").build());
1
2
10、获得预对象路径
String url = minioClient.getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
.method(Method.GET) //get 方法
.bucket("my-bucketname") // 名称
.object("miniotest.txt") //对象名称
.expiry(2, TimeUnit.HOURS) //过期时间
.extraQueryParams(reqParams) //参数配置
.build());
System.out.println(url);
//设置过期时间为1天
String url1 =
minioClient.getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
.method(Method.PUT)
.bucket("my-bucketname")
.object("my-objectname")
.expiry(1, TimeUnit.DAYS) //设置过期1天
.build());
System.out.println(url1);
String url3 =
minioClient.getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
.method(Method.HEAD)
.bucket("my-bucketname")
.object("my-objectname")
.expiry(2, TimeUnit.HOURS)
.extraQueryParams(reqParams)
.build());
System.out.println(url3);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
11、对象是否合法
//查看对象是否可用
boolean status =
minioClient.isObjectLegalHoldEnabled(
IsObjectLegalHoldEnabledArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.versionId("object-versionId")
.build());
if (status) {
System.out.println("Legal hold is on");
}
else {
System.out.println("Legal hold is off");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
12、form-data表达上传数据
//创建一个桶 规则7 天后过期
PostPolicy policy = new PostPolicy("my-bucketname", ZonedDateTime.now().plusDays(7));
//设置对象名
policy.addEqualsCondition("key", "test.jpg");
//设置Content-Type条件
policy.addStartsWithCondition("Content-Type", "image/");
//设置上传文件大小between 64kiB to 10MiB.
policy.addContentLengthRangeCondition(/*64 * 1024*/ 0, 10 * 1024 * 1024);
Map<String, String> formData = minioClient.getPresignedPostFormData(policy);
// Upload an image using POST object with form-data.
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
for (Map.Entry<String, String> entry : formData.entrySet()) {
multipartBuilder.addFormDataPart(entry.getKey(), entry.getValue());
}
multipartBuilder.addFormDataPart("key", "test.jpg");
multipartBuilder.addFormDataPart("Content-Type", "image/png");
multipartBuilder.addFormDataPart(
"file", "test.jpg", RequestBody.create(new File("d://pictures.jpg"), null)
);
Request request =
new Request.Builder()
.url("http://youduk1:9029/my-bucketname")
.post(multipartBuilder.build())
.build();
OkHttpClient httpClient = new OkHttpClient().newBuilder().build();
Response response = httpClient.newCall(request).execute();
if (response.isSuccessful()) {
System.out.println("Pictures/avatar.png is uploaded successfully using POST object");
} else {
System.out.println("Failed to upload Pictures/avatar.png");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
13、上传流数据到对象
//上传已经长度的流
File f = new File("D://pictures.jpg");
InputStream inputStream =new FileInputStream(f);
minioClient.putObject(
PutObjectArgs.builder().bucket("my-bucketname").object("test2.jpg").stream(
inputStream, f.length(), -1)
.contentType("image/jpg")
.build());
//上传未知长度的流
minioClient.putObject(
PutObjectArgs.builder().bucket("my-bucketname").object("test3.jpg").stream(
inputStream, -1, 10485760)
.contentType("image/jpg")
.build());
//创建路径
minioClient.putObject(
PutObjectArgs.builder().bucket("my-bucketname").object("path/to/").stream(
new ByteArrayInputStream(new byte[] {}), 0, -1)
.build());
//上传带有标题和用户元数据的输入流。
Map<String, String> headers = new HashMap<>();
headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY");
Map<String, String> userMetadata = new HashMap<>();
userMetadata.put("My-Project", "Project One");
minioClient.putObject(
PutObjectArgs.builder().bucket("my-bucketname").object("path/to/test4.jpg").stream(
inputStream, f.length(), -1)
.headers(headers)
.userMetadata(userMetadata)
.build());
//上传文件加密
ServerSideEncryption sse =
new ServerSideEncryptionCustomerKey(
new SecretKeySpec(
"12345678912345678912345678912345".getBytes(StandardCharsets.UTF_8), "AES"));
minioClient.putObject(
PutObjectArgs.builder().bucket("my-bucketname").object("my-objectname").stream(
inputStream, f.length(), -1)
.sse(sse)
.build());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
14、上传文件
// 上传json文件.
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket("my-bucketname").object("my-objectname").filename("d://person.json").build());
// 上传mp4文件
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.filename("D:/my-video.avi")
.contentType("video/mp4")
.build());
1
2
3
4
5
6
7
8
9
10
11
12
15、批量上传多个文件
List<SnowballObject> objects = new ArrayList<SnowballObject>();
objects.add(
new SnowballObject(
"my-object-one",
new ByteArrayInputStream("hello".getBytes(StandardCharsets.UTF_8)),
5,
null));
objects.add(
new SnowballObject(
"my-object-two",
new ByteArrayInputStream("java".getBytes(StandardCharsets.UTF_8)),
4,
null));
minioClient.uploadSnowballObjects(
UploadSnowballObjectsArgs.builder().bucket("my-bucketname").objects(objects).build());
16、删除对象
// 删除对象
minioClient.removeObject(
RemoveObjectArgs.builder().bucket("my-bucketname").object("my-objectname").build());
// 删除对应版本的对象
minioClient.removeObject(
RemoveObjectArgs.builder()
.bucket("my-bucketname")
.object("my-versioned-objectname")
.versionId("my-versionid")
.build());
// 绕过治理模式删除版本化对象。
minioClient.removeObject(
RemoveObjectArgs.builder()
.bucket("my-bucketname")
.object("my-versioned-objectname")
.versionId("my-versionid")
.bypassGovernanceMode(true)
.build());
17、批量删除对象
List<DeleteObject> objects = new LinkedList<>();
objects.add(new DeleteObject("my-objectname1"));
objects.add(new DeleteObject("my-objectname2"));
objects.add(new DeleteObject("my-objectname3"));
Iterable<Result<DeleteError>> results =
minioClient.removeObjects(
RemoveObjectsArgs.builder().bucket("my-bucketname").objects(objects).build());
for (Result<DeleteError> result : results) {
DeleteError error = result.get();
System.out.println(
"Error in deleting object " + error.objectName() + "; " + error.message());
}
18、选取对象内容
选择内容对象的SQL表达式
String sqlExpression = "select * from S3Object limit 0,10 ";
InputSerialization is = new InputSerialization(null, false, null, null, FileHeaderInfo.USE, null, null, null);
OutputSerialization os = new OutputSerialization(null, null, null, QuoteFields.ASNEEDED, null);
SelectResponseStream stream =
minioClient.selectObjectContent(
SelectObjectContentArgs.builder()
.bucket("my-bucketname")
.object("miniotest.txt")
.sqlExpression(sqlExpression)
.inputSerialization(is)
.outputSerialization(os)
.requestProgress(true)
.build());
byte[] buf = new byte[512];
int bytesRead = stream.read(buf, 0, buf.length);
System.out.println(new String(buf, 0, bytesRead, StandardCharsets.UTF_8));
Stats stats = stream.stats();
System.out.println("bytes scanned: " + stats.bytesScanned());
System.out.println("bytes processed: " + stats.bytesProcessed());
System.out.println("bytes returned: " + stats.bytesReturned());
stream.close();
19、设置对象保留配置
Retention retention = new Retention(RetentionMode.COMPLIANCE, ZonedDateTime.now().plusYears(1));
minioClient.setObjectRetention(
SetObjectRetentionArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.config(retention)
.bypassGovernanceMode(true)
.build());
1
2
3
4
5
6
7
8
20、为对象设置标签
Map<String, String> map = new HashMap<>();
map.put("Project", "Project One");
map.put("User", "jsmith");
minioClient.setObjectTags(
SetObjectTagsArgs.builder().bucket("my-bucketname").object("miniotest.txt").tags(map).build());
21、获取对象信息和元数据对象
ServerSideEncryptionCustomerKey srcSsec =
new ServerSideEncryptionCustomerKey(
new SecretKeySpec(
"01234567890123456789012345678901".getBytes(StandardCharsets.UTF_8), "AES"));
// 获取对象信息
StatObjectResponse objectStat =
minioClient.statObject(
StatObjectArgs.builder().bucket("my-bucketname").object("my-objectname").build());
// 获取SSE-C加密对象
StatObjectResponse objectStat1 =
minioClient.statObject(
StatObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.ssec(srcSsec)
.build());
// 获取带版本的对象
StatObjectResponse objectStat2 =
minioClient.statObject(
StatObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.versionId("version-id")
.build());
// 获取带版本号的SSE-C加密对象
StatObjectResponse objectStat3 =
minioClient.statObject(
StatObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.versionId("version-id")
.ssec(srcSsec)
.build());
文章知识点与官方知识档案匹配,可进一步学习相关知
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。