For users, who have private buckets and requires the URL.
In my case, I had to get downloadable link of S3 Object for a specific time as my bucket is private.
I'm using Spring Cloud AWS, which under the hood uses AWS SDK For Java and which provides AmazonS3 interface for interacting with S3, use AmazonS3Client if you're using AWS SDK For JAVA instead of AmazonS3. I simply injected AmazonS3 and now I can get URL of an object where-ever required, but it can be downloaded within 5 mins else it will be expired.
public String getURL(String key) throws FileNotFoundException
{
try {
return amazonS3.generatePresignedUrl(BUCKET_NAME, key, new DateTime().plusMinutes(5).toDate()).toString();
}
catch (AmazonS3Exception exception){
if(exception.getStatusCode() == 404){
throw new FileNotFoundException(key);
}
else{
throw exception;
}
}
}