AWS S3 Generating Signed Urls ''AccessDenied''

后端 未结 6 1187
心在旅途
心在旅途 2021-02-06 06:51

I am using NodeJs to upload files to AWS S3. I want the client to be able to download the files securely. So I am trying to generate signed URLs, that expire after one usage. My

6条回答
  •  春和景丽
    2021-02-06 07:23

    Your code looks good but I think you are missing the signatureVersion: 'v4' parameter while creating the s3bucket object. Please try the below updated code.

    const s3bucket = new AWS.S3({
        signatureVersion: 'v4',
        accessKeyId: 'my-access-key-id',
        secretAccessKey: 'my-secret-access-key',
        Bucket: 'my-bucket-name',
    })
    const uploadParams = {
        Body: file.data,
        Bucket: 'my-bucket-name',
        ContentType: file.mimetype,
        Key: `files/${file.name}`,
    }
    s3bucket.upload(uploadParams, function (err, data) {
        // ...
    })
    const url = s3bucket.getSignedUrl('getObject', {
        Bucket: 'my-bucket-name',
        Key: 'file-key',
        Expires: 300,
    })
    

    For more about signatureVersion: 'v4' see the below links

    https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html

    https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-streaming.html

    You can also try out the below nodejs library that create presigned url

    https://www.npmjs.com/package/aws-signature-v4

提交回复
热议问题