Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidalizad committed Dec 24, 2024
1 parent 5871b42 commit 77244c6
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
PutObjectCommand,
DeleteObjectCommand,
GetObjectCommand,
HeadBucketCommand,
} = require('@aws-sdk/client-s3');
const { getSignedUrl } = require('@aws-sdk/s3-request-presigner');
const optionsFromArguments = require('./lib/optionsFromArguments');
Expand Down Expand Up @@ -112,13 +113,27 @@ class S3Adapter {
}

try {
await this._s3Client.send(new CreateBucketCommand({ Bucket: this._bucket }));
// Check if the bucket exists
await this._s3Client.send(new HeadBucketCommand({ Bucket: this._bucket }));
this._hasBucket = true;
} catch (error) {
if (error.name === 'BucketAlreadyOwnedByYou') { this._hasBucket = true; }
else {
if (error.name !== 'NotFound') {
// If the error is something other than "NotFound", rethrow it
throw error;
}

// If the bucket does not exist, attempt to create it
try {
await this._s3Client.send(new CreateBucketCommand({ Bucket: this._bucket }));
this._hasBucket = true;
} catch (creationError) {
// Handle specific errors during bucket creation
if (creationError.name === 'BucketAlreadyExists' || creationError.name === 'BucketAlreadyOwnedByYou') {
this._hasBucket = true;
} else {
throw creationError;
}
}
}
}

Expand Down

0 comments on commit 77244c6

Please sign in to comment.