File tree Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Original file line number Diff line number Diff line change 88 PutObjectCommand,
99 DeleteObjectCommand,
1010 GetObjectCommand,
11+ HeadBucketCommand,
1112} = require ( '@aws-sdk/client-s3' ) ;
1213const { getSignedUrl } = require ( '@aws-sdk/s3-request-presigner' ) ;
1314const optionsFromArguments = require ( './lib/optionsFromArguments' ) ;
@@ -112,13 +113,27 @@ class S3Adapter {
112113 }
113114
114115 try {
115- await this . _s3Client . send ( new CreateBucketCommand ( { Bucket : this . _bucket } ) ) ;
116+ // Check if the bucket exists
117+ await this . _s3Client . send ( new HeadBucketCommand ( { Bucket : this . _bucket } ) ) ;
116118 this . _hasBucket = true ;
117119 } catch ( error ) {
118- if ( error . name === 'BucketAlreadyOwnedByYou ') { this . _hasBucket = true ; }
119- else {
120+ if ( error . name !== 'NotFound ') {
121+ // If the error is something other than "NotFound", rethrow it
120122 throw error ;
121123 }
124+
125+ // If the bucket does not exist, attempt to create it
126+ try {
127+ await this . _s3Client . send ( new CreateBucketCommand ( { Bucket : this . _bucket } ) ) ;
128+ this . _hasBucket = true ;
129+ } catch ( creationError ) {
130+ // Handle specific errors during bucket creation
131+ if ( creationError . name === 'BucketAlreadyExists' || creationError . name === 'BucketAlreadyOwnedByYou' ) {
132+ this . _hasBucket = true ;
133+ } else {
134+ throw creationError ;
135+ }
136+ }
122137 }
123138 }
124139
You can’t perform that action at this time.
0 commit comments