Last active
February 8, 2022 05:36
-
-
Save ruandre/4079470e4a721f59c10536251851a333 to your computer and use it in GitHub Desktop.
Check if S3 bucket exists using AWS SDK JavaScript Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk') | |
const s3 = new AWS.S3() | |
const BUCKET_NAME = 'MyBucket' | |
async function main() { | |
try { | |
const data = await s3.headBucket({ Bucket: BUCKET_NAME }).promise() | |
return `Bucket "${BUCKET_NAME}" exists` | |
} catch (err) { | |
if (err.statusCode === 403) { | |
return `Bucket "${BUCKET_NAME}" Access Denied` | |
} | |
if (err.statusCode >= 400 && err.statusCode < 500) { | |
return `Bucket "${BUCKET_NAME}" Not Found` | |
} | |
throw err | |
} | |
} | |
main() // until https://github.com/tc39/proposal-top-level-await |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah good catch; have updated :)