Skip to content

Commit

Permalink
add builder support for bucketExists
Browse files Browse the repository at this point in the history
  • Loading branch information
sinhaashish committed May 29, 2020
1 parent 928b31b commit 9d2a6b5
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 29 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions api/src/main/java/io/minio/BucketExistsArgs.java
Original file line number Diff line number Diff line change
@@ -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<Builder, BucketExistsArgs> {
public Builder objectLock(boolean objectLock) {
operations.add(args -> args.objectLock = objectLock);
return this;
}
}
}
40 changes: 38 additions & 2 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3526,20 +3526,56 @@ public List<Bucket> 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.
*
* <pre>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");
* }
* }</pre>
*
* @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;
}

Expand Down
14 changes: 8 additions & 6 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ All APIs throw below exceptions in addition to specific to API.
## 2. Bucket operations

<a name="bucketExists"></a>
### 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 |
|:---------------------------------------|
Expand All @@ -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 {
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion examples/BucketExists.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion examples/EnableDisableObjectLegalHold.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion examples/ListIncompleteUploads.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Result<Upload>> myObjects = minioClient.listIncompleteUploads("my-bucketname");
Expand Down
4 changes: 3 additions & 1 deletion examples/ListObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import io.minio.BucketExistsArgs;
import io.minio.ListObjectsArgs;
import io.minio.MinioClient;
import io.minio.Result;
Expand All @@ -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<Result<Item>> myObjects =
Expand Down
9 changes: 6 additions & 3 deletions examples/MakeBucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,21 +39,23 @@ 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");
}

// 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")
Expand Down
4 changes: 3 additions & 1 deletion examples/RemoveBucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down
4 changes: 3 additions & 1 deletion examples/SetGetBucketObjectLockConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
18 changes: 7 additions & 11 deletions functional/FunctionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 9d2a6b5

Please sign in to comment.