aws list all files in s3 bucket node js aws 1

aws list all files in s3 bucket node js aws

// Array of all keys in s3 bucket
const findKeys = async(bucketName)=>{
    const {Contents} =  await s3.listObjects({Bucket: bucketName}).promise()
    const keysArr = []
    for(const obj of Contents){
        keysArr.push(obj.Key)
    }
    return keysArr
}

Here is what the above code is Doing:
1. We’re creating a function called findKeys that takes in a bucketName as a parameter.
2. We’re using the listObjects method to list all the objects in the bucket.
3. We’re creating an empty array called keysArr.
4. We’re looping through the objects in the bucket and pushing the key of each object into the keysArr array.
5. We’re returning the keysArr array.

Similar Posts