This package is an abstraction of the AWS S3 actions throught AWS SDK and S3-URL-Exists package.
The actions available are:
-
Object:
-
Bucket:
npm install @btime/aws-s3 --save
When requiring aws-s3
package, you must have your aws s3 credentials
to use as parameter like example below:
const options = {
S3_REGION: 's3-region-here',
S3_KEY_SECRET: 's3-key-secret-here',
S3_KEY_ID: 's3-key-id-here',
S3_BUCKET: 's3-bucket-here'
}
const S3 = require('@btime/aws-s3')(options)
Note: this is an example, we recomend you get options using dotenv
to set env variables, an example can be find in our Testing section.
Aws-s3 package returns an AWS.S3()
instance along with upload, select, deleteObject
functions that are Promisses, you can freely use AWS.S3 instance like you want, but our overwriten functions should be used like:
S3.upload(payload)
.then(response => {
/* HANDLE RESPONSE */
})
.catch(err => {
/* HANDLE ERROR */
})
Payload example:
const payload = {
key: 'optionalKey',
file: 'requiredBuffer',
fileExtension: 'optionalExetension',
contentType: 'optionalMimeType'
}
Response example:
{
ETag: '"123x321XX"',
Location: 'https://s3-sa-east-1.amazonaws.com/bucket-test/keyExample',
Key: 'keyExample',
Bucket: 'bucket-test'
}
const payload = defineSelectPayload(params)
S3.select(payload)
.then(response => {
/* HANDLE RESPONSE */
})
.catch(err => {
/* HANDLE ERROR */
})
Payload example:
const payload = {
key: 'requiredKey'
}
Response example:
{ status: true, url: 'https://s3-sa-east-1.amazonaws.com/bucket-test/keyExample' }
const payload = defineDeleteObjectPayload(params)
S3.deleteObject(payload)
.then(response => {
/* HANDLE RESPONSE */
})
.catch(err => {
/* HANDLE ERROR */
})
Payload example:
const payload = {
key: 'requiredKey'
}
Response example:
{ Key: 'keyExample' }
S3.createBucket(payload)
.then(response => {
/* HANDLE RESPONSE */
})
.catch(err => {
/* HANDLE ERROR */
})
Payload example:
const payload = {
name: 'bucket-name-here'
}
Response example:
{
Location: 'bucket-name-here.s3.amazonaws.com/'
}
S3.deleteBucket(payload)
.then(response => {
/* HANDLE RESPONSE */
})
.catch(err => {
/* HANDLE ERROR */
})
Payload example:
const payload = {
name: 'bucket-name-here'
}
Response example:
{}
Tests are coverage by Mocha and Chai, also, all inputs are validate by Joi.
- Creating environment file
cp .env.dist .env
- Edit environment file with your s3 credentials
- Running with npm
npm test
Optional: Running test with coverage report using Istanbul
:
npm run coverage