4

I tried if I use access key, it works fine but I am trying to get ride of access key and using role instead, but once I get ride of access key. what I get in return is www.aws.amazon.com

    const AWS = require('aws-sdk');
    const s3 = new AWS.S3();
    const params = {Bucket: config.bucket, Expires: config.time, Key};
    const url = s3.getSignedUrl('getObject', params);
    console.log('The URL is', url);

I even made sure my role is set right by going into my ec2 and run the cli command aws s3 presign s3://bucket/path/file which works fine I get the signed url in return though so this means my role is correct isn't it?

Thanks in advance for any advice / help.

Tsuna
  • 2,098
  • 6
  • 24
  • 46

1 Answers1

7

You can't use getSignedUrl() synchronously when using IAM roles.

Note: You must ensure that you have static or previously resolved credentials if you call this method synchronously (with no callback), otherwise it may not properly sign the request. If you cannot guarantee this (you are using an asynchronous credential provider, i.e., EC2 IAM roles), you should always call this method with an asynchronous callback.

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getSignedUrl-property

s3.getSignedUrl('getObject', params, function (err, url) {
  console.log('The URL is', url);
});
Michael - sqlbot
  • 169,571
  • 25
  • 353
  • 427
  • Thanks thanks, so I have to use a callback and also realized I must use `promise` too else I can't get the returned url properly – Tsuna Dec 04 '18 at 18:40
  • i wasted couple hours on this.. AWS sdk should have thrown an error instead of silently failing and returning a placeholder :( – Deepak Jun 09 '20 at 05:48
  • There is also a promise function `getSignedUrlPromise`. Anyway thank you sir Michael, this helped me a lot! – Vadim May 04 '22 at 15:26
  • Hey, I used this function but still get placeholder var params = {Bucket: 'bucket', Key: 'key', Expires: 60}; var promise = s3.getSignedUrlPromise('getObject', params); promise.then(function(url) { console.log('The URL is', url); }, function(err) { ... }); – Mee Jan 27 '23 at 03:18