@idiamantino/livepro-utils
Version:
Stuff for LP
87 lines (72 loc) • 2.57 kB
JavaScript
const AWS = require('aws-sdk');
const Utils = require("../library/utils");
let LpUtils = new Utils();
var fs = require('fs');
class s3 {
constructor(region, apiVersion = '2006-03-01', debug = false) {
this.setDebug(debug);
this.region = region;
LpUtils.printDebug("S3 constructor region:", region);
AWS.config.update({region:this.region});
this.s3 = new AWS.S3({apiVersion: apiVersion}, { region: this.region });
}
setDebug(debug = false) {
this.debug = debug;
LpUtils.setDebug(debug);
}
async test(params){
LpUtils.printDebug("S3 function accessible:", params);
LpUtils.printDebug("Region:", this.region);
let result = "";
// this.setProcessClientStatus("cdf2c150-c724-11ea-94ea-5d5d37731bbc","testing: " + params);
let bucket = "lp4dev";
let key = "liveproindexer/test.json";
let content = "{test}";
result = await this.uploadContent(bucket,key,content, "tag1=111,tag2=222");
console.log(result);
result = await this.getContent(bucket,key);
console.log(result);
return (true);
}
async uploadContent(bucket, key, content, tag = "") {
var params = {
Body: content,
Bucket: bucket,
Key: key,
ServerSideEncryption: "AES256"
// Tagging: tag
// ContentType: 'application/json; charset=utf-8',
};
const result = new Promise((res, rej) => {
this.s3.putObject(params, function(err, data) {
if (err) {
console.log(err, err.stack);
return(false);
} else {
LpUtils.printDebug("s3 putObject response",data);
res(true);
}
});
});
return(await result);
}
async getContent(bucket, key) {
var params = {
Bucket: bucket,
Key: key
};
const result = new Promise((res, rej) => {
this.s3.getObject(params, function(err, data) {
if (err) {
console.log(err, err.stack);
rej(false);
} else {
LpUtils.printDebug("s3 getObject response",data);
res(data.Body.toString());
}
});
});
return(await result);
}
}
module.exports = s3;