Skip to content

Commit

Permalink
Add {get,set,delete}BucketCors APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Bala.FA <[email protected]>
  • Loading branch information
balamurugana committed Jan 26, 2025
1 parent 0f533e8 commit 2d08b1f
Show file tree
Hide file tree
Showing 10 changed files with 735 additions and 0 deletions.
30 changes: 30 additions & 0 deletions api/src/main/java/io/minio/DeleteBucketCorsArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 {@link MinioAsyncClient#deleteBucketCors} and {@link
* MinioClient#deleteBucketCors}.
*/
public class DeleteBucketCorsArgs extends BucketArgs {
public static Builder builder() {
return new Builder();
}

/** Argument builder of {@link DeleteBucketCorsArgs}. */
public static final class Builder extends BucketArgs.Builder<Builder, DeleteBucketCorsArgs> {}
}
29 changes: 29 additions & 0 deletions api/src/main/java/io/minio/GetBucketCorsArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 {@link MinioAsyncClient#getBucketCors} and {@link MinioClient#getBucketCors}.
*/
public class GetBucketCorsArgs extends BucketArgs {
public static Builder builder() {
return new Builder();
}

/** Argument builder of {@link GetBucketCorsArgs}. */
public static final class Builder extends BucketArgs.Builder<Builder, GetBucketCorsArgs> {}
}
128 changes: 128 additions & 0 deletions api/src/main/java/io/minio/MinioAsyncClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.minio.http.HttpUtils;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.CORSConfiguration;
import io.minio.messages.CopyObjectResult;
import io.minio.messages.CreateBucketConfiguration;
import io.minio.messages.DeleteError;
Expand Down Expand Up @@ -3151,6 +3152,133 @@ public CompletableFuture<Void> deleteObjectTags(DeleteObjectTagsArgs args)
return executeDeleteAsync(args, null, queryParams).thenAccept(response -> response.close());
}

/**
* Gets CORS configuration of a bucket.
*
* <pre>Example:{@code
* CompletableFuture<CORSConfiguration> future =
* minioAsyncClient.getBucketCors(GetBucketCorsArgs.builder().bucket("my-bucketname").build());
* }</pre>
*
* @param args {@link GetBucketCorsArgs} object.
* @return {@link CompletableFuture}&lt;{@link CORSConfiguration}&gt; object.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
* @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 CompletableFuture<CORSConfiguration> getBucketCors(GetBucketCorsArgs args)
throws InsufficientDataException, InternalException, InvalidKeyException, IOException,
NoSuchAlgorithmException, XmlParserException {
checkArgs(args);
return executeGetAsync(args, null, newMultimap("cors", ""))
.exceptionally(
e -> {
Throwable ex = e.getCause();

if (ex instanceof CompletionException) {
ex = ((CompletionException) ex).getCause();
}

if (ex instanceof ExecutionException) {
ex = ((ExecutionException) ex).getCause();
}

if (ex instanceof ErrorResponseException) {
if (((ErrorResponseException) ex).errorResponse().code().equals("NoSuchTagSet")) {
return null;
}
}
throw new CompletionException(ex);
})
.thenApply(
response -> {
if (response == null) return new CORSConfiguration(null);
try {
return Xml.unmarshal(CORSConfiguration.class, response.body().charStream());
} catch (XmlParserException e) {
throw new CompletionException(e);
} finally {
response.close();
}
});
}

/**
* Sets CORS configuration to a bucket.
*
* <pre>Example:{@code
* CORSConfiguration config =
* new CORSConfiguration(
* Arrays.asList(
* new CORSConfiguration.CORSRule[] {
* // Rule 1
* new CORSConfiguration.CORSRule(
* Arrays.asList(new String[] {"*"}), // Allowed headers
* Arrays.asList(new String[] {"PUT", "POST", "DELETE"}), // Allowed methods
* Arrays.asList(new String[] {"http://www.example.com"}), // Allowed origins
* Arrays.asList(
* new String[] {"x-amz-server-side-encryption"}), // Expose headers
* null, // ID
* 3000), // Maximum age seconds
* // Rule 2
* new CORSConfiguration.CORSRule(
* null, // Allowed headers
* Arrays.asList(new String[] {"GET"}), // Allowed methods
* Arrays.asList(new String[] {"*"}), // Allowed origins
* null, // Expose headers
* null, // ID
* null // Maximum age seconds
* )
* }));
* CompletableFuture<Void> future = minioAsyncClient.setBucketCors(
* SetBucketCorsArgs.builder().bucket("my-bucketname").config(config).build());
* }</pre>
*
* @param args {@link SetBucketCorsArgs} object.
* @return {@link CompletableFuture}&lt;{@link Void}&gt; object.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
* @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 CompletableFuture<Void> setBucketCors(SetBucketCorsArgs args)
throws InsufficientDataException, InternalException, InvalidKeyException, IOException,
NoSuchAlgorithmException, XmlParserException {
checkArgs(args);
return executePutAsync(args, null, newMultimap("cors", ""), args.config(), 0)
.thenAccept(response -> response.close());
}

/**
* Deletes CORS configuration of a bucket.
*
* <pre>Example:{@code
* CompletableFuture<Void> future = minioAsyncClient.deleteBucketCors(
* DeleteBucketCorsArgs.builder().bucket("my-bucketname").build());
* }</pre>
*
* @param args {@link DeleteBucketCorsArgs} object.
* @return {@link CompletableFuture}&lt;{@link Void}&gt; object.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
* @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 CompletableFuture<Void> deleteBucketCors(DeleteBucketCorsArgs args)
throws InsufficientDataException, InternalException, InvalidKeyException, IOException,
NoSuchAlgorithmException, XmlParserException {
checkArgs(args);
return executeDeleteAsync(args, null, newMultimap("cors", ""))
.thenAccept(response -> response.close());
}

/**
* Uploads multiple objects in a single put call. It is done by creating intermediate TAR file
* optionally compressed which is uploaded to S3 service.
Expand Down
121 changes: 121 additions & 0 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import io.minio.messages.Bucket;
import io.minio.messages.CORSConfiguration;
import io.minio.messages.DeleteError;
import io.minio.messages.Item;
import io.minio.messages.LifecycleConfiguration;
Expand Down Expand Up @@ -2275,6 +2276,126 @@ public void deleteObjectTags(DeleteObjectTagsArgs args)
}
}

/**
* Gets CORS configuration of a bucket.
*
* <pre>Example:{@code
* CORSConfiguration config =
* minioClient.getBucketCors(GetBucketCorsArgs.builder().bucket("my-bucketname").build());
* }</pre>
*
* @param args {@link GetBucketCorsArgs} object.
* @return {@link CORSConfiguration} - CORSConfiguration.
* @throws ErrorResponseException thrown to indicate S3 service returned an error response.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @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 CORSConfiguration getBucketCors(GetBucketCorsArgs args)
throws ErrorResponseException, InsufficientDataException, InternalException,
InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException,
ServerException, XmlParserException {
try {
return asyncClient.getBucketCors(args).get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
asyncClient.throwEncapsulatedException(e);
return null;
}
}

/**
* Sets CORS configuration to a bucket.
*
* <pre>Example:{@code
* CORSConfiguration config =
* new CORSConfiguration(
* Arrays.asList(
* new CORSConfiguration.CORSRule[] {
* // Rule 1
* new CORSConfiguration.CORSRule(
* Arrays.asList(new String[] {"*"}), // Allowed headers
* Arrays.asList(new String[] {"PUT", "POST", "DELETE"}), // Allowed methods
* Arrays.asList(new String[] {"http://www.example.com"}), // Allowed origins
* Arrays.asList(
* new String[] {"x-amz-server-side-encryption"}), // Expose headers
* null, // ID
* 3000), // Maximum age seconds
* // Rule 2
* new CORSConfiguration.CORSRule(
* null, // Allowed headers
* Arrays.asList(new String[] {"GET"}), // Allowed methods
* Arrays.asList(new String[] {"*"}), // Allowed origins
* null, // Expose headers
* null, // ID
* null // Maximum age seconds
* )
* }));
* minioClient.setBucketCors(
* SetBucketCorsArgs.builder().bucket("my-bucketname").config(config).build());
* }</pre>
*
* @param args {@link SetBucketCorsArgs} object.
* @throws ErrorResponseException thrown to indicate S3 service returned an error response.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @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 setBucketCors(SetBucketCorsArgs args)
throws ErrorResponseException, InsufficientDataException, InternalException,
InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException,
ServerException, XmlParserException {
try {
asyncClient.setBucketCors(args).get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
asyncClient.throwEncapsulatedException(e);
}
}

/**
* Deletes CORS configuration of a bucket.
*
* <pre>Example:{@code
* minioClient.deleteBucketCors(DeleteBucketCorsArgs.builder().bucket("my-bucketname").build());
* }</pre>
*
* @param args {@link DeleteBucketCorsArgs} object.
* @throws ErrorResponseException thrown to indicate S3 service returned an error response.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @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 deleteBucketCors(DeleteBucketCorsArgs args)
throws ErrorResponseException, InsufficientDataException, InternalException,
InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException,
ServerException, XmlParserException {
try {
asyncClient.deleteBucketCors(args).get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
asyncClient.throwEncapsulatedException(e);
}
}

/**
* Uploads multiple objects in a single put call. It is done by creating intermediate TAR file
* optionally compressed which is uploaded to S3 service.
Expand Down
Loading

0 comments on commit 2d08b1f

Please sign in to comment.