From 322b6ef9fe33a26e489fd13bc1b7086cc243a362 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Sinha Date: Sun, 11 Aug 2019 04:48:59 +0530 Subject: [PATCH] Add success action status to PresignedPostPolicy (#784) --- api/src/main/java/io/minio/PostPolicy.java | 19 +++++++++++ .../java/io/minio/SuccessActionStatus.java | 33 +++++++++++++++++++ docs/API.md | 9 +++-- examples/PresignedPostPolicy.java | 4 +++ 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 api/src/main/java/io/minio/SuccessActionStatus.java diff --git a/api/src/main/java/io/minio/PostPolicy.java b/api/src/main/java/io/minio/PostPolicy.java index bb53882a2..7a1346ed3 100644 --- a/api/src/main/java/io/minio/PostPolicy.java +++ b/api/src/main/java/io/minio/PostPolicy.java @@ -31,6 +31,7 @@ import com.google.common.io.BaseEncoding; import io.minio.errors.InvalidArgumentException; +import io.minio.SuccessActionStatus; /** @@ -44,6 +45,7 @@ public class PostPolicy { private final boolean startsWith; private final DateTime expirationDate; private String contentType; + private int successActionStatus; private String contentEncoding; private long contentRangeStart; private long contentRangeEnd; @@ -91,6 +93,18 @@ public void setContentType(String contentType) throws InvalidArgumentException { this.contentType = contentType; } + /** + * Sets success action status. + */ + public void setSuccessActionStatus(int successActionStatus) throws InvalidArgumentException { + if (!(successActionStatus == SuccessActionStatus.SuccessActionStatus200.getValue() + || successActionStatus == SuccessActionStatus.SuccessActionStatus201.getValue() + || successActionStatus == SuccessActionStatus.SuccessActionStatus204.getValue())) { + throw new InvalidArgumentException("Invalid action status, acceptable values are 200, 201, or 204"); + } + + this.successActionStatus = successActionStatus; + } /** * Sets content encoding. @@ -211,6 +225,11 @@ protected Map makeFormData(String accessKey, String secretKey, St formData.put("Content-Encoding", this.contentEncoding); } + if (this.successActionStatus > 0) { + conditions.add(new String[]{"eq", "success_action_status", Integer.toString(this.successActionStatus)}); + formData.put("success_action_status", Integer.toString(this.successActionStatus)); + } + if (this.contentRangeStart > 0 && this.contentRangeEnd > 0) { conditions.add(new String[]{"content-length-range", Long.toString(this.contentRangeStart), Long.toString(this.contentRangeEnd)}); diff --git a/api/src/main/java/io/minio/SuccessActionStatus.java b/api/src/main/java/io/minio/SuccessActionStatus.java new file mode 100644 index 000000000..a081b7faf --- /dev/null +++ b/api/src/main/java/io/minio/SuccessActionStatus.java @@ -0,0 +1,33 @@ +/* + * MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2019 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; + +public enum SuccessActionStatus { + + SuccessActionStatus201(201), SuccessActionStatus200(200), SuccessActionStatus204(204); + + private final int value; + + private SuccessActionStatus(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + +} diff --git a/docs/API.md b/docs/API.md index 842f8d27b..f9074bd4e 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2992,7 +2992,7 @@ try { `public Map presignedPostPolicy(PostPolicy policy)` -Allows setting policy conditions to a presigned URL for POST operations. Policies such as bucket name to receive object uploads, key name prefixes, expiry policy may be set. +Allows setting policy conditions to a presigned URL for POST operations. Policies such as bucket name to receive object uploads, key name prefixes, expiry policy may be set. The client receives HTTP status code under key success_action_status, when the file is uploaded successfully. If its value is set to 201, the client notifies with a XML document containing the key where the file was uploaded to. [View Javadoc](http://minio.github.io/minio-java/io/minio/MinioClient.html#presignedPostPolicy-io.minio.PostPolicy-) @@ -3022,10 +3022,13 @@ __Example__ ```java try { - PostPolicy policy = new PostPolicy("mybucket", "myobject", - DateTime.now().plusDays(7)); + PostPolicy policy = new PostPolicy("mybucket", "myobject", DateTime.now().plusDays(7)); + // 'my-objectname' should be 'image/png' content type policy.setContentType("image/png"); + // set success action status to 201 to receive XML document + policy.setSuccessActionStatus(201); Map formData = minioClient.presignedPostPolicy(policy); + // Print a curl command that can be executable with the file /tmp/userpic.png and the file will be uploaded. System.out.print("curl -X POST "); for (Map.Entry entry : formData.entrySet()) { System.out.print(" -F " + entry.getKey() + "=" + entry.getValue()); diff --git a/examples/PresignedPostPolicy.java b/examples/PresignedPostPolicy.java index 8464a3952..2ad994bf1 100644 --- a/examples/PresignedPostPolicy.java +++ b/examples/PresignedPostPolicy.java @@ -45,6 +45,10 @@ public static void main(String[] args) PostPolicy policy = new PostPolicy("my-bucketname", "my-objectname", DateTime.now().plusDays(7)); // 'my-objectname' should be 'image/png' content type policy.setContentType("image/png"); + // set success action status to 201 because we want the client to notify us with the S3 key + // where the file was uploaded to. + policy.setSuccessActionStatus(201); + Map formData = minioClient.presignedPostPolicy(policy); // Print a curl command that can be executable with the file /tmp/userpic.png and the file will be uploaded.