java - Upload file to S3 Bucket using HTTP POST -
i'm developing application make use of s3 storage amazon. after following tutorials , examples provided amazon still find myself unable the upload work. message keep getting: the authorization mechanism have provided not supported. please use aws4-hmac-sha256. bucket named testbucket-10.09.2017 , located in frankfurt (eu-central-1) region. found application claiming need error message same. bulk of code below has been taken , adapted documentation , tutorials provided aws. appreciated.
here code using:
my html form:
<html> <head> <title>s3 post form</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> </head> <body> <form action="https://testbucket-10.09.2017.s3.amazonaws.com/" method="post" enctype="multipart/form-data"> <input type="hidden" name="key" value="uploads/${filename}"> <input type="hidden" name="awsaccesskeyid" value="removed safety"> <input type="hidden" name="acl" value="private"> <input type="hidden" name="success_action_redirect" value="http://localhost/"> <input type="hidden" name="policy" value="zxlkbgviqnbjbuywyvc5dulqb2djakl3tvrnde1erxrnrezvturbnk1eqtznrejhswl3s0ldqwlzmjl1wkdsmgfxoxvjeuk2suzzz0npqwdjq0i3sw1kmvkydgxkq0k2sunkmfpytjbzblzqytjwmexurxdmake1tgpjd01uy2lmu3dnq2lbz0ldqmjjbk4wwvhkmgn5mtnhwfjvswl3z0lpunjawgtptenbawryqnnimkzry3k4avhtd0tjq0fnsuhzavlxtnnjam9nsw5cewfywmhkr1vpzln3s0ldqwdjshnpyznwalkyvnpjmtlowtnscgiynwzjbvzryvhkbfkzuwlpaufpyuhsmgneb3zmmnh2wtjgc2fhoxpkqzhpzln3s0ldqwdjrnnpyznsagnuunpmwgrwzednauxdqwlkru52ym5sbgjuuxrwsgx3wlnjc0ldswlyu3dlsunbz0lgc2lzmjl1zedwdwrdmxnavzvuzedndgntrnvamlvptenbd0xdqxhnrfe0tlrjmlhrb2djrjblzle9pq=="> <input type="hidden" name="signature" value="removed safety"> <input type="hidden" name="content-type" value="image/jpeg"> <!-- include additional input fields here --> file upload s3: <input name="file" type="file"> <br> <input type="submit" value="upload file s3"> </form> </body> </html> my java code generating policy , signature:
public static void myattempt() throws exception { string policy_document = constructpolicy(); string aws_secret_key="removed safety"; string policy = (new base64encoder()).encode( policy_document.getbytes("utf-8")).replaceall("\n","").replaceall("\r",""); string datestamp ="20170912"; string region = "eu-central-1"; string servicename ="s3"; system.out.println("new signature: "+getsignature(getsignaturekey(aws_secret_key,datestamp,region,servicename))); system.out.println("encoded policy: "+policy); } private static string constructpolicy() throws unsupportedencodingexception { string policy_document="{\"expiration\": \"2018-01-01t00:00:00z\",\n" + " \"conditions\": [ \n" + " {\"bucket\": \"testbucket-10.09.2017\"}, \n" + " [\"starts-with\", \"$key\", \"uploads/\"],\n" + " {\"acl\": \"private\"},\n" + " {\"success_action_redirect\": \"http://localhost/\"},\n" + " [\"starts-with\", \"$content-type\", \"\"],\n" + " [\"content-length-range\", 0, 1048576]\n" + " ]\n" + "}"; string policy = (new base64encoder()).encode( policy_document.getbytes("utf-8")).replaceall("\n","").replaceall("\r",""); return policy; } private static byte[] hmacsha256(string data, byte[] key) throws exception { string algorithm="hmacsha256"; mac mac = mac.getinstance(algorithm); mac.init(new secretkeyspec(key, algorithm)); return mac.dofinal(data.getbytes("utf8")); } private static byte[] getsignaturekey(string key, string datestamp, string regionname, string servicename) throws exception { byte[] ksecret = ("aws4" + key).getbytes("utf8"); byte[] kdate = hmacsha256(datestamp, ksecret); byte[] kregion = hmacsha256(regionname, kdate); byte[] kservice = hmacsha256(servicename, kregion); byte[] ksigning = hmacsha256("aws4_request", kservice); return ksigning; } private static string getsignature(byte[] key) throws exception{ return base16().lowercase().encode(hmacsha256(constructpolicy(), key)); }
turns out reason outdated aws documentation , examples among first results when doing search. few google results pages later, more date example came up. form using wrong. correct 1 follows:
<html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> </head> <body> <form action="http://sigv4examplebucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data"> key upload: <input type="input" name="key" value="user/user1/${filename}" /><br /> <input type="hidden" name="acl" value="public-read" /> <input type="hidden" name="success_action_redirect" value="http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html" /> content-type: <input type="input" name="content-type" value="image/jpeg" /><br /> <input type="hidden" name="x-amz-meta-uuid" value="14365123651274" /> <input type="hidden" name="x-amz-server-side-encryption" value="aes256" /> <input type="text" name="x-amz-credential" value="akiaiosfodnn7example/20151229/us-east-1/s3/aws4_request" /> <input type="text" name="x-amz-algorithm" value="aws4-hmac-sha256" /> <input type="text" name="x-amz-date" value="20151229t000000z" /> tags file: <input type="input" name="x-amz-meta-tag" value="" /><br /> <input type="hidden" name="policy" value='<base64-encoded policy string>' /> <input type="hidden" name="x-amz-signature" value="<signature-value>" /> file: <input type="file" name="file" /> <br /> <!-- elements after ignored --> <input type="submit" name="submit" value="upload amazon s3" /> </form> </html> here link got form , additional examples can found.
Comments
Post a Comment