Reputation: 68
I was working on s3 i was creating a corner case when the user put the wrong bucket which is not present on the s3 but when I did the request call with an invalid bucket name I am still getting the error message as 'Bucket does not exist.
So I didn't get that like if we pass the correct format we should be getting the message as follow 'Bucket doesn't exist'.
But in case of incorrect form of bucket it should be giving the error message for invalid bucket name and error code according to that
Upvotes: 1
Views: 1266
Reputation: 8783
When you say invalid bucket-name that has to be dealt separately by your code. And you can use the below code to check if the bucket exist:
const checkBucketExists = async bucket => {
// here you can add the validation for bucketName
// and based on the validation you can return the status code.
const s3 = new AWS.S3();
const options = {
Bucket: bucket,
};
try {
await s3.headBucket(options).promise();
return true;
} catch (error) {
if (error.statusCode === 404) {
return false;
}
throw error;
}
};
Upvotes: 2