真的很难与这个挣扎,所以任何帮助都会很棒!
所以我试图使用预先签名的网址直接从浏览器上传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的浏览器代码。 然后我使用以下打字稿代码直接上传文件:
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>
有什么想法吗?
我在预设网址中指定了内容类型标题,然后在发布请求中将其设置为网址,因此我无法看到标题不匹配的方式(一个似乎导致错误的错误)我看到的问题。)
发帖请求有什么特别之处吗? 内容类型是否正确用于将pdf文件上载到S3?
任何见解都会很棒,谢谢!
答案 0 :(得分:0)
使用:
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;
}
拨打电话进行上传文件时,应使用相同的Content-Type。