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+ credentials : { accessKeyId : '' , secretAccessKey : '' } ,
20+ forcePathStyle : true ,
21+ signer : { sign : async request => request } ,
22+ } ) ;
23+ }
24+ else {
25+ this . s3 = new S3Client ( {
26+ ...s3Config ,
27+ maxAttempts : 0 ,
28+ } ) ;
29+ }
1630 }
1731
1832 bucketExists ( bucketName ) {
19- return this . s3
20- . headBucket ( { Bucket : bucketName } ) . promise ( )
33+ return this . s3 . send ( new HeadBucketCommand ( { Bucket : bucketName } ) )
2134 . then ( ( ) => true )
2235 . catch ( err => {
23- if ( err . code === 'NotFound' ) {
36+ if ( err . name === 'NotFound' ) {
2437 return false ;
2538 }
2639 throw err ;
2740 } ) ;
2841 }
2942
3043 createOne ( bucketName ) {
31- return this . s3
32- . createBucket ( { Bucket : bucketName } ) . promise ( )
33- . then ( ( ) => bucketName ) ;
44+ return this . s3 . send ( new CreateBucketCommand ( { Bucket : bucketName } ) )
45+ . then ( ( ) => bucketName )
46+ . catch ( err => {
47+ throw err ;
48+ } ) ;
3449 }
3550
3651 createOneWithLock ( bucketName ) {
37- return this . s3 . createBucket ( {
52+ return this . s3 . send ( new CreateBucketCommand ( {
3853 Bucket : bucketName ,
3954 ObjectLockEnabledForBucket : true ,
40- } ) . promise ( )
41- . then ( ( ) => bucketName ) ;
55+ } ) )
56+ . then ( ( ) => bucketName )
57+ . catch ( err => {
58+ throw err ;
59+ } ) ;
4260 }
4361
4462 createMany ( bucketNames ) {
4563 const promises = bucketNames . map (
4664 bucketName => this . createOne ( bucketName )
4765 ) ;
48-
4966 return Promise . all ( promises ) ;
5067 }
51-
5268 createRandom ( nBuckets = 1 ) {
5369 if ( nBuckets === 1 ) {
5470 const bucketName = projectFixture . generateBucketName ( ) ;
55-
5671 return this . createOne ( bucketName ) ;
5772 }
58-
5973 const bucketNames = projectFixture
6074 . generateManyBucketNames ( nBuckets )
61- . sort ( ( ) => 0.5 - Math . random ( ) ) ; // Simply shuffle array
62-
75+ . sort ( ( ) => 0.5 - Math . random ( ) ) ;
6376 return this . createMany ( bucketNames ) ;
6477 }
6578
6679 deleteOne ( bucketName ) {
67- return this . s3
68- . deleteBucket ( { Bucket : bucketName } ) . promise ( ) ;
80+ return this . s3 . send ( new DeleteBucketCommand ( { Bucket : bucketName } ) )
81+ . catch ( err => {
82+ throw err ;
83+ } ) ;
6984 }
7085
7186 deleteMany ( bucketNames ) {
7287 const promises = bucketNames . map (
7388 bucketName => this . deleteOne ( bucketName )
7489 ) ;
75-
7690 return Promise . all ( promises ) ;
7791 }
78-
7992 /**
8093 * Recursively delete all versions of all objects within the bucket
8194 * @param bucketName
8295 * @returns {Promise.<T> }
8396 */
84-
8597 empty ( bucketName ) {
8698 const param = {
8799 Bucket : bucketName ,
88100 } ;
89101
90- return this . s3
91- . listObjectVersions ( param ) . promise ( )
92- . then ( data =>
93- Promise . all (
94- data . Versions
95- . filter ( object => ! object . Key . endsWith ( '/' ) )
96- // remove all objects
102+ return this . s3 . send ( new ListObjectVersionsCommand ( param ) )
103+ . then ( data => Promise . all (
104+ ( data . Versions || [ ] )
105+ . filter ( object => ! object . Key . endsWith ( '/' ) )
106+ . map ( object =>
107+ this . s3 . send ( new DeleteObjectCommand ( {
108+ Bucket : bucketName ,
109+ Key : object . Key ,
110+ VersionId : object . VersionId ,
111+ } ) )
112+ . then ( ( ) => object )
113+ )
114+ . concat ( ( data . Versions || [ ] )
115+ . filter ( object => object . Key . endsWith ( '/' ) )
97116 . map ( object =>
98- this . s3 . deleteObject ( {
99- Bucket : bucketName ,
100- Key : object . Key ,
101- VersionId : object . VersionId ,
102- } ) . promise ( )
103- . then ( ( ) => object )
104- )
105- . concat ( data . Versions
106- . filter ( object => object . Key . endsWith ( '/' ) )
107- // remove all directories
108- . map ( object =>
109- this . s3 . deleteObject ( {
110- Bucket : bucketName ,
111- Key : object . Key ,
112- VersionId : object . VersionId ,
113- } ) . promise ( )
117+ this . s3 . send ( new DeleteObjectCommand ( {
118+ Bucket : bucketName ,
119+ Key : object . Key ,
120+ VersionId : object . VersionId ,
121+ } ) )
114122 . then ( ( ) => object )
115- )
116123 )
117- . concat ( data . DeleteMarkers
118- . map ( object =>
119- this . s3 . deleteObject ( {
120- Bucket : bucketName ,
121- Key : object . Key ,
122- VersionId : object . VersionId ,
123- } ) . promise ( )
124- . then ( ( ) => object ) ) )
125- )
126- ) ;
124+ )
125+ . concat ( ( data . DeleteMarkers || [ ] )
126+ . map ( object =>
127+ this . s3 . send ( new DeleteObjectCommand ( {
128+ Bucket : bucketName ,
129+ Key : object . Key ,
130+ VersionId : object . VersionId ,
131+ } ) )
132+ . then ( ( ) => object )
133+ ) )
134+ ) ) ;
127135 }
128136
129137 emptyMany ( bucketNames ) {
130- const promises = bucketNames . map (
131- bucketName => this . empty ( bucketName )
138+ const promises = bucketNames . map (
139+ bucketName => this . empty ( bucketName )
132140 ) ;
133141
134142 return Promise . all ( promises ) ;
135143 }
136-
137144 emptyIfExists ( bucketName ) {
138145 return this . bucketExists ( bucketName )
139146 . then ( exists => {
@@ -143,19 +150,19 @@ class BucketUtility {
143150 return undefined ;
144151 } ) ;
145152 }
146-
147153 emptyManyIfExists ( bucketNames ) {
148154 const promises = bucketNames . map (
149155 bucketName => this . emptyIfExists ( bucketName )
150156 ) ;
151-
152157 return Promise . all ( promises ) ;
153158 }
154159
155160 getOwner ( ) {
156- return this . s3
157- . listBuckets ( ) . promise ( )
158- . then ( data => data . Owner ) ;
161+ return this . s3 . send ( new ListBucketsCommand ( { } ) )
162+ . then ( data => data . Owner )
163+ . catch ( err => {
164+ throw err ;
165+ } ) ;
159166 }
160167}
161168
0 commit comments