使用预签名网址上传到AWS S3时获取403(禁止)

时间:2019-05-04 17:36:17

标签: reactjs amazon-s3 axios http-status-code-403 pre-signed-url

尝试将文件上传到S3时出现意外403。奇怪的是,在使用Java AWS开发工具包生成预签名URL之前,我已经完成了此操作。我现在正在使用Python AWS开发工具包生成预签名的url,感觉就像我在做完全一样的事情。

这是我的代码,没问题:

ports:
  - "{{ hostvars['localhost'].ports }}"
  - 80

这是成功的客户代码:

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);
}

现在,我在这里尝试使用Python AWS开发工具包完成基本上相同的事情(图像文件而不是视频文件)。

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);
    });

获取403的客户端代码:

def getS3UploadRequest(uuid):
return S3.generate_presigned_url(
    ClientMethod='put_object',
    Params={
        'Bucket': BUCKET,
        'Key': uuid
    }
)

当我尝试在邮递员中使用presignedUrl时,我得到以下响应:

        axios
            .put(signedRequest, attachedFile)
            .then(res => {
                console.log("successfully uploaded file to s3");
                console.log(res);
                // dispatch(createProjectTaskComment(newComment, projectTaskId, userId, isFreelancer));
            });

感谢您的帮助!

0 个答案:

没有答案