Closed AllanOricil closed 1 month ago
Discovered that when the bucket doesn't exist, the error is Unknown
https://github.com/rusoto/rusoto/issues/1099
which isn't documented in the sdk doc
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/s3/command/HeadBucketCommand/
Apparently someone reported it already, but it was never fixed. The error.name is also not set as "NoFound". The following code doesn't work either.
async bucketExists(bucketName: string): Promise<boolean> {
try {
const headBucketCommand = new HeadBucketCommand({
Bucket: bucketName,
});
await this.client.send(headBucketCommand);
// NOTE: if no error is thrown, bucket exists
return true;
} catch (error) {
// NOTE: if err code is NotFound, bucket doesn't exist
if ((error as Error).name === "NotFound") {
return false;
}
throw error;
}
}
Just came back to say that I found the real root cause of this issue. This error actually occurs if you pass an invalid bucket name.
The following code works, contrary to what I said previously.
async bucketExists(bucketName: string): Promise<boolean> {
try {
const headBucketCommand = new HeadBucketCommand({
Bucket: bucketName,
});
await this.client.send(headBucketCommand);
// NOTE: if no error is thrown, bucket exists
return true;
} catch (error) {
// NOTE: if err code is NotFound, bucket doesn't exist
if ((error as Error).name === "NotFound") {
return false;
}
throw error;
}
}
AWS SDK V3 S3 Module is throwing an exception when executing
HeadBucketCommand
In the next images, you can see that the message
bucket ${s3BucketName} exists? ${bucketExists}
isn't displayed proving that the exeception happens right when callingHeadBucketCommand
. Since the error that was thrown isn't of typeNotFound
, my method let the exception propagate to stop execution. However, because the error is unknown I can't figure out what it is. The only clue I have is that it comes from@smithy/smithy-client
This issue wasn't happening 2 weeks ago