Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2010-2015, Inc. or its affiliates. All Rights Reserved.
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
  * or in the "license" file accompanying this file. This file is distributed
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
 import static com.amazonaws.util.StringUtils.UTF8;
This class represents S3 upload policy. Policy string representation and signature to be used within EC2 bundling API.
 public class S3UploadPolicy {
     private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
     private String policySignature;
     private String policyString;

Creates a new S3 upload policy object from the specified parameters. Once constructed, callers can access the policy string and policy signature to use with the EC2 bundling API.

awsAccessKeyId The AWS access key ID for the S3 bucket the bundling artifacts should be stored in.
awsSecretKey The AWS secret key for the specified access key.
bucketName The name of the bucket to store the bundling artifacts in.
prefix The prefix for the bundling artifacts.
expireInMinutes The number of minutes before the upload policy expires and is unable to be used.
     public S3UploadPolicy(String awsAccessKeyId,
             String awsSecretKey,
             String bucketName,
             String prefix,
             int expireInMinutes) {
         Calendar expirationDate = Calendar.getInstance();
         SimpleDateFormat ISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
         ISO8601.setTimeZone(new SimpleTimeZone(0, "GMT"));
         StringBuilder builder = new StringBuilder();
                 .append("\"expiration\": \"")
                 .append("\"conditions\": [")
                 .append("{\"bucket\": \"")
                 .append("{\"acl\": \"")
                 .append("[\"starts-with\", \"$key\", \"")
         try {
             this. = base64Encode(builder.toString().getBytes());
             this. = signPolicy(awsSecretKey);
         } catch (Exception ex) {
             throw new RuntimeException ("Unable to generate S3 upload policy"ex);

Base64 representation of the serialized policy. Use policy generated by this method for passing to EC2 bundling calls.

Base64 policy
     public String getPolicyString() {
         return this.;

Policy signature in base64 format Use signature generated by this method for passing to EC2 bunding calls along with policy.

Base64 signature
    public String getPolicySignature() {
        return this.;
    private String signPolicy(String awsSecretKeyString base64EncodedPolicythrows
            UnsupportedEncodingException {
        SecretKeySpec signingKey = new SecretKeySpec(awsSecretKey.getBytes(), );
        Mac mac = Mac.getInstance();
        return base64Encode(mac.doFinal(base64EncodedPolicy.getBytes()));
    private String base64Encode(byte [] data) {
        return Base64.encodeAsString(data).replaceAll("\\s""");
New to GrepCode? Check out our FAQ X