diff --git a/api/src/main/java/io/minio/GetDefaultRetentionArgs.java b/api/src/main/java/io/minio/GetDefaultRetentionArgs.java new file mode 100644 index 000000000..a05e6729b --- /dev/null +++ b/api/src/main/java/io/minio/GetDefaultRetentionArgs.java @@ -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 {} +} diff --git a/api/src/main/java/io/minio/MinioClient.java b/api/src/main/java/io/minio/MinioClient.java index 3c8370044..58553d585 100755 --- a/api/src/main/java/io/minio/MinioClient.java +++ b/api/src/main/java/io/minio/MinioClient.java @@ -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. + * + *
Example:{@code
+   * ObjectLockConfiguration config = new ObjectLockConfiguration(
+   *     RetentionMode.COMPLIANCE, new RetentionDurationDays(100));
+   * minioClient.setDefaultRetention(
+   *     SetDefaultRetentionArgs.builder().bucket("my-bucketname").config(config).build());
+   * }
+ * + * @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 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. * *
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(
@@ -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.
+   *
+   * 
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());
+   * }
+ * + * @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 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()); } } diff --git a/api/src/main/java/io/minio/SetDefaultRetentionArgs.java b/api/src/main/java/io/minio/SetDefaultRetentionArgs.java new file mode 100644 index 000000000..5f4ad39fa --- /dev/null +++ b/api/src/main/java/io/minio/SetDefaultRetentionArgs.java @@ -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 { + 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; + } + } +} diff --git a/docs/API.md b/docs/API.md index d5ca07976..095ed7ec5 100644 --- a/docs/API.md +++ b/docs/API.md @@ -546,16 +546,16 @@ Tags tags = minioClient.getBucketTags(GetBucketTagsArgs.builder().bucket("my-buc ``` -### 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 | |:---------------------------------------------------------------| @@ -563,9 +563,9 @@ __Parameters__ __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()); ``` @@ -942,22 +942,22 @@ minioClient.setBucketTags(SetBucketTagsArgs.builder().bucket("my-bucketname").ta ``` -### 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 @@ -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 diff --git a/examples/GetDefaultRetention.java b/examples/GetDefaultRetention.java new file mode 100644 index 000000000..c14c280cc --- /dev/null +++ b/examples/GetDefaultRetention.java @@ -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); + } + } +} diff --git a/examples/SetGetBucketObjectLockConfig.java b/examples/SetDefaultRetention.java similarity index 57% rename from examples/SetGetBucketObjectLockConfig.java rename to examples/SetDefaultRetention.java index 56d8d7973..71f805075 100644 --- a/examples/SetGetBucketObjectLockConfig.java +++ b/examples/SetDefaultRetention.java @@ -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; @@ -25,8 +24,8 @@ 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 { @@ -34,32 +33,17 @@ public static void main(String[] args) 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); } diff --git a/functional/FunctionalTest.java b/functional/FunctionalTest.java index 8d77e73e7..ad72d0e2c 100644 --- a/functional/FunctionalTest.java +++ b/functional/FunctionalTest.java @@ -40,6 +40,7 @@ import io.minio.GetBucketNotificationArgs; import io.minio.GetBucketPolicyArgs; import io.minio.GetBucketTagsArgs; +import io.minio.GetDefaultRetentionArgs; import io.minio.GetObjectArgs; import io.minio.GetObjectRetentionArgs; import io.minio.GetObjectTagsArgs; @@ -64,6 +65,7 @@ import io.minio.SetBucketNotificationArgs; import io.minio.SetBucketPolicyArgs; import io.minio.SetBucketTagsArgs; +import io.minio.SetDefaultRetentionArgs; import io.minio.SetObjectRetentionArgs; import io.minio.SetObjectTagsArgs; import io.minio.StatObjectArgs; @@ -3581,10 +3583,11 @@ public static void disableObjectLegalHold_test() throws Exception { } } - /** Test: setDefaultRetention(String bucketName). */ + /** Test: setDefaultRetention(SetDefaultRetentionArgs args). */ public static void setDefaultRetention_test() throws Exception { + String methodName = "setDefaultRetention(SetDefaultRetentionArgs args)"; if (!mintEnv) { - System.out.println("Test: setDefaultRetention(String bucketName)"); + System.out.println("Test: " + methodName); } long startTime = System.currentTimeMillis(); @@ -3595,37 +3598,22 @@ public static void setDefaultRetention_test() throws Exception { try { ObjectLockConfiguration config = new ObjectLockConfiguration(RetentionMode.COMPLIANCE, new RetentionDurationDays(10)); - client.setDefaultRetention(bucketName, config); + client.setDefaultRetention( + SetDefaultRetentionArgs.builder().bucket(bucketName).config(config).build()); } finally { client.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build()); } - mintSuccessLog("setDefaultRetention (String bucketName)", null, startTime); + mintSuccessLog(methodName, null, startTime); } catch (Exception e) { - ErrorResponse errorResponse = null; - if (e instanceof ErrorResponseException) { - ErrorResponseException exp = (ErrorResponseException) e; - errorResponse = exp.errorResponse(); - } - - // Ignore NotImplemented error - if (errorResponse != null && errorResponse.errorCode() == ErrorCode.NOT_IMPLEMENTED) { - mintIgnoredLog("setDefaultRetention (String bucketName)", null, startTime); - } else { - mintFailedLog( - "setDefaultRetention (String bucketName)", - null, - startTime, - null, - e.toString() + " >>> " + Arrays.toString(e.getStackTrace())); - throw e; - } + handleException(methodName, null, startTime, e); } } - /** Test: getDefaultRetention(String bucketName). */ + /** Test: getDefaultRetention(GetDefaultRetentionArgs args). */ public static void getDefaultRetention_test() throws Exception { + String methodName = "getDefaultRetention(GetDefaultRetentionArgs args)"; if (!mintEnv) { - System.out.println("Test: getDefaultRetention(String bucketName)"); + System.out.println("Test: " + methodName); } long startTime = System.currentTimeMillis(); @@ -3635,66 +3623,49 @@ public static void getDefaultRetention_test() throws Exception { try { ObjectLockConfiguration expectedConfig = new ObjectLockConfiguration(RetentionMode.COMPLIANCE, new RetentionDurationDays(10)); - client.setDefaultRetention(bucketName, expectedConfig); - ObjectLockConfiguration config = client.getDefaultRetention(bucketName); + client.setDefaultRetention( + SetDefaultRetentionArgs.builder().bucket(bucketName).config(expectedConfig).build()); + ObjectLockConfiguration config = + client.getDefaultRetention( + GetDefaultRetentionArgs.builder().bucket(bucketName).build()); - if ((!(config.duration().unit() == expectedConfig.duration().unit() - && config.duration().duration() == expectedConfig.duration().duration())) - || (config.mode() != expectedConfig.mode())) { + if (config.mode() != expectedConfig.mode()) { throw new Exception( - "[FAILED] Expected: expected duration : " - + expectedConfig.duration() - + ", got: " - + config.duration() - + " expected mode :" - + expectedConfig.mode() - + ", got: " - + config.mode()); + "[FAILED] mode: expected: " + expectedConfig.mode() + ", got: " + config.mode()); + } + + if (config.duration().unit() != expectedConfig.duration().unit() + || config.duration().duration() != expectedConfig.duration().duration()) { + throw new Exception( + "[FAILED] duration: " + expectedConfig.duration() + ", got: " + config.duration()); } expectedConfig = new ObjectLockConfiguration(RetentionMode.GOVERNANCE, new RetentionDurationYears(1)); - client.setDefaultRetention(bucketName, expectedConfig); - config = client.getDefaultRetention(bucketName); + client.setDefaultRetention( + SetDefaultRetentionArgs.builder().bucket(bucketName).config(expectedConfig).build()); + config = + client.getDefaultRetention( + GetDefaultRetentionArgs.builder().bucket(bucketName).build()); - if ((!(config.duration().unit() == expectedConfig.duration().unit() - && config.duration().duration() == expectedConfig.duration().duration())) - || (config.mode() != expectedConfig.mode())) { + if (config.mode() != expectedConfig.mode()) { throw new Exception( - "[FAILED] Expected: expected duration : " - + expectedConfig.duration() - + ", got: " - + config.duration() - + " expected mode :" - + expectedConfig.mode() - + ", got: " - + config.mode()); + "[FAILED] mode: expected: " + expectedConfig.mode() + ", got: " + config.mode()); } + + if (config.duration().unit() != expectedConfig.duration().unit() + || config.duration().duration() != expectedConfig.duration().duration()) { + throw new Exception( + "[FAILED] duration: " + expectedConfig.duration() + ", got: " + config.duration()); + } + } finally { client.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build()); } - mintSuccessLog("getDefaultRetention (String bucketName)", null, startTime); - + mintSuccessLog(methodName, null, startTime); } catch (Exception e) { - ErrorResponse errorResponse = null; - if (e instanceof ErrorResponseException) { - ErrorResponseException exp = (ErrorResponseException) e; - errorResponse = exp.errorResponse(); - } - - // Ignore NotImplemented error - if (errorResponse != null && errorResponse.errorCode() == ErrorCode.NOT_IMPLEMENTED) { - mintIgnoredLog("getDefaultRetention (String bucketName)", null, startTime); - } else { - mintFailedLog( - "getDefaultRetention (String bucketName)", - null, - startTime, - null, - e.toString() + " >>> " + Arrays.toString(e.getStackTrace())); - throw e; - } + handleException(methodName, null, startTime, e); } }