john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

s3 pre signed upload url https put

// 2013-05-20 johnpfeiffer  http://aws.amazon.com/sdkforjava/ , external jars: aws-java-sdk, commons-logging, commons-codec, httpclient, httpcore, httpmime

import java.io.IOException; //probably should catch and handle these nicely
import java.net.URL;

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

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;



public class S3SelfSignedUploadURL {

    private static String bucketName = "test-bucket";
    private static String objectKey = "javasdkexample.txt";
    private static String accessKey = "AKIAJPRBE3FB6IPNVQ7Q";
    private static String secretKey = "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";

    public static void main(String[] args) throws Exception {

        BasicAWSCredentials creds = new BasicAWSCredentials( accessKey , secretKey );
        AmazonS3 s3client = new AmazonS3Client( creds );

        System.out.println( "Generating pre-signed URL." );
        java.util.Date expiration = new java.util.Date();
        long milliSeconds = expiration.getTime();
        milliSeconds += 1000 * 60 * 60; // Add 1 hour.
        expiration.setTime(milliSeconds);


        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest( bucketName, objectKey );
        generatePresignedUrlRequest.setMethod( HttpMethod.PUT );
        generatePresignedUrlRequest.setExpiration( expiration );

        URL url = s3client.generatePresignedUrl( generatePresignedUrlRequest );
        System.out.println( url );

        String data = "helloworld";
        try{
            uploadJava( url, data );

        }catch (AmazonServiceException exception) {
            System.out.println( "AmazonServiceException: " + exception.getMessage() );
            System.out.println( "HTTP  Code: "    + exception.getStatusCode() );
            System.out.println( "AWS Error Code:" + exception.getErrorCode() );
            System.out.println( "Error Type:    " + exception.getErrorType() );
            System.out.println( "Request ID:    " + exception.getRequestId() );
        } catch (AmazonClientException ace) {
            System.out.println( "AmazonClientException (internal client error): " );
            System.out.println( "Error Message: " + ace.getMessage() );
        }

    } //end main


    private static void uploadJava( URL url, String data ) throws Exception{

        HttpsURLConnection  conn = (HttpsURLConnection ) url.openConnection();
        conn.setDoOutput( true );
        conn.setRequestMethod("PUT");
        OutputStreamWriter out = new OutputStreamWriter( conn.getOutputStream() );
        out.write(  data  );
        out.close();
        int responseCode = conn.getResponseCode();
        System.out.println( Integer.toString(  responseCode ) );
    }


} //end class

  • « HttpClient 4 2 REST get post put delete example
  • s3 boto library presigned url http put and glacier »

Published

May 20, 2013

Category

java

~193 words

Tags

  • https 3
  • java 252
  • pre 1
  • put 3
  • s3 17
  • signed 2
  • upload 10
  • url 14