Pooja Pandey Pooja Pandey - 1 year ago 69
Node.js Question

How to generate Url for each key of a bucket in nodejs

I am trying to generate url for each key of a bucket in loop i.e. iterate over bucket generate url and put in an array. Please suggest me best way to do it.

Here is the code i am trying..

downloadByBusinessFlowId: function (req, res) {

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

AWS.config.update({accessKeyId: sails.config.AwsCredential.AWS_ACCESS_KEY_ID, secretAccessKey: sails.config.AwsCredential.AWS_SECRET_ACCESS_KEY});
var s3 = new AWS.S3();

var params = {
Bucket: sails.config.AwsCredential.bucket_name,
Prefix: 'cc_' + req.param('cc')

const signedUrlExpireSeconds = 60 * 5;

// console.log(url)

var result = [];

s3.listObjects(params, function (err, data) {
if(err)throw err;
dataLength = data.Contents.length;
var flag = false;

for(var i = 0; i < dataLength; i++){
//need logic here and put url in json
return res.json(result);


Answer Source

I do this like this (I have promisified the S3 from aws-sdk and this code is from my project, so you may have to change the code a bit):

return Bluebird.try(function () {    
        const params = {
            Bucket: <BUKCET>, /* required */
            // ContinuationToken: '',
            Prefix: <PREFIX>,  //optional
            StartAfter: <STARTAFTER>  //optional

        // Prefix is for filtering your objects if say you store objects like userID/message1 so prefix can be userID

        // List all objects, for Keys for which we will create URLs
        return AWS.S3.listObjectsV2Async(params);
    }).then(function (data) {
        console.log("ALL OBJECTS FOR REQUEST: ", data.Contents);

        const createSignedUrlPromises = [];
        const response = [];

        const message_ids = [];

        // For each object, create a promise for making signed url
        _.forEach(data.Contents, function (content) {
            const params = {
                Bucket: <BUCKET>,
                Key: content.Key,
                Expires: <EXPIRY>
            createSignedUrlPromises.push(AWS.S3.getSignedUrlAsync('getObject', params));

        return Bluebird.all(createSignedUrlPromises).then(function (data) {
            console.log("URLS FOR VOICE MESSAGES: ", data);

            // data is an array of signed urls for all objects in your bucket