Failed to load image using s3 getSignedUrl ('getObject ..) and returns caption does not match

I am relatively new to AWS. All I tried to do was upload the image from my application to aws S3 and upload it to view the image on another page in the application. The download was successful and was able to see the downloaded image in S3. But he was not able to download it, as it gives the following error.

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

Below is the code to upload an image that works great on the server side.

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

Here is the following code used to upload an image using getSignedUrl for s3 from the server side

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

As soon as I send fullUrl back to the client side, this application, I tried to use the following

$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: AES256 .

  • ()
  • URL- urlParams getSigned . , - - .

- .

+1
2

, . . :

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

. , encodeUI true, .

+1

, getSignedUrl ,

1. , URL- ( URL-).

2. , .

, , getSignedUrl.

0

Source: https://habr.com/ru/post/1655076/


All Articles