Skip to content

Instantly share code, notes, and snippets.

@matylla
Created September 14, 2016 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matylla/5d6f2e6bd9110a9c5556fb0f1dc98e92 to your computer and use it in GitHub Desktop.
Save matylla/5d6f2e6bd9110a9c5556fb0f1dc98e92 to your computer and use it in GitHub Desktop.
kraken-async
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