Upload file to S3

I generate Pre-Signed URL on the server side and loading fileon the client side.

For some reason, I get an error 403 forbidden SignatureDoesNotMatchfrom AWS, but I do not see where the error is.

Using postmanto request URL, what I get as response:

https://s3-us-west-2.amazonaws.com/my_bucket/uploads/%20VID_20160916_061949.mp4?AWSAccessKeyId=AKIAJV3UP7LBEKPYMLGA&Expires=1474005709&Signature=7KYWah4K%2FEkpdYQR%2FqYDrvCDPgQ%3D

Here below I insert my code:

Angularjs

// My videoData comes as a variable
$http.post(CONFIG.API_URL + '/endpoint1', { filename: videoData[0].name })
  .success(function (resp) {
  // Perform The Push To S3
    $http.put(resp, videoData[0], { headers: {'Content-Type': videoData[0].type} })
      .success(function (resp) {
        //Finally, We're done
        callback(null, 'Upload Done!');
      })
      .error(function (resp) {
        callback("An Error Occurred Attaching Your File");
      });
  })
  .error(function (resp) {
    callback("An Error Occurred Attaching Your File");
});

NodeJS

getSignedUrl: function (filename, callback) {
  if (filename) {
    AWS.config.update({
      accessKeyId: 'my_access_key',
      secretAccessKey: 'my_secret_key',
      region: 'us-west-2' // I've checked the bucket is correct
    });

    var s3 = new AWS.S3();

    s3.getSignedUrl('putObject', {Bucket: 'my_bucket', Key: 'uploads/' + filename}, function (err, url) {
      if (err) {
        callback(err);
      } else {
        callback(url);
      }
    });
  } else {
    callback('Error');
  }
}

Bucket Policy

{
    "Version": "2012-10-17",
    "Id": "Policy1474002303781",
    "Statement": [
        {
            "Sid": "Stmt1473001308637",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::my_user:root"
            },
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::my_bucket/uploads"
        }
    ]
}

CORS

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <MaxAgeSeconds>600</MaxAgeSeconds>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

Can someone help me?

Thanks, advice!

0
source share

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


All Articles