1- const AWS = require ( 'aws-sdk' ) ;
2- AWS . config . logger = console ;
3- const { S3 } = require ( 'aws-sdk' ) ;
1+ const {
2+ S3Client,
3+ HeadBucketCommand,
4+ CreateBucketCommand,
5+ DeleteBucketCommand,
6+ ListObjectVersionsCommand,
7+ DeleteObjectCommand,
8+ ListBucketsCommand,
9+ } = require ( '@aws-sdk/client-s3' ) ;
410const projectFixture = require ( '../fixtures/project' ) ;
511const getConfig = require ( '../../test/support/config' ) ;
612
713class BucketUtility {
8- constructor ( profile = 'default' , config = { } ) {
14+ constructor ( profile = 'default' , config = { } , unauthenticated = false ) {
915 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+ if ( unauthenticated ) {
17+ this . s3 = new S3Client ( {
18+ ...s3Config ,
19+ maxAttempts : 0 ,
20+ credentials : { accessKeyId : '' , secretAccessKey : '' } ,
21+ forcePathStyle : true ,
22+ signer : { sign : async request => request } ,
23+ } ) ;
24+ }
25+ else {
26+ this . s3 = new S3Client ( {
27+ ...s3Config ,
28+ maxAttempts : 0 ,
29+ } ) ;
30+ }
1631 }
1732
1833 bucketExists ( bucketName ) {
19- return this . s3
20- . headBucket ( { Bucket : bucketName } )
21- . promise ( )
34+ return this . s3 . send ( new HeadBucketCommand ( { Bucket : bucketName } ) )
2235 . then ( ( ) => true )
2336 . catch ( err => {
24- if ( err . code === 'NotFound' ) {
37+ if ( err . name === 'NotFound' ) {
2538 return false ;
2639 }
2740 throw err ;
2841 } ) ;
2942 }
3043
3144 createOne ( bucketName ) {
32- return this . s3
33- . createBucket ( { Bucket : bucketName } )
34- . promise ( )
35- . then ( ( ) => bucketName ) ;
45+ return this . s3 . send ( new CreateBucketCommand ( { Bucket : bucketName } ) )
46+ . then ( ( ) => bucketName )
47+ . catch ( err => {
48+ throw err ;
49+ } ) ;
3650 }
3751
3852 createOneWithLock ( bucketName ) {
39- return this . s3
40- . createBucket ( {
41- Bucket : bucketName ,
42- ObjectLockEnabledForBucket : true ,
43- } )
44- . promise ( )
45- . then ( ( ) => bucketName ) ;
53+ return this . s3 . send ( new CreateBucketCommand ( {
54+ Bucket : bucketName ,
55+ ObjectLockEnabledForBucket : true ,
56+ } ) ) . then ( ( ) => bucketName ) ;
4657 }
4758
4859 createMany ( bucketNames ) {
4960 const promises = bucketNames . map ( bucketName =>
5061 this . createOne ( bucketName ) ,
5162 ) ;
52-
5363 return Promise . all ( promises ) ;
5464 }
5565
5666 createRandom ( nBuckets = 1 ) {
5767 if ( nBuckets === 1 ) {
5868 const bucketName = projectFixture . generateBucketName ( ) ;
59-
6069 return this . createOne ( bucketName ) ;
6170 }
62-
6371 const bucketNames = projectFixture
6472 . generateManyBucketNames ( nBuckets )
65- . sort ( ( ) => 0.5 - Math . random ( ) ) ; // Simply shuffle array
66-
73+ . sort ( ( ) => 0.5 - Math . random ( ) ) ;
6774 return this . createMany ( bucketNames ) ;
6875 }
6976
7077 deleteOne ( bucketName ) {
71- return this . s3 . deleteBucket ( { Bucket : bucketName } ) . promise ( ) ;
78+ return this . s3 . send ( new DeleteBucketCommand ( { Bucket : bucketName } ) ) ;
7279 }
7380
7481 deleteMany ( bucketNames ) {
7582 const promises = bucketNames . map ( bucketName =>
7683 this . deleteOne ( bucketName ) ,
7784 ) ;
78-
7985 return Promise . all ( promises ) ;
8086 }
81-
87+
8288 /**
8389 * Recursively delete all versions of all objects within the bucket
8490 * @param bucketName
8591 * @returns {Promise.<T> }
8692 */
87-
88- async empty ( bucketName , BypassGovernanceRetention = false ) {
93+ empty ( bucketName , BypassGovernanceRetention = false ) {
8994 const param = {
9095 Bucket : bucketName ,
9196 } ;
9297
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 ( ) ;
140- }
98+ return this . s3 . send ( new ListObjectVersionsCommand ( param ) )
99+ . then ( data => Promise . all (
100+ ( data . Versions || [ ] )
101+ . filter ( object => ! object . Key . endsWith ( '/' ) )
102+ . map ( object =>
103+ this . s3 . send ( new DeleteObjectCommand ( {
104+ Bucket : bucketName ,
105+ Key : object . Key ,
106+ VersionId : object . VersionId ,
107+ ...( BypassGovernanceRetention && { BypassGovernanceRetention } ) ,
108+ } ) ) . then ( ( ) => object )
109+ )
110+ . concat ( ( data . Versions || [ ] )
111+ . filter ( object => object . Key . endsWith ( '/' ) )
112+ . map ( object =>
113+ this . s3 . send ( new DeleteObjectCommand ( {
114+ Bucket : bucketName ,
115+ Key : object . Key ,
116+ VersionId : object . VersionId ,
117+ ...( BypassGovernanceRetention && { BypassGovernanceRetention } ) ,
118+ } ) )
119+ . then ( ( ) => object )
120+ )
121+ )
122+ . concat ( ( data . DeleteMarkers || [ ] )
123+ . map ( object =>
124+ this . s3 . send ( new DeleteObjectCommand ( {
125+ Bucket : bucketName ,
126+ Key : object . Key ,
127+ VersionId : object . VersionId ,
128+ ...( BypassGovernanceRetention && { BypassGovernanceRetention } ) ,
129+ } ) )
130+ . then ( ( ) => object )
131+ )
132+ )
133+ )
134+ ) ;
141135 }
142136
143137 emptyMany ( bucketNames ) {
144- const promises = bucketNames . map ( bucketName => this . empty ( bucketName ) ) ;
145-
138+ const promises = bucketNames . map (
139+ bucketName => this . empty ( bucketName )
140+ ) ;
146141 return Promise . all ( promises ) ;
147142 }
148-
143+
149144 emptyIfExists ( bucketName ) {
150145 return this . bucketExists ( bucketName ) . then ( exists => {
151146 if ( exists ) {
@@ -159,15 +154,15 @@ class BucketUtility {
159154 const promises = bucketNames . map ( bucketName =>
160155 this . emptyIfExists ( bucketName ) ,
161156 ) ;
162-
163157 return Promise . all ( promises ) ;
164158 }
165159
166160 getOwner ( ) {
167- return this . s3
168- . listBuckets ( )
169- . promise ( )
170- . then ( data => data . Owner ) ;
161+ return this . s3 . send ( new ListBucketsCommand ( { } ) )
162+ . then ( data => data . Owner )
163+ . catch ( err => {
164+ throw err ;
165+ } ) ;
171166 }
172167}
173168
0 commit comments