I'm creating a lambda application in node js to find all the files in s3 folder.
I'm using s3Client.listObjectsV2 to fetch all the keys in s3, I'm getting the below response, I want to filter the resultset to just files. whats the best practises for the same.
{
Key: 'AttributeModel/',
LastModified: 2021-12-16T23:47:54.000Z,
ETag: '"d41d8cd98f0"',
Size: 0,
StorageClass: 'STANDARD'
},
{
Key: 'AttributeModel/173b8566-5033-4606-a087-ba93ef2c1467.json',
LastModified: 2022-01-06T17:34:29.000Z,
ETag: '"d41d8cd98f0"',
Size: 68,
StorageClass: 'STANDARD'
},
{
Key: 'AttributeModel/009d155e-e45d-48a7-9a30-19cabb61479b.json',
LastModified: 2021-12-17T04:02:36.000Z,
ETag: '"d41d8cd98f0"',
Size: 48,
StorageClass: 'STANDARD'
}
Are all the files .json? or do you know what are file extensions you will get?
If yes, then you can filter based on whether the key has an extension or not.
const filteredData = dataReceived.filter(eachItem => eachItem.key.endsWith('.json')
console.log(filteredData)
You can add OR condition in endsWith method if you have more extensions
Another Method:
const filteredData = dataReceived.filter(eachItem => !eachItem.key.endsWith('/')
console.log(filteredData)