Wednesday, March 21, 2012

Generate S3 Pre-signed URL in Java

I googled, not happy, so post it myself.

import java.util.Calendar;
import java.util.Date;

import com.amazonaws.HttpMethod;
import com.amazonaws.auth.BasicAWSCredentials;

public class PreSignURLGenerator {

    private final static String BUCKET_NAME = "this-is-not-porn";
    private final static String FILE_NAME = "not-porn-media-files/0146ce52-bbd5-4998-9378-35c20ece0000.jpg";
    private final static String ACCESS_KEY = "not-telling-you";
    private final static String SECRET_KEY = "it-s-a-secret";

     * @param args
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 1000);
        Date expDate = cal.getTime();
        try {
            BasicAWSCredentials cre = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
            AmazonS3 s3 = new AmazonS3Client(cre);
     String url = s3.generatePresignedUrl(BUCKET_NAME, FILE_NAME, expDate, HttpMethod.GET).toString();
        } catch (Exception e) {


and required access to Internet/AWS.

Do feedback if you really like this post.

No comments: