asksuite-core
Version:
42 lines (33 loc) • 1.14 kB
JavaScript
const AWS = require('aws-sdk');
const _ = require('lodash');
class AWSS3Caller {
constructor(awsConfig) {
this.otherParams = _.cloneDeep(awsConfig);
}
async getObject(bucket, key, options, retries = 0) {
const defaultOptions = { timeout: 15000, maxRetries: 2 };
const { timeout, maxRetries } = Object.assign(defaultOptions, options);
const s3 = new AWS.S3(this.otherParams);
const getParams = {
Bucket: bucket,
Key: key,
};
const request = s3.getObject(getParams);
setTimeout(request.abort.bind(request), timeout);
return request
.promise()
.then(function(data) {
const body = data.Body.toString('utf-8');
return JSON.parse(body); // Use the encoding necessary
})
.catch(error => {
const isRetryableError =
error.code === 'RequestAbortedError' || error.code === 'TimeoutError';
if (isRetryableError && retries <= maxRetries) {
return this.getObject(bucket, key, options, retries + 1);
}
throw error;
});
}
}
module.exports = AWSS3Caller;