From 259e09106f15e1ce8007b696e1c2a0253016bb24 Mon Sep 17 00:00:00 2001 From: "Bala.FA" Date: Thu, 14 May 2020 16:43:19 +0530 Subject: [PATCH] add arg builder to {set,get,delete}BucketPolicy APIs --- .../java/io/minio/DeleteBucketPolicyArgs.java | 27 +++ .../java/io/minio/GetBucketPolicyArgs.java | 27 +++ api/src/main/java/io/minio/MinioClient.java | 178 ++++++++++++++---- .../java/io/minio/SetBucketPolicyArgs.java | 51 +++++ docs/API.md | 85 +++++---- examples/DeleteBucketPolicy.java | 47 +++++ examples/GetBucketPolicy.java | 5 +- examples/SetBucketPolicy.java | 4 +- functional/FunctionalTest.java | 110 +++++------ 9 files changed, 405 insertions(+), 129 deletions(-) create mode 100644 api/src/main/java/io/minio/DeleteBucketPolicyArgs.java create mode 100644 api/src/main/java/io/minio/GetBucketPolicyArgs.java create mode 100644 api/src/main/java/io/minio/SetBucketPolicyArgs.java create mode 100644 examples/DeleteBucketPolicy.java diff --git a/api/src/main/java/io/minio/DeleteBucketPolicyArgs.java b/api/src/main/java/io/minio/DeleteBucketPolicyArgs.java new file mode 100644 index 000000000..6678f03fd --- /dev/null +++ b/api/src/main/java/io/minio/DeleteBucketPolicyArgs.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.deleteBucketPolicy(). */ +public class DeleteBucketPolicyArgs extends BucketArgs { + public static Builder builder() { + return new Builder(); + } + + /** Argument builder of {@link DeleteBucketPolicyArgs}. */ + public static final class Builder extends BucketArgs.Builder {} +} diff --git a/api/src/main/java/io/minio/GetBucketPolicyArgs.java b/api/src/main/java/io/minio/GetBucketPolicyArgs.java new file mode 100644 index 000000000..731fce824 --- /dev/null +++ b/api/src/main/java/io/minio/GetBucketPolicyArgs.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.getBucketPolicy(). */ +public class GetBucketPolicyArgs extends BucketArgs { + public static Builder builder() { + return new Builder(); + } + + /** Argument builder of {@link GetBucketPolicyArgs}. */ + 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 6c6773e9d..8a5633e66 100755 --- a/api/src/main/java/io/minio/MinioClient.java +++ b/api/src/main/java/io/minio/MinioClient.java @@ -4373,21 +4373,53 @@ public void putObject( * @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 #getBucketPolicy(GetBucketPolicyArgs)} */ + @Deprecated public String getBucketPolicy(String bucketName) throws BucketPolicyTooLargeException, ErrorResponseException, IllegalArgumentException, InsufficientDataException, InternalException, InvalidBucketNameException, InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException { + return getBucketPolicy(GetBucketPolicyArgs.builder().bucket(bucketName).build()); + } + + /** + * Gets bucket policy configuration of a bucket. + * + *
Example:{@code
+   * String config =
+   *     minioClient.getBucketPolicy(GetBucketPolicyArgs.builder().bucket("my-bucketname").build());
+   * }
+ * + * @param args {@link GetBucketPolicyArgs} object. + * @return String - Bucket policy configuration as JSON string. + * @throws BucketPolicyTooLargeException thrown to indicate returned bucket policy is too large. + * @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 String getBucketPolicy(GetBucketPolicyArgs args) + throws BucketPolicyTooLargeException, ErrorResponseException, IllegalArgumentException, + InsufficientDataException, InternalException, InvalidBucketNameException, + InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException, + XmlParserException { + checkArgs(args); + Map queryParamMap = new HashMap<>(); queryParamMap.put("policy", ""); - Response response = null; - byte[] buf = new byte[MAX_BUCKET_POLICY_SIZE]; - int bytesRead = 0; - - try { - response = executeGet(bucketName, null, null, queryParamMap); + try (Response response = executeGet(args.bucket(), null, null, queryParamMap)) { + byte[] buf = new byte[MAX_BUCKET_POLICY_SIZE]; + int bytesRead = 0; bytesRead = response.body().byteStream().read(buf, 0, MAX_BUCKET_POLICY_SIZE); if (bytesRead < 0) { throw new IOException("unexpected EOF when reading bucket policy"); @@ -4399,26 +4431,23 @@ public String getBucketPolicy(String bucketName) while (byteRead == 0) { byteRead = response.body().byteStream().read(); if (byteRead < 0) { - // reached EOF which is fine. - break; + break; // reached EOF which is fine. } if (byteRead > 0) { - throw new BucketPolicyTooLargeException(bucketName); + throw new BucketPolicyTooLargeException(args.bucket()); } } } + + return new String(buf, 0, bytesRead, StandardCharsets.UTF_8); } catch (ErrorResponseException e) { if (e.errorResponse().errorCode() != ErrorCode.NO_SUCH_BUCKET_POLICY) { throw e; } - } finally { - if (response != null) { - response.body().close(); - } } - return new String(buf, 0, bytesRead, StandardCharsets.UTF_8); + return ""; } /** @@ -4463,19 +4492,112 @@ public String getBucketPolicy(String bucketName) * @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 #setBucketPolicy(SetBucketPolicyArgs)} */ + @Deprecated public void setBucketPolicy(String bucketName, String policy) throws ErrorResponseException, IllegalArgumentException, InsufficientDataException, InternalException, InvalidBucketNameException, InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException { + setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(policy).build()); + } + + /** + * Sets bucket policy configuration to a bucket. + * + *
Example:{@code
+   * // Assume policyJson contains below JSON string;
+   * // {
+   * //     "Statement": [
+   * //         {
+   * //             "Action": [
+   * //                 "s3:GetBucketLocation",
+   * //                 "s3:ListBucket"
+   * //             ],
+   * //             "Effect": "Allow",
+   * //             "Principal": "*",
+   * //             "Resource": "arn:aws:s3:::my-bucketname"
+   * //         },
+   * //         {
+   * //             "Action": "s3:GetObject",
+   * //             "Effect": "Allow",
+   * //             "Principal": "*",
+   * //             "Resource": "arn:aws:s3:::my-bucketname/myobject*"
+   * //         }
+   * //     ],
+   * //     "Version": "2012-10-17"
+   * // }
+   * //
+   * minioClient.setBucketPolicy(
+   *     SetBucketPolicyArgs.builder().bucket("my-bucketname").config(policyJson).build());
+   * }
+ * + * @param args {@link SetBucketPolicyArgs} 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 setBucketPolicy(SetBucketPolicyArgs args) + throws ErrorResponseException, IllegalArgumentException, InsufficientDataException, + InternalException, InvalidBucketNameException, InvalidKeyException, + InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException { + checkArgs(args); + Map headerMap = new HashMap<>(); headerMap.put("Content-Type", "application/json"); Map queryParamMap = new HashMap<>(); queryParamMap.put("policy", ""); - Response response = executePut(bucketName, null, headerMap, queryParamMap, policy, 0); - response.body().close(); + Response response = executePut(args.bucket(), null, headerMap, queryParamMap, args.config(), 0); + response.close(); + } + + /** + * Deletes bucket policy configuration to a bucket. + * + *
Example:{@code
+   * minioClient.deleteBucketPolicy(DeleteBucketPolicyArgs.builder().bucket("my-bucketname"));
+   * }
+ * + * @param args {@link DeleteBucketPolicyArgs} 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 deleteBucketPolicy(DeleteBucketPolicyArgs args) + throws ErrorResponseException, IllegalArgumentException, InsufficientDataException, + InternalException, InvalidBucketNameException, InvalidKeyException, + InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException { + checkArgs(args); + + Map queryParamMap = new HashMap<>(); + queryParamMap.put("policy", ""); + + try { + Response response = executeDelete(args.bucket(), "", queryParamMap); + response.close(); + } catch (ErrorResponseException e) { + if (e.errorResponse().errorCode() != ErrorCode.NO_SUCH_BUCKET_POLICY) { + throw e; + } + } } /** @@ -4557,9 +4679,7 @@ public void setBucketLifeCycle(SetBucketLifeCycleArgs args) throws ErrorResponseException, IllegalArgumentException, InsufficientDataException, InternalException, InvalidBucketNameException, InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException { - if (args == null) { - throw new IllegalArgumentException("null arguments"); - } + checkArgs(args); Map queryParamMap = new HashMap<>(); queryParamMap.put("lifecycle", ""); @@ -4619,9 +4739,7 @@ public void deleteBucketLifeCycle(DeleteBucketLifeCycleArgs args) throws ErrorResponseException, IllegalArgumentException, InsufficientDataException, InternalException, InvalidBucketNameException, InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException { - if (args == null) { - throw new IllegalArgumentException("null arguments"); - } + checkArgs(args); Map queryParamMap = new HashMap<>(); queryParamMap.put("lifecycle", ""); @@ -4685,9 +4803,7 @@ public String getBucketLifeCycle(GetBucketLifeCycleArgs args) throws ErrorResponseException, IllegalArgumentException, InsufficientDataException, InternalException, InvalidBucketNameException, InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException { - if (args == null) { - throw new IllegalArgumentException("null arguments"); - } + checkArgs(args); Map queryParamMap = new HashMap<>(); queryParamMap.put("lifecycle", ""); @@ -5353,9 +5469,7 @@ public void setBucketEncryption(SetBucketEncryptionArgs args) throws ErrorResponseException, IllegalArgumentException, InsufficientDataException, InternalException, InvalidBucketNameException, InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException { - if (args == null) { - throw new IllegalArgumentException("null arguments"); - } + checkArgs(args); Map queryParamMap = new HashMap<>(); queryParamMap.put("encryption", ""); @@ -5390,9 +5504,7 @@ public SseConfiguration getBucketEncryption(GetBucketEncryptionArgs args) throws ErrorResponseException, IllegalArgumentException, InsufficientDataException, InternalException, InvalidBucketNameException, InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException { - if (args == null) { - throw new IllegalArgumentException("null arguments"); - } + checkArgs(args); Map queryParamMap = new HashMap<>(); queryParamMap.put("encryption", ""); @@ -5433,9 +5545,7 @@ public void deleteBucketEncryption(DeleteBucketEncryptionArgs args) throws ErrorResponseException, IllegalArgumentException, InsufficientDataException, InternalException, InvalidBucketNameException, InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException { - if (args == null) { - throw new IllegalArgumentException("null arguments"); - } + checkArgs(args); Map queryParamMap = new HashMap<>(); queryParamMap.put("encryption", ""); diff --git a/api/src/main/java/io/minio/SetBucketPolicyArgs.java b/api/src/main/java/io/minio/SetBucketPolicyArgs.java new file mode 100644 index 000000000..0c20c4058 --- /dev/null +++ b/api/src/main/java/io/minio/SetBucketPolicyArgs.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; + +/** Argument class of MinioClient.setBucketPolicy(). */ +public class SetBucketPolicyArgs extends BucketArgs { + private String config; + + public String config() { + return config; + } + + public static Builder builder() { + return new Builder(); + } + + /** Argument builder of {@link SetBucketPolicyArgs}. */ + public static final class Builder extends BucketArgs.Builder { + private void validateConfig(String config) { + if (config == null) { + throw new IllegalArgumentException("null policy configuration"); + } + } + + @Override + protected void validate(SetBucketPolicyArgs args) { + super.validate(args); + validateConfig(args.config); + } + + public Builder config(String config) { + validateConfig(config); + operations.add(args -> args.config = config); + return this; + } + } +} diff --git a/docs/API.md b/docs/API.md index 2b1813e04..47a815cfb 100644 --- a/docs/API.md +++ b/docs/API.md @@ -21,25 +21,26 @@ MinioClient s3Client = new MinioClient("https://s3.amazonaws.com", | [`bucketExists`](#bucketExists) | [`composeObject`](#composeObject) | | [`deleteBucketEncryption`](#deleteBucketEncryption) | [`copyObject`](#copyObject) | | [`deleteBucketLifeCycle`](#deleteBucketLifeCycle) | [`deleteObjectTags`](#deleteObjectTags) | -| [`deleteBucketTags`](#deleteBucketTags) | [`disableObjectLegalHold`](#disableObjectLegalHold) | -| [`disableVersioning`](#disableVersioning) | [`enableObjectLegalHold`](#enableObjectLegalHold) | -| [`enableVersioning`](#enableVersioning) | [`getObject`](#getObject) | -| [`getBucketEncryption`](#getBucketEncryption) | [`getObjectRetention`](#getObjectRetention) | -| [`getBucketLifeCycle`](#getBucketLifeCycle) | [`getObjectTags`](#getObjectTags) | -| [`getBucketNotification`](#getBucketNotification) | [`getObjectUrl`](#getObjectUrl) | -| [`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) | +| [`deleteBucketPolicy`](#deleteBucketPolicy) | [`disableObjectLegalHold`](#disableObjectLegalHold) | +| [`deleteBucketTags`](#deleteBucketTags) | [`enableObjectLegalHold`](#enableObjectLegalHold) | +| [`disableVersioning`](#disableVersioning) | [`getObject`](#getObject) | +| [`enableVersioning`](#enableVersioning) | [`getObjectRetention`](#getObjectRetention) | +| [`getBucketEncryption`](#getBucketEncryption) | [`getObjectTags`](#getObjectTags) | +| [`getBucketLifeCycle`](#getBucketLifeCycle) | [`getObjectUrl`](#getObjectUrl) | +| [`getBucketNotification`](#getBucketNotification) | [`getPresignedObjectUrl`](#getPresignedObjectUrl) | +| [`getBucketPolicy`](#getBucketPolicy) | [`isObjectLegalHoldEnabled`](#isObjectLegalHoldEnabled) | +| [`getBucketTags`](#getBucketTags) | [`listObjects`](#listObjects) | +| [`getDefaultRetention`](#getDefaultRetention) | [`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) | | @@ -337,6 +338,22 @@ __Example__ minioClient.deleteBucketTags(DeleteBucketTagsArgs.builder().bucket("my-bucketname").build()); ``` + +### deleteBucketPolicy(DeleteBucketPolicyArgs args) +`private void deleteBucketPolicy(DeleteBucketPolicyArgs args)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#deleteBucketPolicy-io.minio.DeleteBucketPolicyArgs-)_ + +Deletes bucket policy configuration of a bucket. + +__Parameters__ +| Parameter | Type | Description | +|:----------|:---------------------------|:------------| +| ``args`` | _[DeleteBucketPolicyArgs]_ | Argumnets. | + +__Example__ +```java +minioClient.deleteBucketPolicy(DeleteBucketPolicyArgs.builder().bucket("my-bucketname").build()); +``` + ### disableVersioning(DisableVersioningArgs args) `public void disableVersioning(DisableVersioningArgs args)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#disableVersioning-io.minio.DisableVersioningArgs-)_ @@ -437,15 +454,15 @@ NotificationConfiguration config = minioClient.getBucketNotification("my-bucketn ``` -### getBucketPolicy(String bucketName) -`public String getBucketPolicy(String bucketName)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#getBucketPolicy-java.lang.String-)_ +### getBucketPolicy(GetBucketPolicyArgs args) +`public String getBucketPolicy(GetBucketPolicyArgs args)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#getBucketPolicy-io.minio.GetBucketPolicyArgs-)_ Gets bucket policy configuration of a bucket. __Parameters__ -| Parameter | Type | Description | -|:---------------|:---------|:--------------------| -| ``bucketName`` | _String_ | Name of the bucket. | +| Parameter | Type | Description | +|:----------|:------------------------|:------------| +| ``args`` | _[GetBucketPolicyArgs]_ | Arguments. | | Returns | @@ -454,7 +471,8 @@ __Parameters__ __Example__ ```java -String config = minioClient.getBucketPolicy("my-bucketname"); +String config = + minioClient.getBucketPolicy(GetBucketPolicyArgs.builder().bucket("my-bucketname").build()); ``` @@ -828,17 +846,16 @@ minioClient.setBucketNotification("my-bucketname", config); ``` -### setBucketPolicy(String bucketName, String policy) -`public void setBucketPolicy(String bucketName, String policy)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#setBucketPolicy-java.lang.String-java.lang.String-)_ +### setBucketPolicy(SetBucketPolicyArgs args) +`public void setBucketPolicy(SetBucketPolicyArgs args)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#setBucketPolicy-io.minio.SetBucketPolicyArgs-)_ Sets bucket policy configuration to a bucket. __Parameters__ -| Parameter | Type | Description | -|:---------------|:---------|:--------------------------------------------| -| ``bucketName`` | _String_ | Name of the bucket. | -| ``policy`` | _String_ | Bucket policy configuration as JSON string. | +| Parameter | Type | Description | +|:----------|:------------------------|:------------| +| ``args`` | _[SetBucketPolicyArgs]_ | Arguments. | __Example__ ```java @@ -864,7 +881,8 @@ __Example__ // "Version": "2012-10-17" // } // -minioClient.setBucketPolicy("my-bucketname", policyJson); +minioClient.setBucketPolicy( + SetBucketPolicyArgs.builder().bucket("my-bucketname").config(policyJson).build()); ``` @@ -1771,3 +1789,6 @@ ObjectStat objectStat = [DeleteBucketLifeCycleArgs]: http://minio.github.io/minio-java/io/minio/DeleteBucketLifeCycleArgs.html [GetBucketLifeCycleArgs]: http://minio.github.io/minio-java/io/minio/GetBucketLifeCycleArgs.html [SetBucketLifeCycleArgs]: http://minio.github.io/minio-java/io/minio/SetBucketLifeCycleArgs.html +[GetBucketPolicyArgs]: http://minio.github.io/minio-java/io/minio/GetBucketPolicyArgs.html +[SetBucketPolicyArgs]: http://minio.github.io/minio-java/io/minio/SetBucketPolicyArgs.html +[DeleteBucketPolicyArgs]: http://minio.github.io/minio-java/io/minio/DeleteBucketPolicyArgs.html diff --git a/examples/DeleteBucketPolicy.java b/examples/DeleteBucketPolicy.java new file mode 100644 index 000000000..32d5f8209 --- /dev/null +++ b/examples/DeleteBucketPolicy.java @@ -0,0 +1,47 @@ +/* + * 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.DeleteBucketPolicyArgs; +import io.minio.MinioClient; +import io.minio.errors.MinioException; +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +public class DeleteBucketPolicy { + /** MinioClient.deleteBucketPolicy() 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"); + + minioClient.deleteBucketPolicy( + DeleteBucketPolicyArgs.builder().bucket("my-bucketname").build()); + System.out.println("Bucket policy is deleted successfully"); + } catch (MinioException e) { + System.out.println("Error occurred: " + e); + } + } +} diff --git a/examples/GetBucketPolicy.java b/examples/GetBucketPolicy.java index 0020317f8..4ede6ef1c 100644 --- a/examples/GetBucketPolicy.java +++ b/examples/GetBucketPolicy.java @@ -14,6 +14,7 @@ * limitations under the License. */ +import io.minio.GetBucketPolicyArgs; import io.minio.MinioClient; import io.minio.errors.MinioException; import java.io.IOException; @@ -36,7 +37,9 @@ public static void main(String[] args) // MinioClient minioClient = new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID", // "YOUR-SECRETACCESSKEY"); - String policy = minioClient.getBucketPolicy("my-bucketname"); + String policy = + minioClient.getBucketPolicy( + GetBucketPolicyArgs.builder().bucket("my-bucketname").build()); System.out.println("Current policy: " + policy); } catch (MinioException e) { System.out.println("Error occurred: " + e); diff --git a/examples/SetBucketPolicy.java b/examples/SetBucketPolicy.java index 5ca267cfb..faa0ab978 100644 --- a/examples/SetBucketPolicy.java +++ b/examples/SetBucketPolicy.java @@ -15,6 +15,7 @@ */ import io.minio.MinioClient; +import io.minio.SetBucketPolicyArgs; import io.minio.errors.MinioException; import java.io.IOException; import java.security.InvalidKeyException; @@ -57,7 +58,8 @@ public static void main(String[] args) builder.append(" ],\n"); builder.append(" \"Version\": \"2012-10-17\"\n"); builder.append("}\n"); - minioClient.setBucketPolicy("my-bucketname", builder.toString()); + minioClient.setBucketPolicy( + SetBucketPolicyArgs.builder().bucket("my-bucketname").config(builder.toString()).build()); } catch (MinioException e) { System.out.println("Error occurred: " + e); } diff --git a/functional/FunctionalTest.java b/functional/FunctionalTest.java index 913aee733..8c1fe7a47 100644 --- a/functional/FunctionalTest.java +++ b/functional/FunctionalTest.java @@ -24,6 +24,7 @@ import io.minio.CopyConditions; import io.minio.DeleteBucketEncryptionArgs; import io.minio.DeleteBucketLifeCycleArgs; +import io.minio.DeleteBucketPolicyArgs; import io.minio.DeleteBucketTagsArgs; import io.minio.DeleteObjectTagsArgs; import io.minio.DisableVersioningArgs; @@ -31,6 +32,7 @@ import io.minio.ErrorCode; import io.minio.GetBucketEncryptionArgs; import io.minio.GetBucketLifeCycleArgs; +import io.minio.GetBucketPolicyArgs; import io.minio.GetBucketTagsArgs; import io.minio.GetObjectRetentionArgs; import io.minio.GetObjectTagsArgs; @@ -47,6 +49,7 @@ import io.minio.ServerSideEncryption; import io.minio.SetBucketEncryptionArgs; import io.minio.SetBucketLifeCycleArgs; +import io.minio.SetBucketPolicyArgs; import io.minio.SetBucketTagsArgs; import io.minio.SetObjectRetentionArgs; import io.minio.SetObjectTagsArgs; @@ -3091,11 +3094,11 @@ public static void copyObject_test11() throws Exception { * SSE_KMS. */ public static void copyObject_test12() throws Exception { + String methodName = + "SSE-KMS: copyObject(String bucketName, String objectName, ServerSideEncryption sseSource, " + + "String destBucketName, CopyConditions copyConditions, ServerSideEncryption sseTarget)"; if (!mintEnv) { - System.out.println( - "Test: copyObject(String bucketName, String objectName, ServerSideEncryption sseSource, " - + "String destBucketName, CopyConditions copyConditions, ServerSideEncryption sseTarget)" - + " using SSE_KMS. "); + System.out.println("Test: " + methodName); } long startTime = System.currentTimeMillis(); @@ -3108,7 +3111,7 @@ public static void copyObject_test12() throws Exception { String keyId = ""; keyId = System.getenv("MINT_KEY_ID"); if (keyId.equals("")) { - mintIgnoredLog("getBucketPolicy(String bucketName)", null, startTime); + mintIgnoredLog(methodName, null, startTime); } ServerSideEncryption sse = ServerSideEncryption.withManagedKeys("keyId", myContext); @@ -3131,17 +3134,10 @@ public static void copyObject_test12() throws Exception { } client.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build()); - mintSuccessLog( - "copyObject(String bucketName, String objectName, ServerSideEncryption sseSource, " - + "String destBucketName, CopyConditions copyConditions, ServerSideEncryption sseTarget)" - + " using SSE_KMS.", - null, - startTime); + mintSuccessLog(methodName, null, startTime); } catch (Exception e) { mintFailedLog( - "copyObject(String bucketName, String objectName, ServerSideEncryption sseSource, " - + "String destBucketName, CopyConditions copyConditions, ServerSideEncryption sseTarget)" - + " using SSE_KMS.", + methodName, null, startTime, null, @@ -3952,10 +3948,11 @@ public static void getObjectRetention_test1() throws Exception { } } - /** Test: getBucketPolicy(String bucketName). */ + /** Test: getBucketPolicy(GetBucketPolicyArgs args). */ public static void getBucketPolicy_test1() throws Exception { + String methodName = "getBucketPolicy(GetBucketPolicyArgs args)"; if (!mintEnv) { - System.out.println("Test: getBucketPolicy(String bucketName)"); + System.out.println("Test: " + methodName); } long startTime = System.currentTimeMillis(); @@ -3965,35 +3962,20 @@ public static void getBucketPolicy_test1() throws Exception { + "\"Principal\":{\"AWS\":[\"*\"]},\"Resource\":[\"arn:aws:s3:::" + bucketName + "/myobject*\"],\"Sid\":\"\"}]}"; - client.setBucketPolicy(bucketName, policy); - client.getBucketPolicy(bucketName); - mintSuccessLog("getBucketPolicy(String bucketName)", null, startTime); + client.setBucketPolicy( + SetBucketPolicyArgs.builder().bucket(bucketName).config(policy).build()); + client.getBucketPolicy(GetBucketPolicyArgs.builder().bucket(bucketName).build()); + 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("getBucketPolicy(String bucketName)", null, startTime); - } else { - mintFailedLog( - "getBucketPolicy(String bucketName)", - null, - startTime, - null, - e.toString() + " >>> " + Arrays.toString(e.getStackTrace())); - throw e; - } + handleException(methodName, null, startTime, e); } } - /** Test: setBucketPolicy(String bucketName, String policy). */ + /** Test: setBucketPolicy(SetBucketPolicyArgs args). */ public static void setBucketPolicy_test1() throws Exception { + String methodName = "setBucketPolicy(SetBucketPolicyArgs args)"; if (!mintEnv) { - System.out.println("Test: setBucketPolicy(String bucketName, String policy)"); + System.out.println("Test: " + methodName); } long startTime = System.currentTimeMillis(); @@ -4003,30 +3985,34 @@ public static void setBucketPolicy_test1() throws Exception { + "\"*\",\"Resource\":\"arn:aws:s3:::" + bucketName + "/myobject*\"}],\"Version\": \"2012-10-17\"}"; - client.setBucketPolicy(bucketName, policy); - mintSuccessLog("setBucketPolicy(String bucketName, String policy)", null, startTime); + client.setBucketPolicy( + SetBucketPolicyArgs.builder().bucket(bucketName).config(policy).build()); + mintSuccessLog(methodName, null, startTime); } catch (Exception e) { - ErrorResponse errorResponse = null; - if (e instanceof ErrorResponseException) { - ErrorResponseException exp = (ErrorResponseException) e; - errorResponse = exp.errorResponse(); - } + handleException(methodName, null, startTime, e); + } + } - // Ignore NotImplemented error - if (errorResponse != null && errorResponse.errorCode() == ErrorCode.NOT_IMPLEMENTED) { - mintIgnoredLog( - "setBucketPolicy(String bucketName, String objectPrefix, " + "PolicyType policyType)", - null, - startTime); - } else { - mintFailedLog( - "setBucketPolicy(String bucketName, String objectPrefix, " + "PolicyType policyType)", - null, - startTime, - null, - e.toString() + " >>> " + Arrays.toString(e.getStackTrace())); - throw e; - } + /** Test: deleteBucketPolicy(deleteBucketPolicyArgs args). */ + public static void deleteBucketPolicy_test1() throws Exception { + String methodName = "deleteBucketPolicy(DeleteBucketPolicyArgs args)"; + if (!mintEnv) { + System.out.println("Test: " + methodName); + } + + long startTime = System.currentTimeMillis(); + try { + String policy = + "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":[\"s3:GetObject\"],\"Effect\":\"Allow\"," + + "\"Principal\":{\"AWS\":[\"*\"]},\"Resource\":[\"arn:aws:s3:::" + + bucketName + + "/myobject*\"],\"Sid\":\"\"}]}"; + client.setBucketPolicy( + SetBucketPolicyArgs.builder().bucket(bucketName).config(policy).build()); + client.deleteBucketPolicy(DeleteBucketPolicyArgs.builder().bucket(bucketName).build()); + mintSuccessLog(methodName, null, startTime); + } catch (Exception e) { + handleException(methodName, null, startTime, e); } } @@ -4892,6 +4878,7 @@ public static void runTests() throws Exception { getBucketPolicy_test1(); setBucketPolicy_test1(); + deleteBucketPolicy_test1(); listenBucketNotification_test1(); @@ -4929,6 +4916,7 @@ public static void runQuickTests() throws Exception { copyObject_test1(); getBucketPolicy_test1(); setBucketPolicy_test1(); + deleteBucketPolicy_test1(); selectObjectContent_test1(); listenBucketNotification_test1(); setBucketTags_test();