Java 客户端 API 参考
创建 MinIO 客户端。
MinIO
MinioClient minioClient =
MinioClient.builder()
.endpoint("https://play.min.io")
.credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
.build();
AWS S3
MinioClient minioClient =
MinioClient.builder()
.endpoint("https://s3.amazonaws.com")
.credentials("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY")
.build();
桶操作 |
对象操作 |
---|---|
1. MinIO 客户端构建器
MinIO 客户端构建器用于创建 MinIO 客户端。构建器具有以下方法来接受参数。
方法 |
描述 |
---|---|
|
接受端点作为字符串、URL 或 okhttp3.HttpUrl 对象,并可以选择接受端口号和标志以启用安全 (TLS) 连接。 |
端点可以像下面这样格式化为字符串 |
|
|
|
|
|
|
|
|
|
|
|
|
接受 S3 服务中帐户的访问密钥(又称用户 ID)和密钥(又称密码)。 |
|
接受 S3 服务的区域名称。如果指定,所有操作都使用此区域,否则会针对每个桶探测区域。 |
|
自定义 HTTP 客户端以覆盖默认值。 |
示例
MinIO
// 1. Create client to S3 service 'play.min.io' at port 443 with TLS security
// for anonymous access.
MinioClient minioClient = MinioClient.builder().endpoint("https://play.min.io").build();
// 2. Create client to S3 service 'play.min.io' at port 443 with TLS security
// using URL object for anonymous access.
MinioClient minioClient = MinioClient.builder().endpoint(new URL("https://play.min.io")).build();
// 3. Create client to S3 service 'play.min.io' at port 9000 with TLS security
// using okhttp3.HttpUrl object for anonymous access.
MinioClient minioClient =
MinioClient.builder().endpoint(HttpUrl.parse("https://play.min.io:9000")).build();
// 4. Create client to S3 service 'play.min.io' at port 443 with TLS security
// for authenticated access.
MinioClient minioClient =
MinioClient.builder()
.endpoint("https://play.min.io")
.credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
.build();
// 5. Create client to S3 service 'play.min.io' at port 9000 with non-TLS security
// for authenticated access.
MinioClient minioClient =
MinioClient.builder()
.endpoint("play.min.io", 9000, false)
.credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
.build();
// 6. Create client to S3 service 'play.min.io' at port 9000 with TLS security
// for authenticated access.
MinioClient minioClient =
MinioClient.builder()
.endpoint("play.min.io", 9000, true)
.credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
.build();
// 7. Create client to S3 service 'play.min.io' at port 443 with TLS security
// and region 'us-west-1' for authenticated access.
MinioClient minioClient =
MinioClient.builder()
.endpoint(new URL("https://play.min.io"))
.credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
.region("us-west-1")
.build();
// 8. Create client to S3 service 'play.min.io' at port 9000 with TLS security,
// region 'eu-east-1' and custom HTTP client for authenticated access.
MinioClient minioClient =
MinioClient.builder()
.endpoint("https://play.min.io:9000")
.credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
.region("eu-east-1")
.httpClient(customHttpClient)
.build();
AWS S3
// 1. Create client to S3 service 's3.amazonaws.com' at port 443 with TLS security
// for anonymous access.
MinioClient s3Client = MinioClient.builder().endpoint("https://s3.amazonaws.com").build();
// 2. Create client to S3 service 's3.amazonaws.com' at port 443 with TLS security
// using URL object for anonymous access.
MinioClient s3Client = MinioClient.builder().endpoint(new URL("https://s3.amazonaws.com")).build();
// 3. Create client to S3 service 's3.amazonaws.com' at port 9000 with TLS security
// using okhttp3.HttpUrl object for anonymous access.
MinioClient s3Client =
MinioClient.builder().endpoint(HttpUrl.parse("https://s3.amazonaws.com")).build();
// 4. Create client to S3 service 's3.amazonaws.com' at port 80 with TLS security
// for authenticated access.
MinioClient s3Client =
MinioClient.builder()
.endpoint("s3.amazonaws.com")
.credentials("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY")
.build();
// 5. Create client to S3 service 's3.amazonaws.com' at port 443 with non-TLS security
// for authenticated access.
MinioClient s3Client =
MinioClient.builder()
.endpoint("s3.amazonaws.com", 433, false)
.credentials("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY")
.build();
// 6. Create client to S3 service 's3.amazonaws.com' at port 80 with non-TLS security
// for authenticated access.
MinioClient s3Client =
MinioClient.builder()
.endpoint("s3.amazonaws.com", 80, false)
.credentials("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY")
.build();
// 7. Create client to S3 service 's3.amazonaws.com' at port 80 with TLS security
// for authenticated access.
MinioClient s3Client =
MinioClient.builder()
.endpoint("s3.amazonaws.com", 80, true)
.credentials("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY")
.build();
// 8. Create client to S3 service 's3.amazonaws.com' at port 80 with non-TLS security
// and region 'us-west-1' for authenticated access.
MinioClient s3Client =
MinioClient.builder()
.endpoint("s3.amazonaws.com", 80, false)
.credentials("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY")
.region("us-west-1")
.build();
// 9. Create client to S3 service 's3.amazonaws.com' at port 443 with TLS security
// and region 'eu-west-2' for authenticated access.
MinioClient s3Client =
MinioClient.builder()
.endpoint("s3.amazonaws.com", 443, true)
.credentials("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY").
.region("eu-west-2")
.build();
// 10. Create client to S3 service 's3.amazonaws.com' at port 443 with TLS security,
// region 'eu-central-1' and custom HTTP client for authenticated access.
MinioClient s3Client =
MinioClient.builder()
.endpoint("s3.amazonaws.com", 443, true)
.credentials("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY")
.region("eu-central-1")
.httpClient(customHttpClient)
.build();
常见异常
除特定于 API 的异常外,所有 API 都抛出以下异常。
异常 |
原因 |
---|---|
ErrorResponseException |
抛出以指示 S3 服务返回了错误响应。 |
IllegalArgumentException |
抛出以指示传递了无效参数。 |
InsufficientDataException |
抛出以指示 InputStream 中没有足够的数据可用。 |
InternalException |
抛出以指示内部库错误。 |
InvalidKeyException |
抛出以指示缺少 HMAC SHA-256 库。 |
InvalidResponseException |
抛出以指示 S3 服务返回了无效或无错误响应。 |
IOException |
抛出以指示 S3 操作上的 I/O 错误。 |
NoSuchAlgorithmException |
抛出以指示缺少 MD5 或 SHA-256 摘要库。 |
服务器异常 |
指示 HTTP 服务器错误。 |
Xml解析异常 |
指示 XML 解析错误。 |
2. 桶操作
bucketExists(BucketExistsArgs args)
public boolean bucketExists(BucketExistsArgs args)
[Javadoc]
检查桶是否存在。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
boolean - 桶存在则返回 True。 |
示例
// Check whether 'my-bucketname' exists or not.
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");
}
deleteBucketEncryption(DeleteBucketEncryptionArgs args)
private void deleteBucketEncryption(DeleteBucketEncryptionArgs args)
[Javadoc]
删除桶的加密配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
minioClient.deleteBucketEncryption(
DeleteBucketEncryptionArgs.builder().bucket("my-bucketname").build());
deleteBucketLifecycle(DeleteBucketLifecycleArgs args)
private void deleteBucketLifecycle(DeleteBucketLifecycleArgs args)
[Javadoc]
删除桶的生命周期配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
minioClient.deleteBucketLifecycle(
DeleteBucketLifecycleArgs.builder().bucket("my-bucketname").build());
deleteBucketPolicy(DeleteBucketPolicyArgs args)
private void deleteBucketPolicy(DeleteBucketPolicyArgs args)
[Javadoc]
删除桶的策略配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
minioClient.deleteBucketPolicy(DeleteBucketPolicyArgs.builder().bucket("my-bucketname").build());
deleteBucketReplication(DeleteBucketReplicationArgs args)
private void deleteBucketReplication(DeleteBucketReplicationArgs args)
[Javadoc]
删除桶的复制配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
minioClient.deleteBucketReplication(
DeleteBucketReplicationArgs.builder().bucket("my-bucketname").build());
deleteBucketNotification(DeleteBucketNotificationArgs args)
public void deleteBucketNotification(DeleteBucketNotificationArgs args)
[Javadoc]
删除桶的通知配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
minioClient.deleteBucketNotification(
DeleteBucketNotificationArgs.builder().bucket("my-bucketname").build());
deleteObjectLockConfiguration(DeleteObjectLockConfigurationArgs args)
public void deleteObjectLockConfiguration(DeleteObjectLockConfigurationArgs args)
[Javadoc]
删除桶中的对象锁配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
minioClient.deleteObjectLockConfiguration(
DeleteObjectLockConfigurationArgs.builder().bucket("my-bucketname").build());
getBucketEncryption(GetBucketEncryptionArgs args)
public SseConfiguration getBucketEncryption(GetBucketEncryptionArgs args)
[Javadoc]
获取桶的加密配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
SseConfiguration - 服务器端加密配置。 |
示例
SseConfiguration config =
minioClient.getBucketEncryption(
GetBucketEncryptionArgs.builder().bucket("my-bucketname").build());
getBucketLifecycle(GetBucketLifecycleArgs args)
public LifecycleConfiguration getBucketLifecycle(GetBucketLifecycleArgs args)
[Javadoc]
获取桶的生命周期配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
LifecycleConfiguration - 生命周期配置。 |
示例
LifecycleConfiguration config =
minioClient.getBucketLifecycle(
GetBucketLifecycleArgs.builder().bucket("my-bucketname").build());
System.out.println("Lifecycle configuration: " + config);
getBucketNotification(GetBucketNotificationArgs args)
public NotificationConfiguration getBucketNotification(GetBucketNotificationArgs args)
[Javadoc]
获取桶的通知配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
NotificationConfiguration - 通知配置。 |
示例
NotificationConfiguration config =
minioClient.getBucketNotification(
GetBucketNotificationArgs.builder().bucket("my-bucketname").build());
getBucketPolicy(GetBucketPolicyArgs args)
public String getBucketPolicy(GetBucketPolicyArgs args)
[Javadoc]
获取桶的策略配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
String - 桶策略配置作为 JSON 字符串。 |
示例
String config =
minioClient.getBucketPolicy(GetBucketPolicyArgs.builder().bucket("my-bucketname").build());
getBucketReplication(GetBucketReplicationArgs args)
public ReplicationConfiguration getBucketReplication(GetBucketReplicationArgs args)
[Javadoc]
获取桶的复制配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
ReplicationConfiguration - 桶复制配置。 |
示例
ReplicationConfiguration config =
minioClient.getBucketReplication(
GetBucketReplicationArgs.builder().bucket("my-bucketname").build());
getBucketVersioning(GetBucketVersioningArgs args)
public VersioningConfiguration getBucketVersioning(GetBucketVersioningArgs args)
[Javadoc]
获取桶的版本控制配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
VersioningConfiguration - 版本控制配置。 |
示例
VersioningConfiguration config =
minioClient.getBucketVersioning(
GetBucketVersioningArgs.builder().bucket("my-bucketname").build());
getObjectLockConfiguration(GetObjectLockConfigurationArgs args)
public ObjectLockConfiguration getObjectLockConfiguration(GetObjectLockConfigurationArgs args)
[Javadoc]
获取桶中的对象锁配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
ObjectLockConfiguration - 默认保留配置。 |
示例
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());
listBuckets()
public List<Bucket> listBuckets()
[Javadoc]
列出所有桶的桶信息。
返回值 |
---|
List<Bucket> - 桶信息列表。 |
示例
List<Bucket> bucketList = minioClient.listBuckets();
for (Bucket bucket : bucketList) {
System.out.println(bucket.creationDate() + ", " + bucket.name());
}
listBuckets(ListBucketsArgs args)
public List<Bucket> listBuckets(ListBucketsArgs args)
[Javadoc]
列出所有桶的桶信息。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
List<Bucket> - 桶信息列表。 |
示例
List<Bucket> bucketList =
minioClient.listBuckets(ListBuckets.builder().extraHeaders(headers).build());
for (Bucket bucket : bucketList) {
System.out.println(bucket.creationDate() + ", " + bucket.name());
}
listenBucketNotification(ListenBucketNotificationArgs args)
public CloseableIterator<Result<NotificationRecords>> listenBucketNotification(ListenBucketNotificationArgs args)
[Javadoc]
监听桶的对象前缀和后缀的事件。返回的可关闭迭代器是惰性求值的,因此需要进行迭代才能获取新记录,并且必须与 try-with-resource 一起使用才能释放底层网络资源。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
CloseableIterator<Result<NotificationRecords>> - 惰性可关闭迭代器包含事件记录。 |
示例
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());
}
}
}
listObjects(ListObjectsArgs args)
public Iterable<Result<Item>> listObjects(ListObjectsArgs args)
[Javadoc]
列出存储桶中的对象信息。
参数
参数 |
类型 |
描述 |
---|---|---|
|
列出对象的的参数 |
返回值 |
---|
示例
// Lists objects information.
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket("my-bucketname").build());
// Lists objects information recursively.
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket("my-bucketname").recursive(true).build());
// Lists maximum 100 objects information whose names starts with 'E' and after 'ExampleGuide.pdf'.
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder()
.bucket("my-bucketname")
.startAfter("ExampleGuide.pdf")
.prefix("E")
.maxKeys(100)
.build());
// Lists maximum 100 objects information with version whose names starts with 'E' and after
// 'ExampleGuide.pdf'.
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder()
.bucket("my-bucketname")
.startAfter("ExampleGuide.pdf")
.prefix("E")
.maxKeys(100)
.includeVersions(true)
.build());
makeBucket(MakeBucketArgs args)
public void makeBucket(MakeBucketArgs args)
[Javadoc]
创建存储桶,并启用给定的区域和对象锁定功能。
参数
参数 |
类型 |
描述 |
---|---|---|
|
创建存储桶的参数 |
示例
// Create bucket with default region.
minioClient.makeBucket(
MakeBucketArgs.builder()
.bucket("my-bucketname")
.build());
// Create bucket with specific region.
minioClient.makeBucket(
MakeBucketArgs.builder()
.bucket("my-bucketname")
.region("us-west-1")
.build());
// Create object-lock enabled bucket with specific region.
minioClient.makeBucket(
MakeBucketArgs.builder()
.bucket("my-bucketname")
.region("us-west-1")
.objectLock(true)
.build());
removeBucket(RemoveBucketArgs args)
public void removeBucket(RemoveBucketArgs args)
[Javadoc]
删除空存储桶。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
setBucketEncryption(SetBucketEncryptionArgs args)
public void setBucketEncryption(SetBucketEncryptionArgs args)
[Javadoc]
设置存储桶的加密配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
minioClient.setBucketEncryption(
SetBucketEncryptionArgs.builder().bucket("my-bucketname").config(config).build());
setBucketLifecycle(SetBucketLifecycleArgs args)
public void setBucketLifecycle(SetBucketLifecycleArgs args)
[Javadoc]
为存储桶设置生命周期配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
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());
setBucketNotification(SetBucketNotificationArgs args)
public void setBucketNotification(SetBucketNotificationArgs args)
[Javadoc]
为存储桶设置通知配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
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());
setBucketPolicy(SetBucketPolicyArgs args)
public void setBucketPolicy(SetBucketPolicyArgs args)
[Javadoc]
为存储桶设置存储桶策略配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
// 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());
setBucketReplication(SetBucketReplicationArgs args)
public void setBucketReplication(SetBucketReplicationArgs args)
[Javadoc]
为存储桶设置存储桶复制配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
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());
setBucketVersioning(SetBucketVersioningArgs args)
public void setBucketVersioning(SetBucketVersioningArgs args)
[Javadoc]
设置存储桶的版本控制配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
minioClient.setBucketVersioning(
SetBucketVersioningArgs.builder().bucket("my-bucketname").config(config).build());
setObjectLockConfiguration(SetObjectLockConfigurationArgs args)
public void setObjectLockConfiguration(SetObjectLockConfigurationArgs args)
[Javadoc]
在存储桶中设置对象锁定配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
ObjectLockConfiguration config =
new ObjectLockConfiguration(RetentionMode.COMPLIANCE, new RetentionDurationDays(100));
minioClient.setObjectLockConfiguration(
SetObjectLockConfigurationArgs.builder().bucket("my-bucketname").config(config).build());
3. 对象操作
composeObject(ComposeObjectArgs args)
public ObjectWriteResponse composeObject(ComposeObjectArgs args)
[Javadoc]
通过使用服务器端复制将来自不同源对象的数据组合起来创建对象。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
ObjectWriteResponse - 包含创建对象的的信息。 |
示例
List<ComposeSource> sourceObjectList = new ArrayList<ComposeSource>();
sourceObjectList.add(
ComposeSource.builder().bucket("my-job-bucket").object("my-objectname-part-one").build());
sourceObjectList.add(
ComposeSource.builder().bucket("my-job-bucket").object("my-objectname-part-two").build());
sourceObjectList.add(
ComposeSource.builder().bucket("my-job-bucket").object("my-objectname-part-three").build());
// Create my-bucketname/my-objectname by combining source object list.
minioClient.composeObject(
ComposeObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.sources(sourceObjectList)
.build());
// Create my-bucketname/my-objectname with user metadata by combining source object
// list.
Map<String, String> userMetadata = new HashMap<>();
userMetadata.put("My-Project", "Project One");
minioClient.composeObject(
ComposeObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.sources(sourceObjectList)
.userMetadata(userMetadata)
.build());
// Create my-bucketname/my-objectname with user metadata and server-side encryption
// by combining source object list.
minioClient.composeObject(
ComposeObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.sources(sourceObjectList)
.userMetadata(userMetadata)
.ssec(sse)
.build());
copyObject(CopyObjectArgs args)
public ObjectWriteResponse copyObject(CopyObjectArgs args)
[Javadoc]
通过将数据从另一个对象服务器端复制创建对象。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
ObjectWriteResponse - 包含创建对象的的信息。 |
示例
// Create object "my-objectname" in bucket "my-bucketname" by copying from object
// "my-objectname" in bucket "my-source-bucketname".
minioClient.copyObject(
CopyObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.source(
CopySource.builder()
.bucket("my-source-bucketname")
.object("my-objectname")
.build())
.build());
// Create object "my-objectname" in bucket "my-bucketname" by copying from object
// "my-source-objectname" in bucket "my-source-bucketname".
minioClient.copyObject(
CopyObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.source(
CopySource.builder()
.bucket("my-source-bucketname")
.object("my-source-objectname")
.build())
.build());
// Create object "my-objectname" in bucket "my-bucketname" with SSE-KMS server-side
// encryption by copying from object "my-objectname" in bucket "my-source-bucketname".
minioClient.copyObject(
CopyObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.source(
CopySource.builder()
.bucket("my-source-bucketname")
.object("my-objectname")
.build())
.sse(sseKms) // Replace with actual key.
.build());
// Create object "my-objectname" in bucket "my-bucketname" with SSE-S3 server-side
// encryption by copying from object "my-objectname" in bucket "my-source-bucketname".
minioClient.copyObject(
CopyObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.source(
CopySource.builder()
.bucket("my-source-bucketname")
.object("my-objectname")
.build())
.sse(sseS3) // Replace with actual key.
.build());
// Create object "my-objectname" in bucket "my-bucketname" with SSE-C server-side encryption
// by copying from object "my-objectname" in bucket "my-source-bucketname".
minioClient.copyObject(
CopyObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.source(
CopySource.builder()
.bucket("my-source-bucketname")
.object("my-objectname")
.build())
.sse(ssec) // Replace with actual key.
.build());
// Create object "my-objectname" in bucket "my-bucketname" by copying from SSE-C encrypted
// object "my-source-objectname" in bucket "my-source-bucketname".
minioClient.copyObject(
CopyObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.source(
CopySource.builder()
.bucket("my-source-bucketname")
.object("my-source-objectname")
.ssec(ssec) // Replace with actual key.
.build())
.build());
// Create object "my-objectname" in bucket "my-bucketname" with custom headers conditionally
// by copying from object "my-objectname" in bucket "my-source-bucketname".
minioClient.copyObject(
CopyObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.source(
CopySource.builder()
.bucket("my-source-bucketname")
.object("my-objectname")
.matchETag(etag) // Replace with actual etag.
.build())
.headers(headers) // Replace with actual headers.
.build());
disableObjectLegalHold(DisableObjectLegalHoldArgs args)
public void disableObjectLegalHold(DisableObjectLegalHoldArgs args)
[Javadoc]
禁用对象的法律保留。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
// Disables legal hold on an object.
minioClient.disableObjectLegalHold(
DisableObjectLegalHoldArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.build());
enableObjectLegalHold(EnableObjectLegalHoldArgs args)
public void enableObjectLegalHold(EnableObjectLegalHoldArgs args)
[Javadoc]
启用对象的法律保留。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
// Disables legal hold on an object.
minioClient.enableObjectLegalHold(
EnableObjectLegalHoldArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.build());
getObject(GetObjectArgs args)
public InputStream getObject(GetObjectArgs args)
[Javadoc]
获取对象的 data。返回的 InputStream
在使用后必须关闭以释放网络资源。
参数
参数 |
类型 |
描述 |
---|---|---|
|
GetObjectArgs |
参数。 |
返回值 |
---|
InputStream - 包含对象 data。 |
示例
// get object given the bucket and object name
try (InputStream stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.build())) {
// Read data from stream
}
// get object data from offset
try (InputStream stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.offset(1024L)
.build())) {
// Read data from stream
}
// get object data from offset to length
try (InputStream stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.offset(1024L)
.length(4096L)
.build())) {
// Read data from stream
}
// get data of an SSE-C encrypted object
try (InputStream stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.ssec(ssec)
.build())) {
// Read data from stream
}
// get object data from offset to length of an SSE-C encrypted object
try (InputStream stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.offset(1024L)
.length(4096L)
.ssec(ssec)
.build())) {
// Read data from stream
}
downloadObject(DownloadObjectArgs args)
public void downloadObject(DownloadObjectArgs args)
[Javadoc]
将对象的 data 下载到文件。
参数
参数 |
类型 |
描述 |
---|---|---|
|
DownloadObjectArgs |
参数。 |
示例
// Download object given the bucket, object name and output file name
minioClient.downloadObject(
DownloadObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.filename("my-object-file")
.build());
// Download server-side encrypted object in bucket to given file name
minioClient.downloadObject(
DownloadObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.ssec(ssec)
.filename("my-object-file")
.build());
getObjectRetention(GetObjectRetentionArgs args)
public Retention getObjectRetention(GetObjectRetentionArgs args)
[Javadoc]
获取对象的保留配置。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
Retention - 对象保留配置。 |
示例
// 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());
getPresignedObjectUrl(GetPresignedObjectUrlArgs args)
public String getPresignedObjectUrl(GetPresignedObjectUrlArgs args)
[Javadoc]
获取对象的预签名 URL,用于 HTTP 方法、过期时间和自定义请求参数。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
String - URL 字符串。 |
示例
// Get presigned URL string to download 'my-objectname' in 'my-bucketname'
// with an expiration of 2 hours.
//
// Additionally also add 'response-content-type' to dynamically set content-type
// for the server response.
Map<String, String> reqParams = new HashMap<String, String>();
reqParams.put("response-content-type", "application/json");
String url =
minioClient.getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
.method(Method.GET)
.bucket("my-bucketname")
.object("my-objectname")
.expiry(2, TimeUnit.HOURS)
.extraQueryParams(reqParams)
.build());
System.out.println(url);
// Get presigned URL string to upload 'my-objectname' in 'my-bucketname'
// with an expiration of 1 day.
String url =
minioClient.getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
.method(Method.PUT)
.bucket("my-bucketname")
.object("my-objectname")
.expiry(1, TimeUnit.DAYS)
.build());
System.out.println(url);
// Get presigned URL string to lookup metadata for 'my-objectname' in 'my-bucketname'
// with an expiration of 2 hours.
//
// Additionally also add 'response-content-type' to dynamically set content-type
// for the server metadata response.
Map<String, String> reqParams = new HashMap<String, String>();
reqParams.put("response-content-type", "application/json");
String url =
minioClient.getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
.method(Method.HEAD)
.bucket("my-bucketname")
.object("my-objectname")
.expiry(2, TimeUnit.HOURS)
.extraQueryParams(reqParams)
.build());
System.out.println(url);
isObjectLegalHoldEnabled(IsObjectLegalHoldEnabledArgs args)
public boolean isObjectLegalHoldEnabled(IsObjectLegalHoldEnabledArgs args)
[Javadoc]
如果对象的法律保留已启用,则返回 true。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
boolean - 如果法律保留已启用,则为 true。 |
示例
boolean status =
s3Client.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");
}
getPresignedPostFormData(PostPolicy policy)
public Map<String,String> getPresignedPostFormData(PostPolicy policy)
[Javadoc]
获取对象的 PostPolicy 的 form-data,以便使用 POST 方法上传其 data。
参数
参数 |
类型 |
描述 |
---|---|---|
|
对象的 POST 策略。 |
返回值 |
---|
Map<String, String> - 包含使用 POST 方法上传对象的 form-data。 |
示例
// Create new post policy for 'my-bucketname' with 7 days expiry from now.
PostPolicy policy = new PostPolicy("my-bucketname", ZonedDateTime.now().plusDays(7));
// Add condition that 'key' (object name) equals to 'my-objectname'.
policy.addEqualsCondition("key", "my-objectname");
// Add condition that 'Content-Type' starts with 'image/'.
policy.addStartsWithCondition("Content-Type", "image/");
// Add condition that 'content-length-range' is between 64kiB to 10MiB.
policy.addContentLengthRangeCondition(64 * 1024, 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", "my-objectname");
multipartBuilder.addFormDataPart("Content-Type", "image/png");
// "file" must be added at last.
multipartBuilder.addFormDataPart(
"file", "my-objectname", RequestBody.create(new File("Pictures/avatar.png"), null));
Request request =
new Request.Builder()
.url("https://play.min.io/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");
}
putObject(PutObjectArgs args)
public ObjectWriteResponse putObject(PutObjectArgs args)
[Javadoc]
将给定的流作为存储桶中的对象上传。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
ObjectWriteResponse - 包含创建对象的的信息。 |
示例
// Upload known sized input stream.
minioClient.putObject(
PutObjectArgs.builder().bucket("my-bucketname").object("my-objectname").stream(
inputStream, size, -1)
.contentType("video/mp4")
.build());
// Upload unknown sized input stream.
minioClient.putObject(
PutObjectArgs.builder().bucket("my-bucketname").object("my-objectname").stream(
inputStream, -1, 10485760)
.contentType("video/mp4")
.build());
// Create object ends with '/' (also called as folder or directory).
minioClient.putObject(
PutObjectArgs.builder().bucket("my-bucketname").object("path/to/").stream(
new ByteArrayInputStream(new byte[] {}), 0, -1)
.build());
// Upload input stream with headers and user metadata.
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("my-objectname").stream(
inputStream, size, -1)
.headers(headers)
.userMetadata(userMetadata)
.build());
// Upload input stream with server-side encryption.
minioClient.putObject(
PutObjectArgs.builder().bucket("my-bucketname").object("my-objectname").stream(
inputStream, size, -1)
.sse(sse)
.build());
uploadObject(UploadObjectArgs args)
public void uploadObject(UploadObjectArgs args)
[Javadoc]
将文件内容上传到存储桶中的对象。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
// Upload an JSON file.
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket("my-bucketname").object("my-objectname").filename("person.json").build());
// Upload a video file.
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.filename("my-video.avi")
.contentType("video/mp4")
.build());
uploadSnowballObjects(UploadSnowballObjectsArgs args)
public void uploadSnowballObjects(UploadSnowballObjectsArgs args)
[Javadoc]
通过创建中间 TAR 文件(可选压缩)并将其上传到 S3 服务来在一个 put 调用中上传多个对象。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
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());
removeObject(RemoveObjectArgs args)
public void removeObject(RemoveObjectArgs args)
[Javadoc]
删除对象。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
// Remove object.
minioClient.removeObject(
RemoveObjectArgs.builder().bucket("my-bucketname").object("my-objectname").build());
// Remove versioned object.
minioClient.removeObject(
RemoveObjectArgs.builder()
.bucket("my-bucketname")
.object("my-versioned-objectname")
.versionId("my-versionid")
.build());
// Remove versioned object bypassing Governance mode.
minioClient.removeObject(
RemoveObjectArgs.builder()
.bucket("my-bucketname")
.object("my-versioned-objectname")
.versionId("my-versionid")
.bypassRetentionMode(true)
.build());
removeObjects(RemoveObjectsArgs args)
public Iterable<Result<DeleteError>> removeObjects(RemoveObjectsArgs args)
[Javadoc]
延迟删除多个对象。需要迭代返回的 Iterable 来执行删除操作。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
Iterable<Result<DeleteError>> - 延迟迭代器包含对象删除状态。 |
示例
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());
}
restoreObject(RestoreObjectArgs args)
public void restoreObject(RestoreObjectArgs args)
[Javadoc]
恢复对象。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
// Restore object.
minioClient.restoreObject(
RestoreObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.request(new RestoreRequest(null, null, null, null, null, null))
.build());
// Restore versioned object.
minioClient.restoreObject(
RestoreObjectArgs.builder()
.bucket("my-bucketname")
.object("my-versioned-objectname")
.versionId("my-versionid")
.request(new RestoreRequest(null, null, null, null, null, null))
.build());
selectObjectContent(SelectObjectContentArgs args)
public SelectResponseStream selectObjectContent(SelectObjectContentArgs args)
[Javadoc]
使用 SQL 表达式选择对象的內容。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
SelectResponseStream - 包含过滤后的记录和进度。 |
示例
String sqlExpression = "select * from S3Object";
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("my-objectName")
.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();
setObjectRetention(SetObjectRetentionArgs args)
public void setObjectLockRetention(SetObjectRetentionArgs)
[Javadoc]
将保留配置设置为对象。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
示例
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());
statObject(StatObjectArgs args)
public ObjectStat statObject(StatObjectArgs args)
[Javadoc]
获取对象的详细信息和元数据。
参数
参数 |
类型 |
描述 |
---|---|---|
|
参数。 |
返回值 |
---|
ObjectStat - 已填充的详细信息和元数据。 |
示例
// Get information of an object.
ObjectStat objectStat =
minioClient.statObject(
StatObjectArgs.builder().bucket("my-bucketname").object("my-objectname").build());
// Get information of SSE-C encrypted object.
ObjectStat objectStat =
minioClient.statObject(
StatObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.ssec(ssec)
.build());
// Get information of a versioned object.
ObjectStat objectStat =
minioClient.statObject(
StatObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.versionId("version-id")
.build());
// Get information of a SSE-C encrypted versioned object.
ObjectStat objectStat =
minioClient.statObject(
StatObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.versionId("version-id")
.ssec(ssec)
.build());