gpt4 book ai didi

angular - 发布到AWS S3预签名URL时的SignatureDoesNotMatch错误

转载 作者:行者123 更新时间:2023-12-02 12:54:51 24 4
gpt4 key购买 nike

真的很难与这个,所以任何帮助将是巨大的!

因此,我尝试使用预签名的URL直接从浏览器将pdf文件上传到Amazon S3。

我使用AWS Java SDK(用Kotlin编写的代码)在服务器上生成url,如下所示:

    val objectKey = StringBuilder().append("xxx").append("/").append("file1.pdf").toString()

val expiration = java.util.Date()
var msec = expiration.time
msec += (1000 * 60).toLong() // Add 1 minute. This means that the url expires in 1 minute.
expiration.time = msec

String bucketName = "test"

val responseHeaders = ResponseHeaderOverrides().withContentType("multipart/form-data")

val generatePresignedUrlRequest = GeneratePresignedUrlRequest(bucketName, objectKey)
generatePresignedUrlRequest.method = HttpMethod.POST
generatePresignedUrlRequest.responseHeaders = responseHeaders
generatePresignedUrlRequest.expiration = expiration

return s3Client.generatePresignedUrl(generatePresignedUrlRequest)

然后将该URL返回到使用Angular的浏览器代码。
然后,我使用以下 typescript 代码直接上传文件:
returnPresignUrl() {
return this.http.get(this.attachmentUploadUrl, this.httpOptions);

}
   uploadFiletoS3(file, filetype) {
let presignURL;
const data = file;
const fileHeaders = {
headers: new HttpHeaders({
'Content-Type': 'multipart/form-data',
})
};
this.returnPresignUrl().subscribe(url => {
presignURL = url;
this.http.post(presignURL, data,
fileHeaders).subscribe((res) => {
console.log('File uploaded', res);
}, (error) => { console.log('Error uploading File', error);
});
}, (error) => { console.log('Error getting a preasign URL: ',
error); });
}

但是我得到以下错误响应:
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you
provided. Check your key and signing method.
</Message>

有任何想法吗?

我在presign url中指定了内容类型 header ,然后在对URL的post请求中也将其设置,所以我看不到 header 不匹配(该错误似乎导致了我所看到的问题)。

邮寄要求有什么特别之处吗?
内容类型是否正确,可以将pdf文件上传到S3?

任何见解都会很棒,谢谢!

最佳答案

使用:generatePresignedUrlRequest.setContentType("application/octet-stream");
代替:
multipart/form-data

这是我生成预签名URL的方法:

public GeneratePresignedUrlRequest getGeneratePresignedUrlRequest(String tempBucketName,String objectKey){
Date expirationTime = getExpirationTime();
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(tempBucketName, objectKey);
generatePresignedUrlRequest.setMethod(HttpMethod.PUT);
generatePresignedUrlRequest.setExpiration(expirationTime);
generatePresignedUrlRequest.setContentType("application/octet-stream");
return generatePresignedUrlRequest;
}

进行 call 放置调用以上传文件时,应使用相同的Content-Type。

关于angular - 发布到AWS S3预签名URL时的SignatureDoesNotMatch错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49012888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com