Commit 918a3b8d authored by Caleb Weeks's avatar Caleb Weeks

Merge branch 'feature/switch-to-aws-sdk' into 'master'

Feature/switch to aws sdk

See merge request !1
parents 91e5b758 e356ac33
var _=require("lodash");
var s3=require("s3");
var aws=require("aws-sdk");
var _url=require("url");
var fs = require("fs");
var confPath = "siteConfig.environment.imageOptimizer.s3-backing";
......@@ -11,24 +11,19 @@ module.exports=function s3BackedRenditions(config, $){
var s3conf = _.get($, confPath);
var timeOfLastEmailError = new Date(1971,4,15,11,30);
if(s3conf.disable){
return null;
}
if(!s3conf){
throw new Error(`'${pluginId}' plugin installed, but missing needed config at: ${confPath}`)
}
var s3client = s3.createClient({
multipartUploadThreshold: 20971520, // this is the default (20 MB)
multipartUploadSize: 15728640, // this is the default (15 MB)
s3Options: {
accessKeyId: s3conf.accessKeyId,
secretAccessKey: s3conf.secretAccessKey,
region: s3conf.region || ""
},
});
if (s3conf.disable) {
return null;
}
var s3client = new aws.S3({
accessKeyId: s3conf.accessKeyId,
secretAccessKey: s3conf.secretAccessKey,
region: s3conf.region || "us-east-1"
});
/*******************************************************************************/
return {
......@@ -36,72 +31,67 @@ module.exports=function s3BackedRenditions(config, $){
/////////////////////////////////////////////////////////////////////////////
checkForRendition: (params, cb)=>{
var lastModifiedTime;
var downloader = s3client.downloadFile({
localFile:params.renditionPathOnDisk,
s3Params:{
Bucket: s3conf.bucket,
Key: params.renditionPath
}
});
////////////////////////////////////////////////////////////////
downloader.on("httpHeaders", (statusCode, headers)=>{
lastModifiedTime = Date.parse(headers["last-modified"])
});
////////////////////////////////////////////////////////////////
downloader.on("end", ()=>{
var localFile = fs.createWriteStream(params.renditionPathOnDisk);
var downloader = s3client.getObject({
Bucket: s3conf.bucket,
Key: params.renditionPath
}).on("httpHeaders", (statusCode, headers)=>{
lastModifiedTime = Date.parse(headers["last-modified"])
}).on("httpData", function(chunk) {
localFile.write(chunk);
}).on("httpDone", function(response) {
localFile.end();
if(lastModifiedTime){
fs.utimes(params.renditionPathOnDisk, lastModifiedTime, lastModifiedTime, ()=>{
log.info("Got s3 rendition", params.renditionPath);
cb(null, lastModifiedTime);
});
});
}
})
////////////////////////////////////////////////////////////////
downloader.on("error", function(err){
}).on("error", function(err){
var msg;
if(err && err.message){
if(err.message.match(/403/)){
msg = `Error downloading rendition from S3 bucket ${s3conf.bucket}. Please check config at: ${confPath}`;
emailErrorOccasionally(params.emailError, msg);
log.error(msg, err.message);
log.error(msg, err);
}else{
msg = `Error downloading rendition from S3 bucket. ${err.message}`;
log.error(msg, err);
}
}else{
msg=`Unknown S3 download error.`;
log.error(msg, err);
}
localFile.end();
cb(msg);
})
}).send();
},
/////////////////////////////////////////////////////////////////////////////
uploadRendition: (params)=>{
var uploader = s3client.uploadFile({
localFile:params.renditionPathOnDisk,
s3Params:{
Bucket: s3conf.bucket,
Key: params.renditionPath
}
var localFile = fs.createReadStream(params.renditionPathOnDisk);
localFile.on('error', function(err) {
var msg = `Unable to access ${params.renditionPathOnDisk}`;
log.error(msg, err);
emailErrorOccasionally(params.emailError, msg);
});
var uploader = s3client.upload({
Body: localFile,
Bucket: s3conf.bucket,
Key: params.renditionPath
});
uploader.on("error", function(err){
var msg = `Error uploading rendition to S3 bucket ${s3conf.bucket}. Please check config at: ${confPath}`;
log.error(msg);
log.error(msg, err);
emailErrorOccasionally(params.emailError, msg);
})
});
uploader.send();
}
}
......
......@@ -9,7 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.10",
"s3": "^4.4.0"
"aws-sdk": "^2.268.1",
"lodash": "^4.17.10"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment