Created
September 14, 2016 20:08
-
-
Save matylla/5d6f2e6bd9110a9c5556fb0f1dc98e92 to your computer and use it in GitHub Desktop.
kraken-async
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 async = require("async"), | |
Kraken = require("kraken"), | |
fs = require("fs"); | |
var kraken = new Kraken({ | |
api_key: "your-api-key", | |
api_secret: "your-api-secret" | |
}); | |
var yourArray = [ | |
"/this/is/file/one", | |
"/this/is/file/two", | |
"/this/is/file/three" | |
]; | |
async.forEach(yourArray, 10, function (filePath, cb) { | |
// here you process your files with concurrency of 10 at a time | |
var opts = { | |
file: fs.createReadStream(filePath), | |
wait: true | |
}; | |
kraken.upload(opts, function (data) { | |
if (data.success) { | |
console.log('Success. Optimized image URL: %s', data.kraked_url); | |
} else { | |
console.log('Fail. Error message: %s', data.message); | |
} | |
// this is very important. Calling this callback (cb) tells async | |
// that this async function is now complete so it can process another one | |
cb(); | |
}); | |
}, function (err) { | |
if (err) { | |
console.log(err); | |
} | |
// this is a final callback called only once | |
// when all files have been processed. | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment