AWS v2 s3.getPresignedUrl vs AWS V3 getSignedUrl

347 views Asked by At

I seem to not be able to get my files to upload with the new Presigned url in AWS V3 SDK.

The following is my AWS-SDK v2 code

await s3Client.getSignedUrlPromise('putObject', {
       Bucket: process.env.S3Bucket,
       Key: req.body.filename,
       ContentType: req.body.filetype,
       Expires: DAY,
});

// url = https://trc-erc-stackprod-filess3bucket-1b5q2jbcvvzlj.s3.amazonaws.com/applications/7105/1697349391787.pdf?AWSAccessKeyId={key}&Content-Type=application%2Fpdf&Expires=1697439459&Signature={signature}

The following is my AWS-SDK v3 code

await getSignedUrl(
     s3,
     new PutObjectCommand({
         Bucket: process.env.S3Bucket,
         Key: req.body.filename,
         ContentType: req.body.filetype,
     }), {expiresIn: DAY}
)

// url = https://trc-erc-stackprod-filess3bucket-1b5q2jbcvvzlj.s3.us-east-1.amazonaws.com/applications/7105/1697349391787.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential={credentials}&X-Amz-Date=20231015T065739Z&X-Amz-Expires=900&X-Amz-Signature={signature}&X-Amz-SignedHeaders=expires%3Bhost&x-id=PutObject

The url gets returned to my frontend code and then uploaded using axios

await axios.put(presignedUrl, _file, {
     params: {
        name: key,
     },
     headers: {
       'Content-Type': _file.type,
     }
})

The AWS-SDK V2 successfully uploads my file

The AWS-SDK V3 fails and the error returns an error with a message that says the following: The request signature we calculated does not match the signature you provided. Check your key and signing method.

Expected Outcome:

I expect the AWS SDK v3 getSignedUrl method to generate a pre-signed URL that is consistent with the behavior of the AWS SDK v2 s3.getPresignedUrl.

Additional Information:

AWS SDK version: v2 and v3 AWS profile configuration AWS region

Solved: It seems adding the key params with value of name was the issue

await axios.put(presignedUrl, _file, {
     params: {
        name: key,
     },
     headers: {
       'Content-Type': _file.type,
     }
})

should actually be

await axios.put(presignedUrl, _file, {
     headers: {
       'Content-Type': _file.type,
     }
})
0

There are 0 answers