import { notNull } from '../../../../common' ; import { Bucket , s3 } from '../../../utils/s3' ; type lsResponse = { keys : string [ ] ; continuationToken : string | undefined ; } ; const listAndFilterPage = async ( Prefix , bucket ) : Promise < lsResponse > => { const objects = await s3 . listObjectsV2 ( { Bucket : bucket . name , Prefix , } ) . promise ( ) ; console . log ( 'returned' , JSON . stringify ( objects ) ) ; return { keys : ( objects . Contents || [ ] ) . map ( obj => obj . Key ) . filter ( notNull ) , continuationToken : objects . ContinuationToken , } ; } ; async function * listAndFilterPrefixes ( prefixes , bucket ) { for ( const prefix of prefixes ) { let nextPrefix = prefix ; while ( nextPrefix !== undefined ) { const { keys , continuationToken , } = await listAndFilterPage ( nextPrefix , bucket ) ; yield keys ; nextPrefix = continuationToken ; } } return ; } export const getMatchingObjects = async ( prefixes , bucket ) => { const keys = [ ] ; for await ( const partialKeys of listAndFilterPrefixes ( prefixes , bucket ) ) { keys . push ( ... partialKeys ) ; } return keys ; } ;