diff --git a/api/src/main/java/io/minio/IsVersioningEnabledArgs.java b/api/src/main/java/io/minio/IsVersioningEnabledArgs.java new file mode 100644 index 000000000..a0693ef6d --- /dev/null +++ b/api/src/main/java/io/minio/IsVersioningEnabledArgs.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.isVersioningEnabled(). */ +public class IsVersioningEnabledArgs extends BucketArgs { + public static Builder builder() { + return new Builder(); + } + + /** Argument builder of {@link IsVersioningEnabledArgs}. */ + 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 394dd8b71..21c4df0c9 100755 --- a/api/src/main/java/io/minio/MinioClient.java +++ b/api/src/main/java/io/minio/MinioClient.java @@ -72,6 +72,7 @@ import io.minio.messages.SseConfiguration; import io.minio.messages.Tags; import io.minio.messages.Upload; +import io.minio.messages.VersioningConfiguration; import io.minio.org.apache.commons.validator.routines.InetAddressValidator; import java.io.BufferedInputStream; import java.io.FileInputStream; @@ -2119,10 +2120,7 @@ public void getObject( * .build()); * } * - * @param args Object of {@link GetObjectArgs} - * @param objectName Object name in the bucket. - * @param ssec SSE-C type server-side encryption. - * @param fileName Name of the file. + * @param args Object of {@link DownloadObjectArgs} * @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. @@ -3733,6 +3731,7 @@ public void makeBucket(MakeBucketArgs 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 #enableVersioning(EnableVersioningArgs)} */ @Deprecated public void enableVersioning(String bucketName) @@ -3770,9 +3769,8 @@ public void enableVersioning(EnableVersioningArgs args) Map queryParamMap = new HashMap<>(); queryParamMap.put("versioning", ""); - String config = - "" - + "Enabled"; + VersioningConfiguration config = new VersioningConfiguration(true); + Response response = executePut(args.bucket(), null, null, queryParamMap, config, 0); response.body().close(); } @@ -3796,6 +3794,7 @@ public void enableVersioning(EnableVersioningArgs 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 #disableVersioning(DisableVersioningArgs)} */ @Deprecated public void disableVersioning(String bucketName) @@ -3834,13 +3833,53 @@ public void disableVersioning(DisableVersioningArgs args) Map queryParamMap = new HashMap<>(); queryParamMap.put("versioning", ""); - String config = - "" - + "Suspended"; + VersioningConfiguration config = new VersioningConfiguration(false); Response response = executePut(args.bucket(), null, null, queryParamMap, config, 0); response.body().close(); } + /** + * Returns true if versioning is enabled on the bucket. + * + *
Example:{@code
+   * boolean isVersioningEnabled =
+   *  minioClient.isVersioningEnabled(
+   *       IsVersioningEnabledArgs.builder().bucket("my-bucketname").build());
+   * if (isVersioningEnabled) {
+   *   System.out.println("Bucket versioning is enabled");
+   * } else {
+   *   System.out.println("Bucket versioning is disabled");
+   * }
+   * }
+ * + * @param args {@link IsVersioningEnabledArgs} 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 boolean isVersioningEnabled(IsVersioningEnabledArgs args) + throws ErrorResponseException, IllegalArgumentException, InsufficientDataException, + InternalException, InvalidBucketNameException, InvalidKeyException, + InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException { + checkArgs(args); + + Map queryParamMap = new HashMap<>(); + queryParamMap.put("versioning", ""); + try (Response response = executeGet(args.bucket(), null, null, queryParamMap)) { + VersioningConfiguration result = + Xml.unmarshal(VersioningConfiguration.class, response.body().charStream()); + return result.status(); + } + } + /** * Sets default object retention in a bucket. * diff --git a/api/src/main/java/io/minio/messages/VersioningConfiguration.java b/api/src/main/java/io/minio/messages/VersioningConfiguration.java new file mode 100644 index 000000000..08a146acb --- /dev/null +++ b/api/src/main/java/io/minio/messages/VersioningConfiguration.java @@ -0,0 +1,51 @@ +/* + * 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.messages; + +import org.simpleframework.xml.Element; +import org.simpleframework.xml.Namespace; +import org.simpleframework.xml.Root; + +/** + * Object representation of request XML of PutBucketVersioning + * API and response XML of GetBucketVersioning + * API. + */ +@Root(name = "VersioningConfiguration", strict = false) +@Namespace(reference = "http://s3.amazonaws.com/doc/2006-03-01/") +public class VersioningConfiguration { + @Element(name = "Status", required = false) + private String status; + + public VersioningConfiguration() {} + + /** Constructs a new VersioningConfiguration object with given status. */ + public VersioningConfiguration(boolean status) { + if (status) { + this.status = "Enabled"; + } else { + this.status = "Suspended"; + } + } + + /** Indicates whether the bucket is version enabled or not. */ + public boolean status() { + return ("Enabled").equals(status); + } +} diff --git a/docs/API.md b/docs/API.md index a45515409..c3dfa9045 100644 --- a/docs/API.md +++ b/docs/API.md @@ -31,16 +31,17 @@ MinioClient s3Client = new MinioClient("https://s3.amazonaws.com", | [`getBucketPolicy`](#getBucketPolicy) | [`getPresignedObjectUrl`](#getPresignedObjectUrl) | | [`getBucketTags`](#getBucketTags) | [`isObjectLegalHoldEnabled`](#isObjectLegalHoldEnabled) | | [`getDefaultRetention`](#getDefaultRetention) | [`listObjects`](#listObjects) | -| [`listBuckets`](#listBuckets) | [`presignedGetObject`](#presignedGetObject) | -| [`listenBucketNotification`](#listenBucketNotification) | [`presignedPostPolicy`](#presignedPostPolicy) | -| [`listIncompleteUploads`](#listIncompleteUploads) | [`presignedPutObject`](#presignedPutObject) | -| [`makeBucket`](#makeBucket) | [`putObject`](#putObject) | -| [`removeAllBucketNotification`](#removeAllBucketNotification) | [`removeObject`](#removeObject) | -| [`removeBucket`](#removeBucket) | [`removeObjects`](#removeObjects) | -| [`removeIncompleteUpload`](#removeIncompleteUpload) | [`selectObjectContent`](#selectObjectContent) | -| [`setBucketEncryption`](#setBucketEncryption) | [`setObjectRetention`](#setObjectRetention) | -| [`setBucketLifeCycle`](#setBucketLifeCycle) | [`setObjectTags`](#setObjectTags) | -| [`setBucketNotification`](#setBucketNotification) | [`statObject`](#statObject) | +| [`isVersioningEnabled`](#isVersioningEnabled) | [`presignedGetObject`](#presignedGetObject) | +| [`listBuckets`](#listBuckets) | [`presignedPostPolicy`](#presignedPostPolicy) | +| [`listenBucketNotification`](#listenBucketNotification) | [`presignedPutObject`](#presignedPutObject) | +| [`listIncompleteUploads`](#listIncompleteUploads) | [`putObject`](#putObject) | +| [`makeBucket`](#makeBucket) | [`removeObject`](#removeObject) | +| [`removeAllBucketNotification`](#removeAllBucketNotification) | [`removeObjects`](#removeObjects) | +| [`removeBucket`](#removeBucket) | [`selectObjectContent`](#selectObjectContent) | +| [`removeIncompleteUpload`](#removeIncompleteUpload) | [`setObjectRetention`](#setObjectRetention) | +| [`setBucketEncryption`](#setBucketEncryption) | [`setObjectTags`](#setObjectTags) | +| [`setBucketLifeCycle`](#setBucketLifeCycle) | [`statObject`](#statObject) | +| [`setBucketNotification`](#setBucketNotification) | | | [`setBucketPolicy`](#setBucketPolicy) | | | [`setBucketTags`](#setBucketTags) | | | [`setDefaultRetention`](#setDefaultRetention) | | @@ -388,6 +389,34 @@ __Example__ minioClient.enableVersioning(EnableVersioningArgs.builder().bucket("my-bucket").build()); ``` + +### isVersioningEnabled(IsVersioningEnabledArgs args) +`public boolean isVersioningEnabled(IsVersioningEnabledArgs args)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#isVersioningEnabled-io.minio.IsVersioningEnabledArgs-)_ + +Get bucket version status. + +__Parameters__ + +| Parameter | Type | Description | +|:-----------|:-----------------------------|:-----------------| +| ``args`` | _[IsVersioningEnabledArgs]_ | Arguments. | + +| Returns | +|:--------------------------------------------------| +| _boolean_ - True if bucket versioning is enabled. | + +__Example__ +```java +boolean isVersioningEnabled = + minioClient.isVersioningEnabled( + IsVersioningEnabledArgs.builder().bucket("my-bucketname").build()); +if (isVersioningEnabled) { + System.out.println("Bucket versioning is enabled"); +} else { + System.out.println("Bucket versioning is disabled"); +} +``` + ### getBucketEncryption(GetBucketEncryptionArgs args) `public SseConfiguration getBucketEncryption(GetBucketEncryptionArgs args)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#getBucketEncryption-io.minio.GetBucketEncryptionArgs-)_ @@ -1737,3 +1766,4 @@ ObjectStat objectStat = [DeleteBucketPolicyArgs]: http://minio.github.io/minio-java/io/minio/DeleteBucketPolicyArgs.html [GetObjectArgs]: http://minio.github.io/minio-java/io/minio/GetObjectArgs.html [DownloadObjectArgs]: http://minio.github.io/minio-java/io/minio/DownloadObjectArgs.html +[IsVersioningEnabledArgs]: http://minio.github.io/minio-java/io/minio/IsVersioningEnabledArgs.html diff --git a/examples/DisableVersioning.java b/examples/DisableVersioning.java new file mode 100644 index 000000000..d093158dc --- /dev/null +++ b/examples/DisableVersioning.java @@ -0,0 +1,50 @@ +/* + * MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2015 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.DisableVersioningArgs; +import io.minio.MinioClient; +import io.minio.errors.MinioException; +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +public class DisableVersioning { + /** MinioClient.disableVersioning() example. */ + public static void main(String[] args) + throws IOException, NoSuchAlgorithmException, InvalidKeyException { + try { + /* play.min.io for test and development. */ + MinioClient minioClient = + new MinioClient( + "https://play.min.io", + "Q3AM3UQ867SPQQA43P2F", + "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); + + /* Amazon S3: */ + // MinioClient minioClient = new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID", + // "YOUR-SECRETACCESSKEY"); + + // Disable versioning on 'my-bucketname'. + minioClient.disableVersioning( + DisableVersioningArgs.builder().bucket("my-bucketname").build()); + + System.out.println("Bucket versioning is disabled successfully"); + + } catch (MinioException e) { + System.out.println("Error occurred: " + e); + } + } +} diff --git a/examples/EnableVersioning.java b/examples/EnableVersioning.java new file mode 100644 index 000000000..79b4904cd --- /dev/null +++ b/examples/EnableVersioning.java @@ -0,0 +1,49 @@ +/* + * MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2015 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.EnableVersioningArgs; +import io.minio.MinioClient; +import io.minio.errors.MinioException; +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +public class EnableVersioning { + /** MinioClient.enableVersioning() example. */ + public static void main(String[] args) + throws IOException, NoSuchAlgorithmException, InvalidKeyException { + try { + /* play.min.io for test and development. */ + MinioClient minioClient = + new MinioClient( + "https://play.min.io", + "Q3AM3UQ867SPQQA43P2F", + "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); + + /* Amazon S3: */ + // MinioClient minioClient = new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID", + // "YOUR-SECRETACCESSKEY"); + + // Enable versioning on 'my-bucketname'. + minioClient.enableVersioning(EnableVersioningArgs.builder().bucket("my-bucketname").build()); + + System.out.println("Bucket versioning is enabled successfully"); + + } catch (MinioException e) { + System.out.println("Error occurred: " + e); + } + } +} diff --git a/examples/IsVersioningEnabled.java b/examples/IsVersioningEnabled.java new file mode 100644 index 000000000..2b186e329 --- /dev/null +++ b/examples/IsVersioningEnabled.java @@ -0,0 +1,73 @@ +/* + * MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2015 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.EnableVersioningArgs; +import io.minio.IsVersioningEnabledArgs; +import io.minio.MakeBucketArgs; +import io.minio.MinioClient; +import io.minio.errors.MinioException; +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +public class IsVersioningEnabled { + /** MinioClient.isVersioningEnabled() example. */ + public static void main(String[] args) + throws IOException, NoSuchAlgorithmException, InvalidKeyException { + try { + /* play.min.io for test and development. */ + MinioClient minioClient = + new MinioClient( + "https://play.min.io", + "Q3AM3UQ867SPQQA43P2F", + "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); + + /* Amazon S3: */ + // MinioClient minioClient = new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID", + // "YOUR-SECRETACCESSKEY"); + + // Create bucket 'my-bucketname' if it doesn`t exist. + if (!minioClient.bucketExists("my-bucketname")) { + minioClient.makeBucket(MakeBucketArgs.builder().bucket("my-bucketname").build()); + System.out.println("my-bucketname is created successfully"); + } + + boolean isVersioningEnabled = + minioClient.isVersioningEnabled( + IsVersioningEnabledArgs.builder().bucket("my-bucketname").build()); + if (isVersioningEnabled) { + System.out.println("Bucket versioning is enabled"); + } else { + System.out.println("Bucket versioning is disabled"); + } + // Enable versioning on 'my-bucketname'. + minioClient.enableVersioning(EnableVersioningArgs.builder().bucket("my-bucketname").build()); + System.out.println("Bucket versioning is enabled successfully"); + + isVersioningEnabled = + minioClient.isVersioningEnabled( + IsVersioningEnabledArgs.builder().bucket("my-bucketname").build()); + if (isVersioningEnabled) { + System.out.println("Bucket versioning is enabled"); + } else { + System.out.println("Bucket versioning is disabled"); + } + + } catch (MinioException e) { + System.out.println("Error occurred: " + e); + } + } +} diff --git a/functional/FunctionalTest.java b/functional/FunctionalTest.java index d1c24c5cf..2ed055c07 100644 --- a/functional/FunctionalTest.java +++ b/functional/FunctionalTest.java @@ -38,6 +38,7 @@ import io.minio.GetObjectArgs; import io.minio.GetObjectRetentionArgs; import io.minio.GetObjectTagsArgs; +import io.minio.IsVersioningEnabledArgs; import io.minio.ListObjectsArgs; import io.minio.MakeBucketArgs; import io.minio.MinioClient; @@ -432,8 +433,9 @@ public static void makeBucket_test4() throws Exception { /** Test: enableVersioning(EnableVersioningArgs args). */ public static void enableVersioning_test() throws Exception { + String methodName = "enableVersioning(EnableVersioningArgs args)"; if (!mintEnv) { - System.out.println("Test: enableVersioning(EnableVersioningArgs args)"); + System.out.println("Test: " + methodName); } long startTime = System.currentTimeMillis(); @@ -441,33 +443,21 @@ public static void enableVersioning_test() throws Exception { String name = getRandomName(); client.makeBucket(MakeBucketArgs.builder().bucket(name).build()); client.enableVersioning(EnableVersioningArgs.builder().bucket(name).build()); + if (!client.isVersioningEnabled(IsVersioningEnabledArgs.builder().bucket(name).build())) { + throw new Exception("[FAILED] isVersioningEnabled(): expected: true, got: false"); + } client.removeBucket(RemoveBucketArgs.builder().bucket(name).build()); - mintSuccessLog("enableVersioning(EnableVersioningArgs args)", 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("enableVersioning(EnableVersioningArgs args)", null, startTime); - } else { - mintFailedLog( - "enableVersioning(EnableVersioningArgs args)", - null, - startTime, - null, - e.toString() + " >>> " + Arrays.toString(e.getStackTrace())); - throw e; - } + handleException(methodName, null, startTime, e); } } /** Test: disableVersioning(DisableVersioningArgs args). */ public static void disableVersioning_test() throws Exception { + String methodName = "disableVersioning(DisableVersioningArgs args)"; if (!mintEnv) { - System.out.println("Test: disableVersioning(DisableVersioningArgs args)"); + System.out.println("Test: " + methodName); } long startTime = System.currentTimeMillis(); @@ -475,30 +465,20 @@ public static void disableVersioning_test() throws Exception { String name = getRandomName(); client.makeBucket(MakeBucketArgs.builder().bucket(name).build()); client.disableVersioning(DisableVersioningArgs.builder().bucket(name).build()); + if (client.isVersioningEnabled(IsVersioningEnabledArgs.builder().bucket(name).build())) { + throw new Exception("[FAILED] isVersioningEnabled(): expected: false, got: true"); + } client.enableVersioning(EnableVersioningArgs.builder().bucket(name).build()); client.disableVersioning(DisableVersioningArgs.builder().bucket(name).build()); + if (client.isVersioningEnabled(IsVersioningEnabledArgs.builder().bucket(name).build())) { + throw new Exception("[FAILED] isVersioningEnabled(): expected: false, got: true"); + } client.removeBucket(RemoveBucketArgs.builder().bucket(name).build()); - mintSuccessLog("disableVersioning(DisableVersioningArgs args)", 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("disableVersioning(DisableVersioningArgs args)", null, startTime); - } else { - mintFailedLog( - "disableVersioning(DisableVersioningArgs args)", - null, - startTime, - null, - e.toString() + " >>> " + Arrays.toString(e.getStackTrace())); - throw e; - } + handleException(methodName, null, startTime, e); } }