Skip to content

Commit

Permalink
add arg builder to {set,get}DefaultRetention APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
balamurugana committed Jun 3, 2020
1 parent 5974546 commit 2250265
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 118 deletions.
27 changes: 27 additions & 0 deletions api/src/main/java/io/minio/GetDefaultRetentionArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.minio;

/** Argument class of MinioClient.getDefaultRetention(). */
public class GetDefaultRetentionArgs extends BucketArgs {
public static Builder builder() {
return new Builder();
}

/** Argument builder of {@link GetDefaultRetentionArgs}. */
public static final class Builder extends BucketArgs.Builder<Builder, GetDefaultRetentionArgs> {}
}
86 changes: 78 additions & 8 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3990,25 +3990,59 @@ public boolean isVersioningEnabled(IsVersioningEnabledArgs args)
* @throws IOException thrown to indicate I/O error on S3 operation.
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
* @deprecated use {@link #setDefaultRetention(SetDefaultRetentionArgs)}
*/
@Deprecated
public void setDefaultRetention(String bucketName, ObjectLockConfiguration config)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, ServerException,
XmlParserException {
setDefaultRetention(
SetDefaultRetentionArgs.builder().bucket(bucketName).config(config).build());
}

/**
* Sets default object retention in a bucket.
*
* <pre>Example:{@code
* ObjectLockConfiguration config = new ObjectLockConfiguration(
* RetentionMode.COMPLIANCE, new RetentionDurationDays(100));
* minioClient.setDefaultRetention(
* SetDefaultRetentionArgs.builder().bucket("my-bucketname").config(config).build());
* }</pre>
*
* @param args {@link SetDefaultRetentionArgs} object.
* @throws ErrorResponseException thrown to indicate S3 service returned an error response.
* @throws IllegalArgumentException throws to indicate invalid argument passed.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @throws InvalidBucketNameException thrown to indicate invalid bucket name passed.
* @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
* @throws InvalidResponseException thrown to indicate S3 service returned invalid or no error
* response.
* @throws IOException thrown to indicate I/O error on S3 operation.
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
public void setDefaultRetention(SetDefaultRetentionArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, ServerException,
XmlParserException {
checkArgs(args);

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("object-lock", "");

Response response = executePut(bucketName, null, null, queryParamMap, config, 0);
response.body().close();
Response response = executePut(args.bucket(), null, null, queryParamMap, args.config(), 0);
response.close();
}

/**
* Gets default object retention in a bucket.
*
* <pre>Example:{@code
* // bucket must be created with object lock enabled.
* minioClient.makeBucket("my-bucketname", null, true);
* ObjectLockConfiguration config = minioClient.getDefaultRetention("my-bucketname");
* System.out.println("Mode: " + config.mode());
* System.out.println(
Expand All @@ -4028,19 +4062,55 @@ public void setDefaultRetention(String bucketName, ObjectLockConfiguration confi
* @throws IOException thrown to indicate I/O error on S3 operation.
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
* @deprecated use {@link #getDefaultRetention(GetDefaultRetentionArgs)}
*/
@Deprecated
public ObjectLockConfiguration getDefaultRetention(String bucketName)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, ServerException,
XmlParserException {
return getDefaultRetention(GetDefaultRetentionArgs.builder().bucket(bucketName).build());
}

/**
* Gets default object retention in a bucket.
*
* <pre>Example:{@code
* ObjectLockConfiguration config =
* minioClient.getDefaultRetention(
* GetDefaultRetentionArgs.builder().bucket("my-bucketname").build());
* System.out.println("Mode: " + config.mode());
* System.out.println(
* "Duration: " + config.duration().duration() + " " + config.duration().unit());
* }</pre>
*
* @param args {@link GetDefaultRetentionArgs} object.
* @return {@link ObjectLockConfiguration} - Default retention configuration.
* @throws ErrorResponseException thrown to indicate S3 service returned an error response.
* @throws IllegalArgumentException throws to indicate invalid argument passed.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @throws InvalidBucketNameException thrown to indicate invalid bucket name passed.
* @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
* @throws InvalidResponseException thrown to indicate S3 service returned invalid or no error
* response.
* @throws IOException thrown to indicate I/O error on S3 operation.
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
public ObjectLockConfiguration getDefaultRetention(GetDefaultRetentionArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, ServerException,
XmlParserException {
checkArgs(args);

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("object-lock", "");

Response response = executeGet(bucketName, null, null, queryParamMap);

try (ResponseBody body = response.body()) {
return Xml.unmarshal(ObjectLockConfiguration.class, body.charStream());
try (Response response = executeGet(args.bucket(), null, null, queryParamMap)) {
return Xml.unmarshal(ObjectLockConfiguration.class, response.body().charStream());
}
}

Expand Down
52 changes: 52 additions & 0 deletions api/src/main/java/io/minio/SetDefaultRetentionArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.minio;

import io.minio.messages.ObjectLockConfiguration;

/** Argument class of MinioClient.setDefaultRetention(). */
public class SetDefaultRetentionArgs extends BucketArgs {
private ObjectLockConfiguration config;

public ObjectLockConfiguration config() {
return config;
}

public static Builder builder() {
return new Builder();
}

/** Argument builder of {@link SetDefaultRetentionArgs}. */
public static final class Builder extends BucketArgs.Builder<Builder, SetDefaultRetentionArgs> {
private void validateConfig(ObjectLockConfiguration config) {
if (config == null) {
throw new IllegalArgumentException("null object-lock configuration");
}
}

protected void validate(SetDefaultRetentionArgs args) {
super.validate(args);
validateConfig(args.config);
}

public Builder config(ObjectLockConfiguration config) {
validateConfig(config);
operations.add(args -> args.config = config);
return this;
}
}
}
32 changes: 17 additions & 15 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,26 +546,26 @@ Tags tags = minioClient.getBucketTags(GetBucketTagsArgs.builder().bucket("my-buc
```

<a name="getDefaultRetention"></a>
### getDefaultRetention(String bucketName)
`public ObjectLockConfiguration getDefaultRetention(String bucketName)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#getDefaultRetention-java.lang.String-)_
### getDefaultRetention(GetDefaultRetentionArgs args)
`public ObjectLockConfiguration getDefaultRetention(GetDefaultRetentionArgs args)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#getDefaultRetention-io.minio.GetDefaultRetentionArgs-)_

Gets default object retention in a bucket.

__Parameters__

| Parameter | Type | Description |
|:---------------|:---------|:--------------------|
| ``bucketName`` | _String_ | Name of the bucket. |
| Parameter | Type | Description |
|:----------|:----------------------------|:------------|
| ``args`` | _[GetDefaultRetentionArgs]_ | Arguments. |

| Returns |
|:---------------------------------------------------------------|
| _[ObjectLockConfiguration]_ - Default retention configuration. |

__Example__
```java
// bucket must be created with object lock enabled.
minioClient.makeBucket("my-bucketname", null, true);
ObjectLockConfiguration config = minioClient.getDefaultRetention("my-bucketname");
ObjectLockConfiguration config =
minioClient.getDefaultRetention(
GetDefaultRetentionArgs.builder().bucket("my-bucketname").build());
System.out.println("Mode: " + config.mode());
System.out.println("Duration: " + config.duration().duration() + " " + config.duration().unit());
```
Expand Down Expand Up @@ -942,22 +942,22 @@ minioClient.setBucketTags(SetBucketTagsArgs.builder().bucket("my-bucketname").ta
```

<a name="setDefaultRetention"></a>
### setDefaultRetention(String bucketName, ObjectLockConfiguration config)
`public void setDefaultRetention(String bucketName, ObjectLockConfiguration config)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#setDefaultRetention-java.lang.String-io.minio.messages.ObjectLockConfiguration-)_
### setDefaultRetention(SetDefaultRetentionArgs args)
`public void setDefaultRetention(SetDefaultRetentionArgs args)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#setDefaultRetention-io.minio.SetDefaultRetentionArgs-)_

Sets default object retention in a bucket.

__Parameters__
| Parameter | Type | Description |
|:---------------|:----------------------------|:---------------------------|
| ``bucketName`` | _String_ | Name of the bucket. |
| ``config`` | _[ObjectLockConfiguration]_ | Object lock Configuration. |
| Parameter | Type | Description |
|:----------|:----------------------------|:------------|
| ``args`` | _[SetDefaultRetentionArgs]_ | Arguments. |

__Example__
```java
ObjectLockConfiguration config =
new ObjectLockConfiguration(RetentionMode.COMPLIANCE, new RetentionDurationDays(100));
minioClient.setDefaultRetention("my-bucketname", config);
minioClient.setDefaultRetention(
SetDefaultRetentionArgs.builder().bucket("my-bucketname").config(config).build());
```

## 3. Object operations
Expand Down Expand Up @@ -1800,3 +1800,5 @@ ObjectStat objectStat =
[SetBucketNotificationArgs]: http://minio.github.io/minio-java/io/minio/SetBucketNotificationArgs.html
[ListenBucketNotificationArgs]: http://minio.github.io/minio-java/io/minio/ListenBucketNotificationArgs.html
[SelectObjectContentArgs]: http://minio.github.io/minio-java/io/minio/SelectObjectContentArgs.html
[GetDefaultRetentionArgs]: http://minio.github.io/minio-java/io/minio/GetDefaultRetentionArgs.html
[SetDefaultRetentionArgs]: http://minio.github.io/minio-java/io/minio/SetDefaultRetentionArgs.html
45 changes: 45 additions & 0 deletions examples/GetDefaultRetention.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2019 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import io.minio.GetDefaultRetentionArgs;
import io.minio.MinioClient;
import io.minio.errors.MinioException;
import io.minio.messages.ObjectLockConfiguration;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class GetDefaultRetention {
/** MinioClient.getDefaultRetention() example. */
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try {
/* Amazon S3: */
MinioClient s3Client =
new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY");

ObjectLockConfiguration config =
s3Client.getDefaultRetention(
GetDefaultRetentionArgs.builder().bucket("my-lock-enabled-bucketname").build());

System.out.println("Default retention configuration of bucket");
System.out.println("Mode: " + config.mode());
System.out.println("Duration: " + config.duration());
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
* limitations under the License.
*/

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.SetDefaultRetentionArgs;
import io.minio.errors.MinioException;
import io.minio.messages.ObjectLockConfiguration;
import io.minio.messages.RetentionDurationDays;
Expand All @@ -25,41 +24,26 @@
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class SetGetBucketObjectLockConfig {
/** Set/Get Bucket Object Lock configuration example. */
public class SetDefaultRetention {
/** MinioClient.setDefaultRetention() exanple. */
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try {
/* Amazon S3: */
MinioClient s3Client =
new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY");

// Create bucket if it doesn't exist.
boolean found =
s3Client.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build());
if (found) {
System.out.println("my-bucketname already exists");
} else {
// Create bucket 'my-bucketname' with object lock functionality enabled
s3Client.makeBucket(
MakeBucketArgs.builder().bucket("my-bucketname").objectLock(true).build());
System.out.println(
"my-bucketname is created successfully with object lock functionality enabled.");
}

// Declaring config with Retention mode as Compliance and duration as 100 days
ObjectLockConfiguration config =
new ObjectLockConfiguration(RetentionMode.COMPLIANCE, new RetentionDurationDays(100));

// Set object lock configuration
s3Client.setDefaultRetention("my-bucketname", config);

// Get object lock configuration
ObjectLockConfiguration bucketConfig = s3Client.getDefaultRetention("my-bucketname");
s3Client.setDefaultRetention(
SetDefaultRetentionArgs.builder()
.bucket("my-lock-enabled-bucketname")
.config(config)
.build());

System.out.println("Default retention configuration of bucket");
System.out.println("Mode: " + bucketConfig.mode());
System.out.println("Duration: " + bucketConfig.duration());
System.out.println("Default retention configuration is set successfully");
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
}
Expand Down
Loading

0 comments on commit 2250265

Please sign in to comment.