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

jets3t.jets3t.0.7.2.source-code.S3PostFormBuilder Maven / Gradle / Ivy

Go to download

Toolkit for Amazon S3, Amazon CloudFront, and Google Storage Service.

There is a newer version: 0.9.4
Show newest version
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;

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 BASE64Encoder()).encode(
            policy_document.getBytes("UTF-8")).replaceAll("\n","").replaceAll("\r","");

        Mac hmac = Mac.getInstance("HmacSHA1");
        hmac.init(new SecretKeySpec(
            aws_secret_key.getBytes("UTF-8"), "HmacSHA1"));
        String signature = (new BASE64Encoder()).encode(
            hmac.doFinal(policy.getBytes("UTF-8")))
            .replaceAll("\n", "");

        // 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 - 2024 Weber Informatics LLC | Privacy Policy