Using the below code I can sign an upload URL to Amazon S3. What I would like to add to this is the ability to limit the upload size.
AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider(configFileName, profileName));
GeneratePresignedUrlRequest generateUploadPresignedUrlRequest = new GeneratePresignedUrlRequest(BUCKETNAME, key);
generateUploadPresignedUrlRequest.setMethod(HttpMethod.PUT);
generateUploadPresignedUrlRequest.setExpiration(getFutureDate( 2*DAYS ));
URL uploadURL = s3client.generatePresignedUrl(generateUploadPresignedUrlRequest);
I am aware that what I need is to add a policy that sets the "content-length-range". Example: ["content-length-range", 0, 10485760]
But all my attempts to encode this in Java have failed with 403 Forbidden.
EDIT: I ommitted non-working code and adding this before the generatePresignedUrl which was my "fix".
Date futureDate = getFutureDate( 2*DAYS );
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String date = df.format(futureDate);
String policy = "{ \"expiration\": \""+date+"\","+
"\"conditions\": [" +
"{\"bucket\": \""+BUCKETNAME+"\" }," +
"['content-length-range', 0, 25000]," +
"]" +
"}";
generateUploadPresignedUrlRequest.addRequestParameter("Policy", policy);
LOL - 5 min after posting I found my error...
ANSWER - note the "Policy" instead of "policy" (capital P) generateUploadPresignedUrlRequest.addRequestParameter("Policy", policyStr);