Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danielsan/935fc05afb155d4c32791017408adb3b to your computer and use it in GitHub Desktop.
Save danielsan/935fc05afb155d4c32791017408adb3b to your computer and use it in GitHub Desktop.
Lists all the content of a given Bucket and Prefix and matches with a RegExp to filter the desired results
import S3 from 'aws-sdk/clients/s3.js'
export const s3 = new S3()
const myFilterContents = (s3Response, pattern) => s3Response.Contents.filter(s3Object => pattern.test(s3Object.Key))
export async function listObjectsWithSuffix (Bucket, Prefix, pattern) {
let myResults = []
let myS3Response
const myS3ListParams = {
Bucket,
Prefix,
MaxKeys: 1000,
ContinuationToken: undefined
}
do {
myS3Response = await s3.listObjectsV2(myS3ListParams).promise()
myResults = myResults.concat(myFilterContents(myS3Response, pattern))
myS3ListParams.ContinuationToken = myS3Response.NextContinuationToken
} while (myS3Response.IsTruncated === true)
return myResults
}
// npm i @aws-sdk/client-s3
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/preview/client/s3/command/ListObjectsV2Command/
import { S3Client, ListObjectsV2Command } from "@aws-sdk/client-s3"
const myS3Client = new S3Client();
const myFilterContents = ({ Contents: c }, pattern) => c.filter(s3Object => pattern.test(s3Object.Key))
export async function listObjectsWithSuffix (Bucket, Prefix, myPattern) {
let myResults = []
let myS3Response
const myS3ListParams = {
Bucket,
Prefix,
MaxKeys: 1000,
ContinuationToken: undefined
}
do {
myS3Response = await myS3Client.send(new ListObjectsV2Command(myS3ListParams))
myResults = myResults.concat(myFilterContents(myS3Response, myPattern))
myS3ListParams.ContinuationToken = myS3Response.NextContinuationToken
} while (myS3Response.IsTruncated === true)
return myResults
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment