Search code examples
javaamazon-web-servicesamazon-s3aws-sdk-java

How to set Policy conditions in Java AWS S3 SDK presigned url


I use Java AWS S3 SDK to presign my requests. I have the following code:

var request = new GeneratePresignedUrlRequest(bucketName, filename)
        .withMethod(method)
        .withExpiration(expiration());

// do something with request

return s3Client.generatePresignedUrl(request);

What I need to write in place of comment to add custom conditions like content-length-range?


Solution

  • For browser-based POST uploads to S3, the AWS Java SDK doesn't provide a way to generate pre-signed URLs with conditions. There's an open feature request to add this to the v2 SDK. Note that the PHP, Node.js, and Python SDKs do all provide this feature.

    For regular HTTP PUT pre-signed URLs, you can't apply content length restrictions to pre-signed URLs. You can place conditions using a custom policy but that only supports:

    • DateLessThan
    • DateGreaterThan
    • IpAddress

    If you need to deal with objects outside of a given size range then you could potentially do that in AWS Lambda, after the object has been uploaded.