@@ -25,6 +25,7 @@ import { ReadableStream } from "stream/web"
25
25
import { NodeJsClient } from "@smithy/types"
26
26
import tracer from "dd-trace"
27
27
import { pipeline } from "stream/promises"
28
+ import { utils } from "@budibase/shared-core"
28
29
29
30
// use this as a temporary store of buckets that are being created
30
31
const STATE = {
@@ -440,22 +441,30 @@ export async function retrieveDirectory(bucketName: string, path: string) {
440
441
await fsp . mkdir ( writePath , { recursive : true } )
441
442
442
443
let numObjects = 0
443
- for await ( const object of listAllObjects ( bucketName , path ) ) {
444
- numObjects ++
445
-
446
- const filename = object . Key !
447
- const stream = await getReadStream ( bucketName , filename )
448
- const possiblePath = filename . split ( "/" )
449
- const dirs = possiblePath . slice ( 0 , possiblePath . length - 1 )
450
- const possibleDir = join ( writePath , ...dirs )
451
- if ( possiblePath . length > 1 && ! fs . existsSync ( possibleDir ) ) {
452
- await fsp . mkdir ( possibleDir , { recursive : true } )
453
- }
454
- await pipeline (
455
- stream ,
456
- fs . createWriteStream ( join ( writePath , ...possiblePath ) , { mode : 0o644 } )
457
- )
458
- }
444
+ await utils . parallelForeach (
445
+ listAllObjects ( bucketName , path ) ,
446
+ async object => {
447
+ numObjects ++
448
+ await tracer . trace ( "retrieveDirectory.object" , async span => {
449
+ const filename = object . Key !
450
+ span . addTags ( { filename } )
451
+ const stream = await getReadStream ( bucketName , filename )
452
+ const possiblePath = filename . split ( "/" )
453
+ const dirs = possiblePath . slice ( 0 , possiblePath . length - 1 )
454
+ const possibleDir = join ( writePath , ...dirs )
455
+ if ( possiblePath . length > 1 && ! fs . existsSync ( possibleDir ) ) {
456
+ await fsp . mkdir ( possibleDir , { recursive : true } )
457
+ }
458
+ await pipeline (
459
+ stream ,
460
+ fs . createWriteStream ( join ( writePath , ...possiblePath ) , {
461
+ mode : 0o644 ,
462
+ } )
463
+ )
464
+ } )
465
+ } ,
466
+ 3 /* max concurrency */
467
+ )
459
468
460
469
span . addTags ( { numObjects } )
461
470
return writePath
@@ -592,18 +601,25 @@ export async function getReadStream(
592
601
bucketName : string ,
593
602
path : string
594
603
) : Promise < Readable > {
595
- bucketName = sanitizeBucket ( bucketName )
596
- path = sanitizeKey ( path )
597
- const client = ObjectStore ( )
598
- const params = {
599
- Bucket : bucketName ,
600
- Key : path ,
601
- }
602
- const response = await client . getObject ( params )
603
- if ( ! response . Body || ! ( response . Body instanceof stream . Readable ) ) {
604
- throw new Error ( "Unable to retrieve stream - invalid response" )
605
- }
606
- return response . Body
604
+ return await tracer . trace ( "getReadStream" , async span => {
605
+ bucketName = sanitizeBucket ( bucketName )
606
+ path = sanitizeKey ( path )
607
+ span . addTags ( { bucketName, path } )
608
+ const client = ObjectStore ( )
609
+ const params = {
610
+ Bucket : bucketName ,
611
+ Key : path ,
612
+ }
613
+ const response = await client . getObject ( params )
614
+ if ( ! response . Body || ! ( response . Body instanceof stream . Readable ) ) {
615
+ throw new Error ( "Unable to retrieve stream - invalid response" )
616
+ }
617
+ span . addTags ( {
618
+ contentLength : response . ContentLength ,
619
+ contentType : response . ContentType ,
620
+ } )
621
+ return response . Body
622
+ } )
607
623
}
608
624
609
625
export async function getObjectMetadata (
0 commit comments