Node.js – Ppload file to S3 via cURL using presigned URL

amazon s3amazon-web-servicescurlnode.js

I'm getting a pre-signed URL to upload a file on S3 bucket.

this is the curl command:

curl -v -T ./dansero.jpg 'https://ss-files-dev.s3.ap-southeast-2.amazonaws.com/dansero.jpg\?AWSAccessKeyId\=AKIAIT6VM43PPNS43Y7Q\&Expires\=1531973770\&Signature\=hNvG5rnICkk58mMBLeMgHGDZ93c%3D'

That gives me the error:

<Error>
  <Code>AccessDenied</Code>
  <Message>Access Denied</Message>
</Error>

Here my node.js generation of the presigned URL

var AWS = require('aws-sdk');

const s3 = new AWS.S3({

  accessKeyId: 'aaa',
  secretAccessKey: 'bbb+oeyjn8zGANuDyCIY',
  region: 'ap-southeast-2'
});

const params = {
  Bucket: 'ss-files-dev',
  Key: 'dansero.jpg'  
};

});

s3.getSignedUrl('putObject', params, function(err, urlsign) {
  if (err) console.log(err);
  console.log(urlsign);
});

So where is my problem in the URL generation or the curl?
thanks

Best Answer

I can't see the sign request part. Make sure get signed URL for put object. It is working code from my script:

 s3.getSignedUrl('putObject', params, function(err, urlsign) {
            if (err) console.log(err);
            var output = {
                url: urlsign
            };
            cb(null, output);
        });

Try put the object into your bucket by simple put request like this:

                        var req = http.request({
                            hostname: 's3.amazonaws.com',
                            port: 80,
                            path:{YOURPRESIGNEDURL}.replace('https://s3.amazonaws.com', ''),
                            method: 'PUT',
                        }, function(res) {

Finally, make sure about the following things:

1-User policy and right IAM for your key. You should have the permission to put objects like this:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt14546345345",
            "Effect": "Allow",
            "Action": [
                "s3:Put*"
            ],
            "Resource": [
                "arn:aws:s3:::myBucket/*"
            ]
        }
    ]
}

2- I think you should pass -X PUT in your curl request.