Created
December 27, 2019 17:45
-
-
Save vmmartinezlona/2151830dbab052edf4b2f5420ba8ee78 to your computer and use it in GitHub Desktop.
Script for upload edited image (with watermark) with gm to s3. Took the time of execution.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
AWS.config.update({ | |
region: "***", | |
endpoint: "***", | |
accessKeyId: "***", | |
secretAccessKey: "***" | |
}); | |
module.exports = AWS; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gm = require('gm'); | |
var dir = __dirname + '/imgs' | |
var request = require('request'); | |
const {performance} = require('perf_hooks'); | |
var AWS = require('./aws-config'); | |
var s3 = new AWS.S3({ region:'***', endpoint: '***'}); | |
main(); | |
async function main(){ | |
var t0, t1; | |
var url = '***'; | |
t0 = performance.now(); | |
var height = await getHeight(request(url)); | |
t1 = performance.now(); | |
console.log("Call to getHeight took " + (t1 - t0) + " milliseconds."); | |
t0 = performance.now(); | |
var buf = await createImage(request(url), height); | |
t1 = performance.now(); | |
console.log("Call to createImage took " + (t1 - t0) + " milliseconds."); | |
t0 = performance.now(); | |
await uploadToS3(buf, 'keylol'); | |
t1 = performance.now(); | |
console.log("Call to uploadtoS3 took " + (t1 - t0) + " milliseconds."); | |
} | |
function getHeight(image){ | |
return new Promise(resolve => { | |
gm(image).size((err, size) => resolve(size.height)); | |
}); | |
} | |
function createImage(image, height){ | |
return new Promise(resolve => { | |
var fontSize = height/20; | |
gm(image) | |
.fontSize(fontSize) | |
.stroke("#000", fontSize/23) | |
.fill("#fff") | |
.drawText(20, height-fontSize/3, "La mejor inmobiliaria del mundo mundial") | |
.toBuffer((err, buffer) => resolve(buffer)); | |
}); | |
} | |
function uploadToS3(data, key){ | |
var data = { | |
Bucket: "***", | |
Key: "my-image.jpg", | |
Body: data, | |
ContentType: 'image/jpeg', | |
} | |
s3.putObject(data, function(err, res) { | |
if (err) { | |
// Failed saving to S3 | |
console.log('Failed saving to S3', err); | |
}else{ | |
console.log("done"); | |
} | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment