2019-06-28 20:09:40 +02:00
|
|
|
/*global require,module,process*/
|
|
|
|
|
2020-08-26 04:54:58 +02:00
|
|
|
const AWS = require('aws-sdk');
|
|
|
|
const winston = require('winston');
|
2019-06-28 20:09:40 +02:00
|
|
|
|
2020-08-26 04:54:58 +02:00
|
|
|
const AmazonS3DocumentStore = function(options) {
|
2019-06-28 20:09:40 +02:00
|
|
|
this.expire = options.expire;
|
|
|
|
this.bucket = options.bucket;
|
|
|
|
this.client = new AWS.S3({region: options.region});
|
|
|
|
};
|
|
|
|
|
|
|
|
AmazonS3DocumentStore.prototype.get = function(key, callback, skipExpire) {
|
2020-08-26 04:54:58 +02:00
|
|
|
const _this = this;
|
2019-06-28 20:09:40 +02:00
|
|
|
|
2020-08-26 04:54:58 +02:00
|
|
|
const req = {
|
2019-06-28 20:09:40 +02:00
|
|
|
Bucket: _this.bucket,
|
|
|
|
Key: key
|
|
|
|
};
|
|
|
|
|
|
|
|
_this.client.getObject(req, function(err, data) {
|
|
|
|
if(err) {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback(data.Body.toString('utf-8'));
|
|
|
|
if (_this.expire && !skipExpire) {
|
|
|
|
winston.warn('amazon s3 store cannot set expirations on keys');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
AmazonS3DocumentStore.prototype.set = function(key, data, callback, skipExpire) {
|
2020-08-26 04:54:58 +02:00
|
|
|
const _this = this;
|
2019-06-28 20:09:40 +02:00
|
|
|
|
2020-08-26 04:54:58 +02:00
|
|
|
const req = {
|
2019-06-28 20:09:40 +02:00
|
|
|
Bucket: _this.bucket,
|
|
|
|
Key: key,
|
|
|
|
Body: data,
|
|
|
|
ContentType: 'text/plain'
|
|
|
|
};
|
|
|
|
|
|
|
|
_this.client.putObject(req, function(err, data) {
|
|
|
|
if (err) {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback(true);
|
|
|
|
if (_this.expire && !skipExpire) {
|
|
|
|
winston.warn('amazon s3 store cannot set expirations on keys');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AmazonS3DocumentStore;
|