英文:
Corrupted PDF in S3 pre signed URL with Spring Boot
问题
I'm having problems with the pre signed URL of S3
当我尝试下载时,我的浏览器显示错误消息,指出 PDF 文件已损坏。
然而,当我尝试通过字节流直接调用下载时,它可以正常工作。
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:
解决尝试:
- 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"
 - 指定内容类型。结果:这个 S3 错误 "我们计算的请求签名与您提供的签名不匹配。请检查您的密钥和签名方法"。
 
英文:
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:
- 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"
 
答案1
得分: 1
尝试在URL响应标头中使用withResponseHeaders()方法提供Content-Type为application/pdf,看看是否解决了不匹配的问题。
更新上述的代码:
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, filesS3.getUrl())
        .withMethod(HttpMethod.GET)
        .withExpiration(expiration)
        .withResponseHeaders(new ResponseHeaderOverrides().withContentType("application/pdf"));
英文:
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"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论