Skip to content

Commit 77244c6

Browse files
committed
fix issue #230
1 parent 5871b42 commit 77244c6

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

index.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
PutObjectCommand,
99
DeleteObjectCommand,
1010
GetObjectCommand,
11+
HeadBucketCommand,
1112
} = require('@aws-sdk/client-s3');
1213
const { getSignedUrl } = require('@aws-sdk/s3-request-presigner');
1314
const 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

0 commit comments

Comments
 (0)