我对S3的预签名URL有问题
当我尝试下载我的浏览器显示一个错误消息,说PDF文件已损坏。但是,当我尝试通过字节流直接调用下载时,它工作得很好。
我的代码:
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;
}
尝试解:
- 指定内容类型。结果:S3错误"我们计算的请求签名与您提供的签名不匹配。检查您的密钥和签名方法">
尝试使用withResponseHeaders()
方法在url中提供application/pdf
的Cotent-Type
作为响应头,看看是否解决了不匹配。
更新上面的代码:
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, filesS3.getUrl())
.withMethod(HttpMethod.GET)
.withExpiration(expiration)
.withResponseHeaders(new ResponseHeaderOverrides().withContentType("application/pdf"));