KenobiBastila
KenobiBastila

Reputation: 721

Corrupted PDF in S3 pre signed URL with Spring Boot

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:

  1. Content Type specify. Result: this S3 error "The request signature we calculated does not match the signature you provided. Check your key and signing method"

Upvotes: 2

Views: 431

Answers (1)

djmonki
djmonki

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

Related Questions