@@ -13,6 +13,8 @@ type pluginOptionsType = {
13
13
buckets : string [ ] ;
14
14
} ;
15
15
16
+ type ObjectType = AWS . S3 . Object & { Bucket : string } ;
17
+
16
18
// source all objects from s3
17
19
export async function sourceNodes (
18
20
{ actions : { createNode } , createNodeId, createContentDigest, reporter } ,
@@ -26,42 +28,55 @@ export async function sourceNodes(
26
28
// get objects
27
29
const s3 = new AWS . S3 ( ) ;
28
30
29
- try {
31
+ const listObjects = async bucket => {
30
32
// todo improve this call
31
33
// see https://stackoverflow.com/a/49888947
32
34
const response = await s3
33
35
. listObjectsV2 ( {
34
- // todo handle several buckets
35
- Bucket : buckets [ 0 ]
36
+ Bucket : bucket
36
37
// todo handle continuation token
37
38
// ContinuationToken: token,
38
39
} )
39
40
. promise ( ) ;
40
41
42
+ // add bucket key
43
+ const objects = response . Contents ?. reduce ( ( acc : ObjectType [ ] , cur ) => {
44
+ const object : ObjectType = { ...cur , Bucket : bucket } ;
45
+ acc . push ( object ) ;
46
+ return acc ;
47
+ } , [ ] ) ;
48
+
49
+ return objects ;
50
+ } ;
51
+
52
+ try {
53
+ const objects = await Promise . all (
54
+ buckets . map ( bucket => listObjects ( bucket ) )
55
+ ) ;
56
+
41
57
// create file nodes
42
58
// todo touch nodes if they exist already
43
- response . Contents &&
44
- response . Contents . forEach ( async object => {
45
- const { Key } = object ;
46
- const { region } = awsConfig ;
47
- const node = {
48
- // node meta
49
- id : createNodeId ( `s3-object-${ object . Key } ` ) ,
50
- parent : null ,
51
- children : [ ] ,
52
- internal : {
53
- type : "S3Object" ,
54
- content : JSON . stringify ( object ) ,
55
- contentDigest : createContentDigest ( object )
56
- } ,
57
- // s3 object data
58
- Url : `https://s3.${ region ? `${ region } .` : "" } amazonaws.com/${
59
- buckets [ 0 ]
60
- } /${ Key } `,
61
- ...object
62
- } ;
63
- createNode ( node ) ;
59
+ objects ?. flat ( ) . forEach ( async object => {
60
+ const { Key, Bucket } = object ;
61
+ const { region } = awsConfig ;
62
+
63
+ createNode ( {
64
+ ...object ,
65
+ // construct url
66
+ Url : `https://s3.${
67
+ region ? `${ region } .` : ""
68
+ } amazonaws.com/${ Bucket } /${ Key } `,
69
+ // node meta
70
+ id : createNodeId ( `s3-object-${ Key } ` ) ,
71
+ parent : null ,
72
+ children : [ ] ,
73
+ internal : {
74
+ type : "S3Object" ,
75
+ content : JSON . stringify ( object ) ,
76
+ contentDigest : createContentDigest ( object )
77
+ }
64
78
} ) ;
79
+ } ) ;
65
80
} catch ( error ) {
66
81
reporter . error ( error ) ;
67
82
}
0 commit comments