@@ -5,160 +5,170 @@ const projectFixture = require('../fixtures/project');
55const getConfig = require ( '../../test/support/config' ) ;
66
77class BucketUtility {
8- constructor ( profile = 'default' , config = { } ) {
9- const s3Config = getConfig ( profile , config ) ;
10-
11- this . s3 = new S3 ( s3Config ) ;
12- this . s3 . config . setPromisesDependency ( Promise ) ;
13- this . s3 . config . update ( {
14- maxRetries : 0 ,
15- } ) ;
16- }
17-
18- bucketExists ( bucketName ) {
19- return this . s3
20- . headBucket ( { Bucket : bucketName } )
21- . promise ( )
22- . then ( ( ) => true )
23- . catch ( err => {
24- if ( err . code === 'NotFound' ) {
25- return false ;
8+ constructor ( profile = 'default' , config = { } ) {
9+ const s3Config = getConfig ( profile , config ) ;
10+
11+ this . s3 = new S3 ( s3Config ) ;
12+ this . s3 . config . setPromisesDependency ( Promise ) ;
13+ this . s3 . config . update ( {
14+ maxRetries : 0 ,
15+ } ) ;
16+ }
17+
18+ bucketExists ( bucketName ) {
19+ return this . s3
20+ . headBucket ( { Bucket : bucketName } )
21+ . promise ( )
22+ . then ( ( ) => true )
23+ . catch ( err => {
24+ if ( err . code === 'NotFound' ) {
25+ return false ;
26+ }
27+ throw err ;
28+ } ) ;
29+ }
30+
31+ createOne ( bucketName ) {
32+ return this . s3
33+ . createBucket ( { Bucket : bucketName } )
34+ . promise ( )
35+ . then ( ( ) => bucketName ) ;
36+ }
37+
38+ createOneWithLock ( bucketName ) {
39+ return this . s3
40+ . createBucket ( {
41+ Bucket : bucketName ,
42+ ObjectLockEnabledForBucket : true ,
43+ } )
44+ . promise ( )
45+ . then ( ( ) => bucketName ) ;
46+ }
47+
48+ createMany ( bucketNames ) {
49+ const promises = bucketNames . map ( bucketName =>
50+ this . createOne ( bucketName ) ,
51+ ) ;
52+
53+ return Promise . all ( promises ) ;
54+ }
55+
56+ createRandom ( nBuckets = 1 ) {
57+ if ( nBuckets === 1 ) {
58+ const bucketName = projectFixture . generateBucketName ( ) ;
59+
60+ return this . createOne ( bucketName ) ;
61+ }
62+
63+ const bucketNames = projectFixture
64+ . generateManyBucketNames ( nBuckets )
65+ . sort ( ( ) => 0.5 - Math . random ( ) ) ; // Simply shuffle array
66+
67+ return this . createMany ( bucketNames ) ;
68+ }
69+
70+ deleteOne ( bucketName ) {
71+ return this . s3 . deleteBucket ( { Bucket : bucketName } ) . promise ( ) ;
72+ }
73+
74+ deleteMany ( bucketNames ) {
75+ const promises = bucketNames . map ( bucketName =>
76+ this . deleteOne ( bucketName ) ,
77+ ) ;
78+
79+ return Promise . all ( promises ) ;
80+ }
81+
82+ /**
83+ * Recursively delete all versions of all objects within the bucket
84+ * @param bucketName
85+ * @returns {Promise.<T> }
86+ */
87+
88+ async empty ( bucketName , BypassGovernanceRetention = false ) {
89+ const param = {
90+ Bucket : bucketName ,
91+ } ;
92+
93+ const listedObjects = await this . s3 . listObjectVersions ( param ) . promise ( ) ;
94+
95+ for ( const version of listedObjects . Versions ) {
96+ if ( version . Key . endsWith ( '/' ) ) {
97+ continue ;
98+ }
99+
100+ await this . s3
101+ . deleteObject ( {
102+ Bucket : bucketName ,
103+ Key : version . Key ,
104+ VersionId : version . VersionId ,
105+ ...( BypassGovernanceRetention && {
106+ BypassGovernanceRetention,
107+ } ) ,
108+ } )
109+ . promise ( ) ;
110+ }
111+
112+ for ( const version of listedObjects . Versions ) {
113+ if ( ! version . Key . endsWith ( '/' ) ) {
114+ continue ;
115+ }
116+
117+ await this . s3
118+ . deleteObject ( {
119+ Bucket : bucketName ,
120+ Key : version . Key ,
121+ VersionId : version . VersionId ,
122+ ...( BypassGovernanceRetention && {
123+ BypassGovernanceRetention,
124+ } ) ,
125+ } )
126+ . promise ( ) ;
127+ }
128+
129+ for ( const marker of listedObjects . DeleteMarkers ) {
130+ await this . s3
131+ . deleteObject ( {
132+ Bucket : bucketName ,
133+ Key : marker . Key ,
134+ VersionId : marker . VersionId ,
135+ ...( BypassGovernanceRetention && {
136+ BypassGovernanceRetention,
137+ } ) ,
138+ } )
139+ . promise ( ) ;
26140 }
27- throw err ;
28- } ) ;
29- }
30-
31- createOne ( bucketName ) {
32- return this . s3
33- . createBucket ( { Bucket : bucketName } )
34- . promise ( )
35- . then ( ( ) => bucketName ) ;
36- }
37-
38- createOneWithLock ( bucketName ) {
39- return this . s3
40- . createBucket ( {
41- Bucket : bucketName ,
42- ObjectLockEnabledForBucket : true ,
43- } )
44- . promise ( )
45- . then ( ( ) => bucketName ) ;
46- }
47-
48- createMany ( bucketNames ) {
49- const promises = bucketNames . map ( bucketName => this . createOne ( bucketName ) ) ;
50-
51- return Promise . all ( promises ) ;
52- }
53-
54- createRandom ( nBuckets = 1 ) {
55- if ( nBuckets === 1 ) {
56- const bucketName = projectFixture . generateBucketName ( ) ;
57-
58- return this . createOne ( bucketName ) ;
59141 }
60142
61- const bucketNames = projectFixture
62- . generateManyBucketNames ( nBuckets )
63- . sort ( ( ) => 0.5 - Math . random ( ) ) ; // Simply shuffle array
64-
65- return this . createMany ( bucketNames ) ;
66- }
67-
68- deleteOne ( bucketName ) {
69- return this . s3 . deleteBucket ( { Bucket : bucketName } ) . promise ( ) ;
70- }
71-
72- deleteMany ( bucketNames ) {
73- const promises = bucketNames . map ( bucketName => this . deleteOne ( bucketName ) ) ;
74-
75- return Promise . all ( promises ) ;
76- }
77-
78- /**
79- * Recursively delete all versions of all objects within the bucket
80- * @param bucketName
81- * @returns {Promise.<T> }
82- */
83-
84- async empty ( bucketName , BypassGovernanceRetention = false ) {
85- const param = {
86- Bucket : bucketName ,
87- } ;
88-
89- const listedObjects = await this . s3 . listObjectVersions ( param ) . promise ( ) ;
90-
91- for ( const version of listedObjects . Versions ) {
92- if ( version . Key . endsWith ( '/' ) ) {
93- continue ;
94- }
95-
96- await this . s3
97- . deleteObject ( {
98- Bucket : bucketName ,
99- Key : version . Key ,
100- VersionId : version . VersionId ,
101- ...( BypassGovernanceRetention && { BypassGovernanceRetention } ) ,
102- } )
103- . promise ( ) ;
143+ emptyMany ( bucketNames ) {
144+ const promises = bucketNames . map ( bucketName => this . empty ( bucketName ) ) ;
145+
146+ return Promise . all ( promises ) ;
104147 }
105148
106- for ( const version of listedObjects . Versions ) {
107- if ( ! version . Key . endsWith ( '/' ) ) {
108- continue ;
109- }
110-
111- await this . s3
112- . deleteObject ( {
113- Bucket : bucketName ,
114- Key : version . Key ,
115- VersionId : version . VersionId ,
116- ...( BypassGovernanceRetention && { BypassGovernanceRetention } ) ,
117- } )
118- . promise ( ) ;
149+ emptyIfExists ( bucketName ) {
150+ return this . bucketExists ( bucketName ) . then ( exists => {
151+ if ( exists ) {
152+ return this . empty ( bucketName ) ;
153+ }
154+ return undefined ;
155+ } ) ;
156+ }
157+
158+ emptyManyIfExists ( bucketNames ) {
159+ const promises = bucketNames . map ( bucketName =>
160+ this . emptyIfExists ( bucketName ) ,
161+ ) ;
162+
163+ return Promise . all ( promises ) ;
119164 }
120165
121- for ( const marker of listedObjects . DeleteMarkers ) {
122- await this . s3
123- . deleteObject ( {
124- Bucket : bucketName ,
125- Key : marker . Key ,
126- VersionId : marker . VersionId ,
127- ...( BypassGovernanceRetention && { BypassGovernanceRetention } ) ,
128- } )
129- . promise ( ) ;
166+ getOwner ( ) {
167+ return this . s3
168+ . listBuckets ( )
169+ . promise ( )
170+ . then ( data => data . Owner ) ;
130171 }
131- }
132-
133- emptyMany ( bucketNames ) {
134- const promises = bucketNames . map ( bucketName => this . empty ( bucketName ) ) ;
135-
136- return Promise . all ( promises ) ;
137- }
138-
139- emptyIfExists ( bucketName ) {
140- return this . bucketExists ( bucketName ) . then ( exists => {
141- if ( exists ) {
142- return this . empty ( bucketName ) ;
143- }
144- return undefined ;
145- } ) ;
146- }
147-
148- emptyManyIfExists ( bucketNames ) {
149- const promises = bucketNames . map ( bucketName =>
150- this . emptyIfExists ( bucketName ) ,
151- ) ;
152-
153- return Promise . all ( promises ) ;
154- }
155-
156- getOwner ( ) {
157- return this . s3
158- . listBuckets ( )
159- . promise ( )
160- . then ( data => data . Owner ) ;
161- }
162172}
163173
164174module . exports = BucketUtility ;
0 commit comments