Corrupted PDF in S3预签名URL与Spring Boot

huangapple go评论70阅读模式
英文:

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:
解决尝试:

  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"
  2. 指定内容类型。结果:这个 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:

  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"

答案1

得分: 1

尝试在URL响应标头中使用withResponseHeaders()方法提供Content-Typeapplication/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"));

huangapple
  • 本文由 发表于 2023年3月31日 03:12:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定