package com; import com.amazonaws.AmazonServiceException; import com.amazonaws.HttpMethod; import com.amazonaws.SdkClientException; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicSessionCredentials; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; import java.io.IOException; import java.net.URL; public class GeneratePresignedURL { private static BasicSessionCredentials sessionCredentials; public static void main(String[] args) throws IOException { String clientRegion = "us-east-1"; String bucketName = "bucketname"; String objectKey = "CloudServices.png"; String AccessKeyID = "ASIAIPORVHUBITG7"; String SecretAccessKey = "WYOrynMz20B5ORHpu9f/m+ufH2t4+fkTjnb"; String SessionToken = "XdzEIj//////////wEaDBx8AUTyX+PoMuFXUiLrARdFmJCz0ffbywapf9EPCzmy960mugqjVfy1uSoN4+iaSn/GjdE0x5kDGJ330foAe2XxvkbjVzpl4CN7ui6Z/oOiJC25H5/Y3vrV9qmDT/5MoT/CVnUz8vcxGtoDF/UyR5znoy5BlyqkagCaB/RmO6MdzTf9bqh4lPqU8ASn4yQ0YlVdQGvQvDZtsts0hits2pPdvwXKGOmX5zENFTscldFA1fCGK2fOmJp+UbaU+C7KzSNq4io48VdPEYBqSQZxl4W9ZIcdSkrVSzq7q17LCkpB6fVg6bSqmN/0Q3Ej0o0CfLhdIwiEMcfhH2wo4/b22QU="; try { sessionCredentials = new BasicSessionCredentials(AccessKeyID, SecretAccessKey, SessionToken); AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withRegion(clientRegion) .withCredentials(new AWSStaticCredentialsProvider(sessionCredentials)) .build(); // Set the presigned URL to expire after one hour. java.util.Date expiration = new java.util.Date(); long expTimeMillis = expiration.getTime(); expTimeMillis += 1000 * 60 * 60; expiration.setTime(expTimeMillis); // Generate the presigned URL. System.out.println("Generating pre-signed URL."); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey) .withMethod(HttpMethod.GET) .withExpiration(expiration); URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest); System.out.println("Pre-Signed URL: " + url.toString()); } catch(AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch(SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } } output:
https://bucketname.s3.amazonaws.com/CloudServices.png? X-Amz-Security-Token=XdzEIj//////////wEaDBx8AUTyX+PoMuFXUiLrARdFmJCz0ffbywapf9EPCzmy960mugqjVfy1uSoN4+iaSn/GjdE0x5kDGJ330foAe2XxvkbjVzpl4CN7ui6Z/oOiJC25H5/Y3vrV9qmDT/5MoT/CVnUz8vcxGtoDF/UyR5znoy5BlyqkagCaB/RmO6MdzTf9bqh4lPqU8ASn4yQ0YlVdQGvQvDZtsts0hits2pPdvwXKGOmX5zENFTscldFA1fCGK2fOmJp+UbaU+C7KzSNq4io48VdPEYBqSQZxl4W9ZIcdSkrVSzq7q17LCkpB6fVg6bSqmN/0Q3Ej0o0CfLhdIwiEMcfhH2wo4/b22QU= &X-Amz-Algorithm=AWS4-HMAC-SHA256 &X-Amz-Date=20180705T064708Z &X-Amz-SignedHeaders=host &X-Amz-Expires=3599 &X-Amz-Credential=ASIAIPORVHUBITG7%2F20180705%2Fus-east-1%2Fs3%2Faws4_request &X-Amz-Signature=39a033db5db1acd7ce579c7cf66adf3b14117676834345a0acdfa0fd1c42c364