Skip to content

Commit

Permalink
Add success action status to PresignedPostPolicy (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinhaashish authored and harshavardhana committed Aug 10, 2019
1 parent 33fd018 commit 322b6ef
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
19 changes: 19 additions & 0 deletions api/src/main/java/io/minio/PostPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.common.io.BaseEncoding;

import io.minio.errors.InvalidArgumentException;
import io.minio.SuccessActionStatus;


/**
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -211,6 +225,11 @@ protected Map<String,String> 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)});
Expand Down
33 changes: 33 additions & 0 deletions api/src/main/java/io/minio/SuccessActionStatus.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
9 changes: 6 additions & 3 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -2992,7 +2992,7 @@ try {

`public Map<String,String> 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-)

Expand Down Expand Up @@ -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<String,String> 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<String,String> entry : formData.entrySet()) {
System.out.print(" -F " + entry.getKey() + "=" + entry.getValue());
Expand Down
4 changes: 4 additions & 0 deletions examples/PresignedPostPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String,String> formData = minioClient.presignedPostPolicy(policy);

// Print a curl command that can be executable with the file /tmp/userpic.png and the file will be uploaded.
Expand Down

0 comments on commit 322b6ef

Please sign in to comment.