Created
September 24, 2018 07:44
-
-
Save trotzig/b8a2f76d1c743dd85ace9f6537b86613 to your computer and use it in GitHub Desktop.
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_REGION: region, | |
S3_BUCKET_NAME: Bucket, | |
S3_ACCESS_KEY_ID: accessKeyId, | |
S3_SECRET_ACCESS_KEY: secretAccessKey, | |
} = process.env; | |
AWS.config.update({ | |
region, | |
accessKeyId, | |
secretAccessKey, | |
}); | |
const s3 = new AWS.S3(); | |
const s3Wrapper = { | |
upload: ({ name, buffer }) => { | |
return new Promise((resolve, reject) => { | |
const params = { | |
Bucket, | |
Key: name, | |
Body: buffer, | |
ContentType: 'image/png', | |
ACL: 'public-read', | |
}; | |
s3.upload(params, (err) => { | |
if (err) { | |
reject(err); | |
return; | |
} | |
resolve(); | |
}); | |
}); | |
}, | |
remove: ({ name, buffer }) => { | |
return new Promise((resolve, reject) => { | |
const params = { | |
Bucket, | |
Key: name, | |
}; | |
s3.deleteObject(params, (err) => { | |
if (err) { | |
reject(err); | |
return; | |
} | |
resolve(); | |
}); | |
}); | |
}, | |
}; | |
async function validateS3Bucket() { | |
try { | |
console.log('Validating S3 account...') | |
const rnd = Math.random().toString(36).slice(2); | |
const name = `tmp-${rnd}`; | |
const buffer = Buffer.from('', 'utf8'); | |
await s3Wrapper.upload({ name, buffer }); | |
await s3Wrapper.remove({ name }); | |
console.log('✓ S3 account okay'); | |
} catch (e) { | |
console.log('Failed to upload test image to S3'); | |
throw e; | |
} | |
} | |
validateS3Bucket(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment