Fetching signed URL from AWS S3 on my Node Server

user3105927 picture user3105927 · Jun 7, 2014 · Viewed 13.6k times · Source

Solved:

I want to get a signed URL from my amazon S3 server. I am new to AWS. where do i set my secret-key and access_id_key so that S3 identifies request from my server.

var express=require('express');
var app=express();


var AWS = require('aws-sdk')
, s3 = new AWS.S3()
, params = {Bucket: 'my-bucket', Key: 'path/to/key', Expiration: 20}

s3.getSignedUrl('getObject', params, function (err, url) {
console.log('Signed URL: ' + url)
})

app.listen(8000)

Answer

Nathan picture Nathan · Sep 16, 2016

You can also set the credentials for each bucket if you are working with multiple buckets, you just need to pass the credentials into the constructor of the S3 object, like so:

var AWS = require('aws-sdk');
var credentials = {
  accessKeyId: AWS_CONSTANTS.S3_KEY,
  secretAccessKey: AWS_CONSTANTS.S3_SECRET,
  region: AWS_CONSTANTS.S3_REGION
};
var s3 = new AWS.S3(credentials);
var params = {Bucket:'bucket-name', Key: 'key-name', Expires: 20};
s3.getSignedUrl('getObject', params, function (err, url) {
  console.log('Signed URL: ' + url);
});