无法使用s3 getSignedUrl(' getObject ..)下载图像并返回签名与

时间:2016-07-31 13:24:01

标签: node.js cordova amazon-s3

我对AWS比较新。我所要做的就是将图像从我的应用程序上传到aws S3并下载它以在应用程序的另一个页面中查看图像。上传成功,并能够在S3中看到上传的图像。但无法下载它,因为它会引发以下错误。

     FileTransferError {body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<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>AKXXXXXXXXXXXXXXXXXX</AWSAccessKeyId>
<StringToSign>GET\n\n\n1469897687\n/huntuploads/uploads/%25222a85a6675d15eeeca5c8b
da6eed4c52e%2522</StringToSign>
<SignatureProvided>cUBhtiA5AGJbj8vl%2FX6xi%2B9BBRY%3D</SignatureProvided>
<StringToSignBytes>47 45 54 0a 0a 0a 31 34 36 39 38 39 37 36 38 37 0a 2f 68 75 6e 
74 66 6f 6f 64 75 70 6c 6f 61 64 73 2f 75 70 6c 6f 61 64 73 2f 25 32 35 32 32 32 
61 38 35 61 36 36 37 35 64 31 35 65 65 65 63 61 35 63 38 62 64 61 36 65 65 64 34 
63 35 32 65 25 32 35 32 32</StringToSignBytes>
<RequestId>CCB513320456EB6B</RequestId>
<HostId>v4c7Ozf911tErWo5dCsL9RNLL78r3rUE6234Z801ZFXuELrji4juDehHmaxnK8t5qMBGcjz90a
I=</HostId></Error>";
        code = 3;
        "http_status" = 403;
        source = "https://huntuploads.s3-us-west-
2.amazonaws.com/uploads/%25222a85a6675d15eeeca5c8bda6eed4c52e%2522?
AWSAccessKeyId=AKXXXXXXXXXXXXXXXXXX&Expires=1469897687&Signature=cUBhtiA5AGJbj8vl%
252FX6xi%252B9BBRY%253D";
        target = "file:///var/mobile/Containers/Data/Application/1EB46B25-8BC5-
46C9-BE6B-BF1E120B7627/Documents/%222a85a6675d15eeeca5c8bda6eed4c52e%22";
}

以下代码用于上传在服务器端正常工作的图像

 var AWS = require('aws-sdk');
    AWS.config.update({accessKeyId: 'AXXXXXXXXX......', secretAccessKey: 'XXXXX....'});
    AWS.config.update({region: 'us-west-2'});

var s3 = new AWS.S3( { params: {Bucket: 'huntfooduploads'}});

app.post('/FileUpload', function(req, res, next) {
var fileStream = fs.createReadStream(req.files.file.path);
          var params = {
              'Key': 'uploads/' + req.files.file.name,
              'Body': fileStream,
              'ContentEncoding': 'base64',
              'Content-Type ': 'image/jpeg'
          };

   s3.upload(params, function(err, data) {
      if (err) throw err;
      console.log('after s3 upload====', err, data);
       var imgFileInfo = req.files;
       var imgUploadData = data;
       ....
   }
}

以下代码用于从服务器端使用getSignedUrl为s3下载映像

var urlParams = {Bucket: 'huntuploads', Key:'uploads/'+rows[0].MyHunt_FileName};

  // s3 getSigned Url
  s3.getSignedUrl('getObject', urlParams, function(err, url)  {
      if (err) throw callback(err);
       var fullUrl={awsUrl:url};
       res.send(fullUrl);
  })

一旦我将fullUrl发送回客户端即app,我尝试使用以下

$cordovaFileTransfer.download(encodeURI(itemData[3].awsUrl), targetPath, options, trustHosts)
         .then(function(result) {
           console.log('Success! Download is successful');
            $scope.imgURI = targetPath;
         }, function(err) {
            console.log('Error!!! Download is not successful');
           // Error
         }, function (progress) {
             $timeout(function () {
             $scope.downloadProgress = (progress.loaded / progress.total) * 100;
           });
         });

当我尝试下载图片时从应用程序端获取上述消息。我尝试添加nx-amz-server-side-encryption-customer-algorithm:AES256作为标题。我需要知道以下内容

  1. 这是将数据从服务器传递到客户端(app)的正确方法
  2. 如果getSigned网址的urlParams是正确的。不确定我是否遗漏了某些事情或任何事情要做。
  3. 有人可以告诉我正确的方向。

2 个答案:

答案 0 :(得分:2)

我已经解决了我的自我。但感谢Michael和其他人的帮助。解决方案是我在

之一中添加了以下行作为选项

var options = {encodeURI:false}; 然后 cordovaFileTransfer.download(encodeURI(itemData [3] .awsUrl),targetPath,options,trustHosts){...}

修复了这个问题。看起来默认情况下encodeUI设置为true,这改变了签名。

答案 1 :(得分:0)

据我所知,getSignedUrl函数不检查文件是否存在,

1 - 确保您的文件网址正确(未签名的网址)。

2 - 确保您的存储桶的权限和访问权限正确

最后,在调用getSignedUrl函数时检查机器的日期和时间。