@@ -37,18 +37,11 @@ describe("s3 express CRUD test suite", () => {
37
37
beforeAll ( async ( ) => {
38
38
( { s3, controller, bucketName, recorder } = await createClientAndRecorder ( ) ) ;
39
39
40
- await deleteBuckets ( controller ) ;
41
40
await s3 . createBucket ( {
42
41
Bucket : bucketName ,
43
42
CreateBucketConfiguration : {
44
- Location : {
45
- Type : "AvailabilityZone" ,
46
- Name : zone ,
47
- } ,
48
- Bucket : {
49
- Type : "Directory" ,
50
- DataRedundancy : "SingleAvailabilityZone" ,
51
- } ,
43
+ Location : { Type : "AvailabilityZone" , Name : zone } ,
44
+ Bucket : { Type : "Directory" , DataRedundancy : "SingleAvailabilityZone" } ,
52
45
} ,
53
46
} ) ;
54
47
@@ -99,34 +92,22 @@ describe("s3 express CRUD test suite", () => {
99
92
} ) ;
100
93
101
94
afterAll ( async ( ) => {
102
- await deleteBuckets ( controller ) ;
95
+ await emptyAndDeleteBucket ( controller , bucketName ) ;
103
96
} ) ;
104
97
105
98
it ( "can create a bucket" , ( ) => {
106
99
expect ( createRecorder ) . toEqual ( {
107
- "CreateBucketCommand (normal)" : {
108
- [ bucketName ] : 1 ,
109
- } ,
110
- "HeadBucketCommand (s3 express)" : {
111
- [ bucketName ] : 1 ,
112
- } ,
113
- "CreateSessionCommand (normal)" : {
114
- [ bucketName ] : 1 ,
115
- } ,
100
+ "CreateBucketCommand (normal)" : { [ bucketName ] : 1 } ,
101
+ "HeadBucketCommand (s3 express)" : { [ bucketName ] : 1 } ,
102
+ "CreateSessionCommand (normal)" : { [ bucketName ] : 1 } ,
116
103
} ) ;
117
104
} ) ;
118
105
119
106
it ( "can read/write/delete from a bucket" , ( ) => {
120
107
expect ( readWriteDeleteRecorder ) . toEqual ( {
121
- "PutObjectCommand (s3 express)" : {
122
- [ bucketName ] : SCALE ,
123
- } ,
124
- "GetObjectCommand (s3 express)" : {
125
- [ bucketName ] : SCALE ,
126
- } ,
127
- "DeleteObjectCommand (s3 express)" : {
128
- [ bucketName ] : SCALE ,
129
- } ,
108
+ "PutObjectCommand (s3 express)" : { [ bucketName ] : SCALE } ,
109
+ "GetObjectCommand (s3 express)" : { [ bucketName ] : SCALE } ,
110
+ "DeleteObjectCommand (s3 express)" : { [ bucketName ] : SCALE } ,
130
111
} ) ;
131
112
} ) ;
132
113
@@ -209,12 +190,10 @@ describe("s3 express CRUD test suite", () => {
209
190
} ) ;
210
191
211
192
async function createClientAndRecorder ( ) {
212
- const sts = new STS ( {
213
- region,
214
- } ) ;
193
+ const sts = new STS ( { region } ) ;
215
194
const accountId = ( await sts . getCallerIdentity ( { } ) ) . Account ;
216
195
217
- const bucketName = `${ accountId } -js-test-bucket-${ ( Date . now ( ) / 1000 ) | 0 } --${ suffix } ` ;
196
+ const bucketName = `${ accountId } -js-test-bucket-${ ( Math . random ( ) + 1 ) . toString ( 36 ) . substring ( 2 ) } --${ suffix } ` ;
218
197
219
198
const s3 = new S3 ( {
220
199
region,
@@ -261,52 +240,34 @@ async function createClientAndRecorder() {
261
240
} ;
262
241
}
263
242
264
- async function deleteBuckets ( s3 : S3 ) {
265
- const buckets = await s3 . listDirectoryBuckets ( { } ) ;
266
-
267
- for ( const bucket of buckets . Buckets ?? [ ] ) {
268
- const Bucket = bucket . Name ;
243
+ async function emptyAndDeleteBucket ( s3 : S3 , bucketName : string ) {
244
+ const Bucket = bucketName ;
245
+ try {
246
+ await s3 . headBucket ( { Bucket } ) ;
247
+ } catch ( e ) {
248
+ return ;
249
+ }
269
250
270
- try {
271
- await s3 . headBucket ( {
272
- Bucket,
273
- } ) ;
274
- } catch ( e ) {
275
- return ;
251
+ const list = await s3 . listObjectsV2 ( { Bucket } ) . catch ( ( e ) => {
252
+ if ( ! String ( e ) . includes ( "NoSuchBucket" ) ) {
253
+ throw e ;
276
254
}
255
+ return {
256
+ Contents : [ ] ,
257
+ } ;
258
+ } ) ;
277
259
278
- const list = await s3
279
- . listObjectsV2 ( {
280
- Bucket,
281
- } )
282
- . catch ( ( e ) => {
283
- if ( ! String ( e ) . includes ( "NoSuchBucket" ) ) {
284
- throw e ;
285
- }
286
- return {
287
- Contents : [ ] ,
288
- } ;
289
- } ) ;
290
-
291
- const promises = [ ] as Promise < any > [ ] ;
292
- for ( const key of list . Contents ?? [ ] ) {
293
- promises . push (
294
- s3 . deleteObject ( {
295
- Bucket,
296
- Key : key . Key ,
297
- } )
298
- ) ;
299
- }
300
- await Promise . all ( promises ) ;
260
+ const promises = [ ] as Promise < any > [ ] ;
261
+ for ( const key of list . Contents ?? [ ] ) {
262
+ promises . push ( s3 . deleteObject ( { Bucket, Key : key . Key } ) ) ;
263
+ }
264
+ await Promise . all ( promises ) ;
301
265
302
- try {
303
- return await s3 . deleteBucket ( {
304
- Bucket,
305
- } ) ;
306
- } catch ( e ) {
307
- if ( ! String ( e ) . includes ( "NoSuchBucket" ) ) {
308
- throw e ;
309
- }
266
+ try {
267
+ return await s3 . deleteBucket ( { Bucket } ) ;
268
+ } catch ( e ) {
269
+ if ( ! String ( e ) . includes ( "NoSuchBucket" ) ) {
270
+ throw e ;
310
271
}
311
272
}
312
273
}
0 commit comments