@@ -140,7 +140,6 @@ export async function listFilesInBucket(prefix: string): Promise<S3FileInfo[]> {
140140 }
141141 } ) ;
142142 const response = await s3Client . send ( command ) ;
143- const files = response . Contents ?. map ( ( item ) => item . Key || '' ) . filter ( Boolean ) as string [ ] || [ ] ;
144143 const fileInfoList : S3FileInfo [ ] = ( response . Contents || [ ] )
145144 . filter ( s3Object => s3Object . Key )
146145 . map ( s3Object => {
@@ -197,54 +196,48 @@ export async function findLeafPrefixes(
197196 // Start with a list containing only the initial prefix
198197 let prefixesToExplore = [ startPrefix ] ;
199198
200- try {
201- // We determine the current depth by counting slashes in the startPrefix
202- const startDepth = ( startPrefix . match ( / \/ / g) || [ ] ) . length ;
203- logger . info ( { startPrefix, startDepth} , "prefix search" )
204-
205- // Loop from the current depth until we reach the target depth
206- for ( let currentDepth = startDepth ; currentDepth < targetDepth ; currentDepth ++ ) {
207- logger . info ( `Discovering prefixes at depth ${ currentDepth + 1 } ...` ) ;
208-
209- // Fetch all sub-prefixes for the current level in parallel
210- const promises = prefixesToExplore . map ( prefix => {
211- const command = new ListObjectsV2Command ( {
212- Bucket : AppServerConfig . S3 . S3_BUCKET_NAME ,
213- Prefix : prefix ,
214- Delimiter : '/' ,
215- } ) ;
216- return s3Client . send ( command ) ;
217- } ) ;
199+ // Determine the current depth by counting slashes in the startPrefix
200+ const startDepth = ( startPrefix . match ( / \/ / g) || [ ] ) . length ;
201+ logger . debug ( { startPrefix, startDepth} , "prefix search" )
218202
219- const responses = await Promise . all ( promises ) ;
203+ // Loop from the current depth until we reach the target depth
204+ for ( let currentDepth = startDepth ; currentDepth < targetDepth ; currentDepth ++ ) {
205+ logger . debug ( `Discovering prefixes at depth ${ currentDepth + 1 } ...` ) ;
220206
221- // Collect all the newly found "subfolders"
222- const nextLevelPrefixes = responses . flatMap (
223- response => response . CommonPrefixes ?. map ( p => p . Prefix ! ) . filter ( Boolean ) || [ ]
224- ) ;
207+ // Fetch all sub-prefixes for the current level in parallel
208+ const promises = prefixesToExplore . map ( prefix => {
209+ const command = new ListObjectsV2Command ( {
210+ Bucket : AppServerConfig . S3 . S3_BUCKET_NAME ,
211+ Prefix : prefix ,
212+ Delimiter : '/' ,
213+ } ) ;
214+ return s3Client . send ( command ) ;
215+ } ) ;
225216
226- if ( nextLevelPrefixes . length === 0 ) {
227- logger . warn ( `No further prefixes found at depth ${ currentDepth + 1 } . Stopping.` ) ;
228- prefixesToExplore = [ ] ; // Stop the loop
229- break ;
230- }
217+ const responses = await Promise . all ( promises ) ;
231218
232- prefixesToExplore = nextLevelPrefixes ;
233- logger . info ( { prefixesToExplore} , "prefixes in between" ) ;
219+ // Collect all the newly found "subfolders"
220+ const nextLevelPrefixes = responses . flatMap (
221+ response => response . CommonPrefixes ?. map ( p => p . Prefix ! ) . filter ( Boolean ) || [ ]
222+ ) ;
223+
224+ if ( nextLevelPrefixes . length === 0 ) {
225+ logger . warn ( `No further prefixes found at depth ${ currentDepth + 1 } . Stopping.` ) ;
226+ prefixesToExplore = [ ] ; // Stop the loop
227+ break ;
234228 }
235229
236- // The loop is finished, prefixesToExplore now holds our campaign-level prefixes
237- const finalPrefixes = prefixesToExplore . sort ( ) ;
230+ prefixesToExplore = nextLevelPrefixes ;
231+ logger . debug ( { prefixesToExplore} , "prefixes in between" ) ;
232+ }
238233
239- return {
240- prefixes : finalPrefixes ,
241- count : finalPrefixes . length ,
242- } ;
234+ // The loop is finished, prefixesToExplore now holds our campaign-level prefixes
235+ const finalPrefixes = prefixesToExplore . sort ( ) ;
243236
244- } catch ( error : any ) {
245- logger . error ( `S3 Util: Error in findLeafPrefixes for prefix ${ startPrefix } :` , error ) ;
246- throw error ;
247- }
237+ return {
238+ prefixes : finalPrefixes ,
239+ count : finalPrefixes . length ,
240+ } ;
248241}
249242
250243/**
0 commit comments