Reactjs 使用预先签名的Url上传到AWS S3时获得403(禁止)
我在尝试将文件上载到S3时遇到了意外的403。奇怪的是,当我使用JavaAWSSDK生成预签名url时,我已经完成了这项工作。我现在正在使用PythonAWSSDK生成预先签名的url,我感觉我正在做完全相同的事情 以下是我的代码,可以正常工作:Reactjs 使用预先签名的Url上传到AWS S3时获得403(禁止),reactjs,amazon-s3,axios,http-status-code-403,pre-signed-url,Reactjs,Amazon S3,Axios,Http Status Code 403,Pre Signed Url,我在尝试将文件上载到S3时遇到了意外的403。奇怪的是,当我使用JavaAWSSDK生成预签名url时,我已经完成了这项工作。我现在正在使用PythonAWSSDK生成预先签名的url,我感觉我正在做完全相同的事情 以下是我的代码,可以正常工作: public UploadSignedRequest getUploadSignedRequest() { AmazonS3 s3Client = getS3Client(); // Set the pre-signed URL t
public UploadSignedRequest getUploadSignedRequest() {
AmazonS3 s3Client = getS3Client();
// Set the pre-signed URL to expire after one hour.
Date expiration = DateUtil.getSignedUrlExpirationDate();
// Generate the pre-signed URL.
String objectKey = UUID.randomUUID().toString();
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(BUCKET_NAME, objectKey)
.withMethod(HttpMethod.PUT)
.withExpiration(expiration);
String s3FilePath = String.format("%s/%s/%s", S3_URL, BUCKET_NAME, objectKey);
URL signedRequest = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
return new UploadSignedRequest(signedRequest, s3FilePath, objectKey);
}
以下是成功的客户端代码:
var config = {
onUploadProgress: function (progressEvent) {
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
updateProgressFunc(percentCompleted);
}
};
axios
.put(signedRequest.signedRequestUrl, videoFile, config)
.then(res => {
console.log(res);
console.log(res.status);
// save video metadata in db
dispatch(saveVideoMetadata(video));
})
.catch(function (error) {
console.log(error);
});
现在,我正在尝试使用Python AWS SDK完成基本相同的事情(图像文件而不是视频文件)
def getS3UploadRequest(uuid):
return S3.generate_presigned_url(
ClientMethod='put_object',
Params={
'Bucket': BUCKET,
'Key': uuid
}
)
获取403的客户端代码:
axios
.put(signedRequest, attachedFile)
.then(res => {
console.log("successfully uploaded file to s3");
console.log(res);
// dispatch(createProjectTaskComment(newComment, projectTaskId, userId, isFreelancer));
});
当我尝试在《邮递员》中使用预签名时,我得到了以下响应:
?xml version="1.0" encoding="UTF-8"?>
<Error><Code>SignatureDoesNotMatch</Code><Message>The request
signature we calculated does not match the signature you provided.
Check your key and signing method.</Message>
<AWSAccessKeyId>gibberish</AWSAccessKeyId><StringToSign>PUT
谢谢你的帮助 可能重复的