Reputation: 16148
I have problems with the s3 post form. I read though http://aws.amazon.com/articles/1434/
I followed the whole tutorial:
import sun.misc.BASE64Encoder;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class Storage {
public static String awsAccessKey = "xxxxxxxxxxxxxxx";
public static String awsSecretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
static String policy_document = "{\"expiration\": \"2013-01-01T00:00:00Z\","
+ "\"conditions\": ["
+ "{\"bucket\": \"kanta-assets\"},"
+ "[\"starts-with\", \"$key\", \"\"],"
+ "{\"acl\": \"public\"},"
+ "{\"success_action_redirect\": \"http://localhost/\"},"
+ "[\"starts-with\", \"$Content-Type\", \"\"],"
+ "[\"content-length-range\", 0, 1048576]" + "]" + "}";
public static String getPolicy() throws UnsupportedEncodingException {
return (new BASE64Encoder()).encode(policy_document.getBytes("UTF-8"))
.replaceAll("\n", "").replaceAll("\r", "");
}
public static String getSignature() throws InvalidKeyException,
IllegalStateException, NoSuchAlgorithmException,
UnsupportedEncodingException {
Mac hmac = Mac.getInstance("HmacSHA1");
hmac.init(new SecretKeySpec(awsSecretKey.getBytes("UTF-8"), "HmacSHA1"));
return (new BASE64Encoder()).encode(
hmac.doFinal(getPolicy().getBytes("UTF-8"))).replaceAll("\n",
"");
}
}
and my post form looks like this
<form action="https://kanta-assets.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="uploads/${filename}">
<input type="hidden" name="AWSAccessKeyId" value="@appconfig.Storage.awsAccessKey">
<input type="hidden" name="acl" value="public">
<input type="hidden" name="success_action_redirect" value="http://localhost/">
<input type="hidden" name="policy" value="@appconfig.Storage.getPolicy()">
<input type="hidden" name="signature" value="@appconfig.Storage.getSignature()">
<input type="hidden" name="Content-Type" value="image/jpeg">
<!-- Include any additional input fields here -->
File to upload to S3:
<input name="file" type="file">
<br>
<input type="submit" value="Upload File to S3">
</form>
If I try to upload an jpeg image I get the following error:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>InvalidArgument</Code>
<Message/>
<ArgumentValue>public</ArgumentValue>
<ArgumentName>x-amz-acl</ArgumentName>
<RequestId>6679060CE7C84FA5</RequestId>
<HostId>
wSIzNZvFDFT7WNnUtBq9UY5WSN1ltN9dHErNk6L3v4ciZCSzwUgzTf1PgaFAJTWD
</HostId>
</Error>
I am pretty sure that my mistake is the key value in the policy "[\"starts-with\", \"$key\", \"\"],"
I wasn't sure what this actually means.
The rule for the object’s key name uses the prefix string “upload/”, which means that the key value must always start with the upload/ subdirectory path.
Upvotes: 1
Views: 1571
Reputation: 439
POST / HTTP/1.1 Host: destinationBucket.s3.amazonaws.com User-Agent: browser_data Accept: file_types Accept-Language: Regions Accept-Encoding: encoding Accept-Charset: character_set Keep-Alive: 300 Connection: keep-alive Content-Type: multipart/form-data; boundary=9431149156168 Content-Length: length
--9431149156168 Content-Disposition: form-data; name="key"
acl --9431149156168 Content-Disposition: form-data; name="success_action_redirect"
success_redirect --9431149156168 Content-Disposition: form-data; name="Content-Type"
content_type --9431149156168 Content-Disposition: form-data; name="x-amz-meta-uuid"
uuid --9431149156168 Content-Disposition: form-data; name="x-amz-meta-tag"
metadata --9431149156168 Content-Disposition: form-data; name="AWSAccessKeyId"
access-key-id --9431149156168 Content-Disposition: form-data; name="Policy"
encoded_policy --9431149156168 Content-Disposition: form-data; name="Signature"
signature= --9431149156168 Content-Disposition: form-data; name="file"; filename="MyFilename.jpg" Content-Type: image/jpeg
file_content --9431149156168 Content-Disposition: form-data; name="submit"
Upload to Amazon S3 --9431149156168--
How to cerate post request in java?
Upvotes: 0