Snippets

Quan Nguyen upload file to aws s3 from js

Created by Quan Nguyen last modified
    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"));
    }

    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;
    }

    final protected static char[] hexArray = "0123456789abcdef".toCharArray();
    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for ( int j = 0; j < bytes.length; j++ ) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static void main(String[] args) throws Exception {
        System.out.println(bytesToHex(HmacSHA256(base64(policy_text),getSignatureKey(privateKey,"20150921","us-west-2","s3"))));
    }
{ "expiration": "2015-09-21T12:00:00.000Z",
  "conditions": [
    {"bucket": "posiba-givn-dev"},
    ["starts-with", "$key", "images/"],
    {"acl": "public-read"},
    ["starts-with", "$Content-Type", "image/"],

    {"x-amz-credential": "publicKey/20150921/us-west-2/s3/aws4_request"},
    {"x-amz-algorithm": "AWS4-HMAC-SHA256"},
    {"x-amz-date": "20150921T000000Z" }
  ]
}

http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-UsingHTTPPOST.html
<html>
  <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>
  
  <form action="http://posiba-givn-dev.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
    Key to upload: 
    <input type="input"  name="key" value="images/test.jpg" /><br />
    <input type="hidden" name="acl" value="public-read" />
    Content-Type: 
    <input type="input"  name="Content-Type" value="image/jpeg" /><br />
    <input type="text"   name="X-Amz-Credential" value="AKIAJ7IJVHPVKE7PH25A/20150921/us-west-2/s3/aws4_request" />
    <input type="text"   name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" />
    <input type="text"   name="X-Amz-Date" value="20150921T000000Z" />

    <input type="hidden" name="Policy" value='base64(policy)' />
    <input type="hidden" name="X-Amz-Signature" value="signature" />
    File: 
    <input type="file"   name="file" /> <br />
    <!-- The elements after this will be ignored -->
    <input type="submit" name="submit" value="Upload to Amazon S3" />
  </form>
  
</html>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.