diff --git a/README.md b/README.md index 8efd7bbe4..682f2ba4a 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,8 @@ public class FileUploader { MinioClient minioClient = new MinioClient("https://play.min.io", "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); // Check if the bucket already exists. - boolean isExist = minioClient.bucketExists("asiatrip"); + boolean isExist = + minioClient.bucketExists(BucketExistsArgs.builder().bucket("asiatrip").build()); if(isExist) { System.out.println("Bucket already exists."); } else { diff --git a/api/src/main/java/io/minio/BucketExistsArgs.java b/api/src/main/java/io/minio/BucketExistsArgs.java new file mode 100644 index 000000000..d921f82d1 --- /dev/null +++ b/api/src/main/java/io/minio/BucketExistsArgs.java @@ -0,0 +1,38 @@ +/* + * 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.bucketExists(). */ +public class BucketExistsArgs extends BucketArgs { + private boolean objectLock; + + public boolean objectLock() { + return objectLock; + } + + public static Builder builder() { + return new Builder(); + } + + /** Argument builder of {@link BucketExistsArgs}. */ + public static final class Builder extends BucketArgs.Builder { + public Builder objectLock(boolean objectLock) { + operations.add(args -> args.objectLock = objectLock); + return this; + } + } +} diff --git a/api/src/main/java/io/minio/MinioClient.java b/api/src/main/java/io/minio/MinioClient.java index 394dd8b71..87dffe074 100755 --- a/api/src/main/java/io/minio/MinioClient.java +++ b/api/src/main/java/io/minio/MinioClient.java @@ -3526,20 +3526,56 @@ public List listBuckets() * @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 #bucketExists(BucketExistsArgs)} */ + @Deprecated public boolean bucketExists(String bucketName) throws ErrorResponseException, IllegalArgumentException, InsufficientDataException, InternalException, InvalidBucketNameException, InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException { + return bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); + } + + /** + * Checks if a bucket exists. + * + *
Example:{@code
+   * 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");
+   * }
+   * }
+ * + * @param args {@link BucketExistsArgs} object. + * @return boolean - True if the bucket exists. + * @throws ErrorResponseException thrown to indicate S3 service returned an error response. + * @throws IllegalArgumentException throws to indicate invalid argument passed. + * @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 bucketExists(BucketExistsArgs args) + throws ErrorResponseException, IllegalArgumentException, InsufficientDataException, + InternalException, InvalidBucketNameException, InvalidKeyException, + InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException { try { - executeHead(bucketName, null); + executeHead(args.bucket(), null); return true; } catch (ErrorResponseException e) { if (e.errorResponse().errorCode() != ErrorCode.NO_SUCH_BUCKET) { throw e; } } - return false; } diff --git a/docs/API.md b/docs/API.md index a45515409..128ae8a67 100644 --- a/docs/API.md +++ b/docs/API.md @@ -263,15 +263,15 @@ All APIs throw below exceptions in addition to specific to API. ## 2. Bucket operations -### bucketExists(String bucketName) -`public boolean bucketExists(String bucketName)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#bucketExists-java.lang.String-)_ +### bucketExists(BucketExistsArgs args) +`public boolean bucketExists(BucketExistsArgs args)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#bucketExists-io.minio.BucketExistsArgs-)_ Checks if a bucket exists. __Parameters__ -| Parameter | Type | Description | -|:---------------|:---------|:--------------------| -| ``bucketName`` | _String_ | Name of the bucket. | +| Parameter | Type | Description | +|:---------------|:---------------------|:---------------| +| ``bucketName`` | _[BucketExistsArgs]_ | Arguments. | | Returns | |:---------------------------------------| @@ -280,7 +280,8 @@ __Parameters__ __Example__ ```java // Check whether 'my-bucketname' exists or not. -boolean found = minioClient.bucketExists("my-bucketname"); +boolean found = + minioClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build()); if (found) { System.out.println("my-bucketname exists"); } else { @@ -1737,3 +1738,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 +[BucketExistsArgs]: http://minio.github.io/minio-java/io/minio/BucketExistsArgs.html diff --git a/examples/BucketExists.java b/examples/BucketExists.java index 546a0cf79..17a8b79bf 100644 --- a/examples/BucketExists.java +++ b/examples/BucketExists.java @@ -14,6 +14,7 @@ * limitations under the License. */ +import io.minio.BucketExistsArgs; import io.minio.MinioClient; import io.minio.errors.MinioException; import java.io.IOException; @@ -37,7 +38,8 @@ public static void main(String[] args) // "YOUR-SECRETACCESSKEY"); // Check whether 'my-bucketname' exist or not. - boolean found = minioClient.bucketExists("my-bucketname"); + boolean found = + minioClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build()); if (found) { System.out.println("my-bucketname exists"); } else { diff --git a/examples/EnableDisableObjectLegalHold.java b/examples/EnableDisableObjectLegalHold.java index d524746c3..f0dd73cc4 100644 --- a/examples/EnableDisableObjectLegalHold.java +++ b/examples/EnableDisableObjectLegalHold.java @@ -14,6 +14,7 @@ * limitations under the License. */ +import io.minio.BucketExistsArgs; import io.minio.MakeBucketArgs; import io.minio.MinioClient; import io.minio.PutObjectOptions; @@ -51,7 +52,8 @@ public static void main(String[] args) "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); // Create bucket if it doesn't exist. - boolean found = minioClient.bucketExists("my-bucketname"); + boolean found = + minioClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build()); if (found) { System.out.println("my-bucketname already exists"); } else { diff --git a/examples/ListIncompleteUploads.java b/examples/ListIncompleteUploads.java index 68c83073c..e97f745ad 100644 --- a/examples/ListIncompleteUploads.java +++ b/examples/ListIncompleteUploads.java @@ -14,6 +14,7 @@ * limitations under the License. */ +import io.minio.BucketExistsArgs; import io.minio.MinioClient; import io.minio.Result; import io.minio.errors.MinioException; @@ -39,7 +40,8 @@ public static void main(String[] args) // "YOUR-SECRETACCESSKEY"); // Check whether 'my-bucketname' exist or not. - boolean found = minioClient.bucketExists("my-bucketname"); + boolean found = + minioClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build()); if (found) { // List all incomplete multipart upload of objects in 'my-bucketname' Iterable> myObjects = minioClient.listIncompleteUploads("my-bucketname"); diff --git a/examples/ListObjects.java b/examples/ListObjects.java index f37b70b58..208237a82 100644 --- a/examples/ListObjects.java +++ b/examples/ListObjects.java @@ -14,6 +14,7 @@ * limitations under the License. */ +import io.minio.BucketExistsArgs; import io.minio.ListObjectsArgs; import io.minio.MinioClient; import io.minio.Result; @@ -40,7 +41,8 @@ public static void main(String[] args) // "YOUR-SECRETACCESSKEY"); // Check whether 'my-bucketname' exist or not. - boolean found = minioClient.bucketExists("my-bucketname"); + boolean found = + minioClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build()); if (found) { // List objects from 'my-bucketname' Iterable> myObjects = diff --git a/examples/MakeBucket.java b/examples/MakeBucket.java index 15ca2c624..3c8642165 100644 --- a/examples/MakeBucket.java +++ b/examples/MakeBucket.java @@ -14,6 +14,7 @@ * limitations under the License. */ +import io.minio.BucketExistsArgs; import io.minio.MakeBucketArgs; import io.minio.MinioClient; import io.minio.errors.MinioException; @@ -38,13 +39,14 @@ public static void main(String[] args) // "YOUR-SECRETACCESSKEY"); // Create bucket 'my-bucketname' if it doesn`t exist. - if (!minioClient.bucketExists("my-bucketname")) { + if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build())) { minioClient.makeBucket(MakeBucketArgs.builder().bucket("my-bucketname").build()); System.out.println("my-bucketname is created successfully"); } // Create bucket 'my-bucketname-in-eu' in 'eu-west-1' region if it doesn't exist. - if (!minioClient.bucketExists("my-bucketname-in-eu")) { + if (!minioClient.bucketExists( + BucketExistsArgs.builder().bucket("my-bucketname-in-eu").build())) { minioClient.makeBucket( MakeBucketArgs.builder().bucket("my-bucketname-in-eu").region("eu-west-1").build()); System.out.println("my-bucketname-in-eu is created successfully"); @@ -52,7 +54,8 @@ public static void main(String[] args) // Create bucket 'my-bucketname-in-eu-with-object-lock' in 'eu-west-1' with object lock // functionality enabled. - if (!minioClient.bucketExists("my-bucketname-in-eu-with-object-lock")) { + if (!minioClient.bucketExists( + BucketExistsArgs.builder().bucket("my-bucketname-in-eu-with-object-lock").build())) { minioClient.makeBucket( MakeBucketArgs.builder() .bucket("my-bucketname-in-eu-with-object-lock") diff --git a/examples/RemoveBucket.java b/examples/RemoveBucket.java index da0048851..436446abb 100644 --- a/examples/RemoveBucket.java +++ b/examples/RemoveBucket.java @@ -14,6 +14,7 @@ * limitations under the License. */ +import io.minio.BucketExistsArgs; import io.minio.MinioClient; import io.minio.RemoveBucketArgs; import io.minio.errors.MinioException; @@ -39,7 +40,8 @@ public static void main(String[] args) // Remove bucket 'my-bucketname' if it exists. // This operation will only work if your bucket is empty. - boolean found = minioClient.bucketExists("my-bucketname"); + boolean found = + minioClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build()); if (found) { minioClient.removeBucket(RemoveBucketArgs.builder().bucket("my-bucketname").build()); System.out.println("my-bucketname is removed successfully"); diff --git a/examples/SetGetBucketObjectLockConfig.java b/examples/SetGetBucketObjectLockConfig.java index 064523854..56d8d7973 100644 --- a/examples/SetGetBucketObjectLockConfig.java +++ b/examples/SetGetBucketObjectLockConfig.java @@ -14,6 +14,7 @@ * limitations under the License. */ +import io.minio.BucketExistsArgs; import io.minio.MakeBucketArgs; import io.minio.MinioClient; import io.minio.errors.MinioException; @@ -34,7 +35,8 @@ public static void main(String[] args) new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY"); // Create bucket if it doesn't exist. - boolean found = s3Client.bucketExists("my-bucketname"); + boolean found = + s3Client.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build()); if (found) { System.out.println("my-bucketname already exists"); } else { diff --git a/functional/FunctionalTest.java b/functional/FunctionalTest.java index d1c24c5cf..e4237a5c7 100644 --- a/functional/FunctionalTest.java +++ b/functional/FunctionalTest.java @@ -19,6 +19,7 @@ import static java.nio.file.StandardOpenOption.CREATE; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import io.minio.BucketExistsArgs; import io.minio.CloseableIterator; import io.minio.ComposeSource; import io.minio.CopyConditions; @@ -547,29 +548,24 @@ public static void listBuckets_test() throws Exception { } } - /** Test: bucketExists(String bucketName). */ + /** Test: bucketExists(BucketExistsArgs args). */ public static void bucketExists_test() throws Exception { + String methodName = "bucketExists(BucketExistsArgs args)"; if (!mintEnv) { - System.out.println("Test: bucketExists(String bucketName)"); + System.out.println("Test: " + methodName); } long startTime = System.currentTimeMillis(); try { String name = getRandomName(); client.makeBucket(MakeBucketArgs.builder().bucket(name).build()); - if (!client.bucketExists(name)) { + if (!client.bucketExists(BucketExistsArgs.builder().bucket(name).build())) { throw new Exception("[FAILED] bucket does not exist"); } client.removeBucket(RemoveBucketArgs.builder().bucket(name).build()); - mintSuccessLog("bucketExists(String bucketName)", null, startTime); + mintSuccessLog(methodName, null, startTime); } catch (Exception e) { - mintFailedLog( - "bucketExists(String bucketName)", - null, - startTime, - null, - e.toString() + " >>> " + Arrays.toString(e.getStackTrace())); - throw e; + handleException(methodName, null, startTime, e); } }