I am using generatePresignedUrl method of AmazonS3Client class to get a pre-signed URL. However when the method is called in the same session as that of the upload of the file. it returns url of the form :
But when I rerun my API and call the same method with the same params I get url of the form :
First Url works fine but the second url results in :
The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256.
I have tried to change configuration to use signature version 4 as per the following discussion :
The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256
But doesn't work. Ask me if any additional info is required.
Endpoint to generate url :
@SkipAuthentication
@GET
@Path("/xyz")
public String download(@QueryParam("cer") String objectKey) {
return testService.test(objectKey).toString();
}
Test method of service :
public URL test(String objectKey) {
return awsDownloadService.generateDownloadUrl(objectKey, awsServer.getDownloadsBucketName());
}
The generateDownloadUrl method is as follows :
public URL generateDownloadUrl(String keyName, String bucketName) {
LOG.info("generateDownloadUrl - {} {} {} {}>",accessKeyId, secretAccessKey, keyName, bucketName);
BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKeyId, secretAccessKey);
AmazonS3Client s3Client = new AmazonS3Client(awsCreds);
Date expiration = new Date();
long msec = expiration.getTime();
msec += 1000 * 60 * 15; // 15 minutes.
expiration.setTime(msec);
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(bucketName, keyName);
generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default.
generatePresignedUrlRequest.setExpiration(expiration);
URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
LOG.info("GeneratePreAuthenticatedUrl - url generated is <{}>",url);
return url;
}
When I upload the file using upload method and then call the end point to generate url it works fine but when I restart the api and call the end point to generate url, different url is returned as described earlier in the question.
Solution and new query:
I have solved the problem. Apparently ,AmazonS3Client
is deprecated. Making client this way works consistently :
AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds));
builder.setRegion(Regions.AP_SOUTH_1.getName());
AmazonS3 s3client = builder.build();
Still, the behavior AmazonS3Client
exhibited earlier is kind of odd. Can anybody provide explanation of the earlier behavior?