从节点为

时间:2018-05-15 18:11:26

标签: node.js amazon-web-services express amazon-s3

我有一个MEAN堆栈应用程序,我正在尝试允许用户下载使用Amazon S3存储的文件。我正在为用户生成预签名网址,但是当我在Chrome中转到该网址时,我收到以下错误消息

The request signature we calculated does not match the signature you provided. Check your key and signing method.

当我从Postman查询节点应用程序时,它会生成一个带有问题的假定的预先签名的URL,但显然它不起作用。这是我的相关代码:

var express=require('express');
var S3 = require('aws-sdk/clients/s3');
var router = express.Router();

router.get('/api/downloadURL', (req,res,next) => {
  const s3 = new S3({
    apiVersion: '2006-03-01',
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    signatureVersion: 'v4',
    region: 'us-east-2' // region of my bucket and what it says is expected
  });
  var s3params = {'Bucket': process.env.S3_BUCKET_NAME, 'Key': 'folder1/file.pdf'}
  s3.getSignedUrl('getObject', s3params, function(err, url) {
    if (err) res.json(err);
    res.json(url);
  });
});

当邮递员查询时,我会得到以下格式的网址

https://bucketname.s3.us-east-2.amazonaws.com/folder1/file.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ACCESS_KEY_HERE%2F20180515%2Fus-east-2%2Fs3%2Faws4_request&X-Amz-Date=20180515T174606Z&X-Amz-Expires=900&X-Amz-Signature=89f6b9a6be699fc1e3268bceaf6585d34ed11e077126ba7c634b769f1add4475&X-Amz-SignedHeaders=host

0 个答案:

没有答案