import { randomBytes } from 'crypto' ; import S3 from 'aws-sdk/clients/s3' ; interface BucketHandler { bucketName : string ; destroy : ( ) => Promise < boolean > ; } /**
 * Helper to create a new bucket
 */ async function s3CreateBucket ( s3 , bucketName = randomBytes ( 8 ) . toString ( 'hex' ) ) { await s3 . createBucket ( { Bucket : bucketName , } ) . promise ( ) ; return { bucketName , async destroy ( ) { try { const { Contents } = await s3 . listObjects ( { Bucket : bucketName } ) . promise ( ) ; if ( Contents && Contents . length > 0 ) { function isObjectIdentifier ( obj ) { return typeof obj . Key === 'string' ; } await s3 . deleteObjects ( { Bucket : bucketName , Delete : { Objects : Contents . filter ( isObjectIdentifier ) . map ( ( { Key } ) => ( { Key , } ) ) , } , } ) . promise ( ) ; } await s3 . deleteBucket ( { Bucket : bucketName } ) . promise ( ) ; return true ; } catch ( err ) { console . log ( err ) ; return false ; } } , } ; } export type { BucketHandler } ; export { s3CreateBucket } ;