All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jets3t.samples.S3PostFormBuilder Maven / Gradle / Ivy

There is a newer version: 0.9.4
Show newest version
package org.jets3t.samples;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class S3PostFormBuilder {

    public static void main(String[] args) throws Exception {
        String aws_access_key = "YOUR AWS ACCESS KEY GOES HERE";
        String aws_secret_key = "YOUR AWS SECRET KEY GOES HERE";

        String policy_document =
          "{\"expiration\": \"2009-01-01T00:00:00Z\"," +
            "\"conditions\": [" +
              "{\"bucket\": \"s3-bucket\"}," +
              "[\"starts-with\", \"$key\", \"uploads/\"]," +
              "{\"acl\": \"private\"}," +
              "{\"success_action_redirect\": \"http://localhost/\"}," +
              "[\"starts-with\", \"$Content-Type\", \"\"]," +
              "[\"content-length-range\", 0, 1048576]" +
            "]" +
          "}";

        // Calculate policy and signature values from the given policy document and AWS credentials.
        String policy = new String(
            Base64.encodeBase64(policy_document.getBytes("UTF-8")), "ASCII");

        Mac hmac = Mac.getInstance("HmacSHA1");
        hmac.init(new SecretKeySpec(
            aws_secret_key.getBytes("UTF-8"), "HmacSHA1"));
        String signature = new String(
            Base64.encodeBase64(hmac.doFinal(policy.getBytes("UTF-8"))), "ASCII");

        // Build an S3 POST HTML document
        String html_document =
            "\n" +
                "\n" +
                "  S3 POST Form\n" +
                "  \n" +
                "\n" +

                "\n" +
                "  
\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " File to upload to S3:\n" + " \n" + "
\n" + " \n" + "
\n" + "\n" + ""; System.out.print(html_document); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy