Reputation: 721
I'm having problems with the pre signed URL of S3
When I try to download my browser shows a error message saying that the PDF File is corrupted. However, when I attempt to download by calling directly via the byte stream, it works fine.
My code:
public URL getSignedURL(FileS3 filesS3) {
if (!clienteS3.doesBucketExistV2(bucketName)) {
System.out.println("O bucket informado não existe [" + bucketName + "].");
throw new NullPointerException("O bucket informado não existe [" + bucketName + "].");
}
java.util.Date expiration = new java.util.Date();
long expTimeMillis = Instant.now().toEpochMilli();
expTimeMillis += MySettings.AWS_S3_EXPIRATION_TIME;
expiration.setTime(expTimeMillis);
//
// Generate the presigned URL.
System.out.println("Generating pre-signed URL.");
GeneratePresignedUrlRequest generatePresignedUrlRequest
= new GeneratePresignedUrlRequest(bucketName, filesS3.getUrl())
.withMethod(HttpMethod.GET)
.withExpiration(expiration);
URL url = clienteS3.generatePresignedUrl(generatePresignedUrlRequest);
//
filesS3.setPreSignedUrl(url);
//
return url;
}
Attempt of solution:
Upvotes: 2
Views: 431
Reputation: 3949
Try providing the Cotent-Type
of application/pdf
as a response header within the url using withResponseHeaders()
method, see if that resolves the mismatch.
Update to the code above:
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, filesS3.getUrl())
.withMethod(HttpMethod.GET)
.withExpiration(expiration)
.withResponseHeaders(new ResponseHeaderOverrides().withContentType("application/pdf"));
Upvotes: 1