Home > Software engineering >  listObjectsV2 to list all the objects from nested "folders" using nodejs
listObjectsV2 to list all the objects from nested "folders" using nodejs

Time:10-08

I want to do a simple task: List all objects inside a specific "folder" in s3. I have this structure of folders in s3"

 s3Bucket/folder1/folder2/some-txt-file.txt.

In my case I need to list everything in folder2. I want node to give me this output

['some-txt-file.txt']

I'm writing a function for this. I'm using a for loop because I have multiple files in folder 2, so the output should really be a list of text files names. The code looks like this

const AWS = require('aws-sdk');
const s3Bucket = process.env.S3_BUCKET;
AWS.config.loadFromPath('./config_test.json');
const s3 = new AWS.S3();



async function list_everything_in_folder2(f1,f2){
    var params = {
        Bucket:s3Bucket,
        Delimiter:'/',
        Prefix:f1 '/' f2 '/'
    }
    const objs = await s3.listObjectsV2(params).promise();
    var out=[];
    for(obj of objs.CommonPrefixes){
        out.push(obj.Prefix);
    }

    return out
}

The result is an empty list.

Interestingly though, when I disregard the second argument of the function and put my Prefix in params like this

var params = {
  Bucket: 'top-bucket',
  Delimiter: '/',
  Prefix: f1  '/'
}

the output becomes

['top-bucket/folder1/']

I can't seem to go through more nested folders, it stops working after the "first appearance of the delimiter", so to speak. Any ideas? I searched for the answers here and elsewhere, but according to those I'm doing nothing wrong? I'm quite new to nodejs btw, so explain it to me like I'm 5 years old. Thanks

CodePudding user response:

CommonPrefixes contains the list of repeated prefixes in objects between the specified delimiter. In other words, it is the list of "sub directories" for the current listing.

The list of objects is in Contents, so changing your for loop to this:

    for(obj of objs.Contents){
        out.push(obj.Key);
    }

This will output the objects under that prefix not counting any objects a level deeper. If you want a complete recursive listing starting at the prefix you specify, drop the Delimiter:'/', from your params.

  • Related