From 77244c6fc5f0d98e93a3e1d8e2b1383fc5aa22ea Mon Sep 17 00:00:00 2001 From: vahid Date: Tue, 24 Dec 2024 19:48:53 +0330 Subject: [PATCH] fix issue #230 --- index.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 7ef2d72..d0589ee 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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; + } + } } }