Last active
March 14, 2018 09:18
-
-
Save zeynepsu/935f92af6b40fc0395dd to your computer and use it in GitHub Desktop.
Algorithmia NodeJS SDK
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
/* | |
* Algorithmia Lambda Sample Code | |
*/ | |
var AWS = require('aws-sdk'); | |
var apiKey = 'YOUR_API_KEY_HERE' | |
exports.handler = function (event, context) { | |
// Specify the target algorithm | |
var algo = "algo://besirkurtulmus/quadtree_art/0.1.x"; | |
// Get the image from the event triggered by S3 | |
var s3 = new AWS.S3(); | |
var bucket = event.Records[0].s3.bucket.name; | |
var key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " ")); | |
var params = {Bucket: bucket, Key: key}; | |
var signedUrl = s3.getSignedUrl('getObject', params); | |
// Run the algorithm | |
var client = algorithmia(apiKey); | |
client.algo(algo).pipe(signedUrl).then(function(output) { | |
if(output.error) { | |
// The algorithm returned an error | |
console.log("Error: " + output.error.message); | |
// We call context.succeed to avoid Lambda retries, for more information see: https://forums.aws.amazon.com/thread.jspa?messageID=643046#643046 | |
context.succeed(output.error.message); | |
} else { | |
// Upload the result image to the bucket | |
var outputKey = 'output/'+key.substring(key.lastIndexOf('/') + 1); | |
var params = {Bucket: bucket, Key: outputKey, Body: output.get()}; | |
s3.upload(params, function(err, data) { | |
if (err) { | |
console.log("Error uploading data: ", err); | |
context.fail(err); | |
} else { | |
console.log("Successfully uploaded data to bucket"); | |
context.succeed("Finished processing"); | |
} | |
}); | |
} | |
}); | |
}; | |
/* | |
* Algorithmia NodeJS SDK | |
*/ | |
var Algorithm, AlgorithmiaClient, AlgoResponse, Data, algorithmia, defaultApiAddress, http, https, packageJson, url; | |
https = require('https'); | |
http = require('http'); | |
url = require('url'); | |
defaultApiAddress = 'https://api.algorithmia.com'; | |
AlgorithmiaClient = (function() { | |
function AlgorithmiaClient(key, address) { | |
this.apiAddress = address || process.env.ALGORITHMIA_API || defaultApiAddress; | |
key = key || process.env.ALGORITHMIA_API_KEY; | |
if (key) { | |
if (key.indexOf('Simple ') === 0) { | |
this.apiKey = key; | |
} else { | |
this.apiKey = 'Simple ' + key; | |
} | |
} else { | |
this.apiKey = ''; | |
} | |
} | |
AlgorithmiaClient.prototype.algo = function(path) { | |
return new Algorithm(this, path); | |
}; | |
AlgorithmiaClient.prototype.file = function(path) { | |
return new Data(this, path); | |
}; | |
AlgorithmiaClient.prototype.req = function(path, method, data, cheaders, callback) { | |
var dheader, httpRequest, key, options, protocol, val; | |
dheader = { | |
'Content-Type': 'application/JSON', | |
'Accept': 'application/JSON', | |
'User-Agent': 'algorithmia-lambda/1.0.1 (NodeJS ' + process.version + ')' | |
}; | |
if (this.apiKey) { | |
dheader['Authorization'] = this.apiKey; | |
} | |
for (key in cheaders) { | |
val = cheaders[key]; | |
dheader[key] = val; | |
} | |
options = url.parse(this.apiAddress + path); | |
options.method = method; | |
options.headers = dheader; | |
protocol = options.protocol === 'https:' ? https : http; | |
httpRequest = protocol.request(options, function(res) { | |
var chunks; | |
res.setEncoding('utf8'); | |
chunks = []; | |
res.on('data', function(chunk) { | |
return chunks.push(chunk); | |
}); | |
res.on('end', function() { | |
var body, buff; | |
buff = chunks.join(''); | |
if (dheader['Accept'] === 'application/JSON') { | |
body = JSON.parse(buff); | |
} else { | |
body = buff; | |
} | |
if (callback) { | |
if (res.statusCode < 200 || res.statusCode >= 300) { | |
if (!body) { | |
body = {}; | |
} | |
if (!body.error) { | |
body.error = { | |
message: 'HTTP Response: ' + res.statusCode | |
}; | |
} | |
} | |
callback(body, res.statusCode); | |
} | |
}); | |
return res; | |
}); | |
httpRequest.write(data); | |
httpRequest.end(); | |
}; | |
return AlgorithmiaClient; | |
})(); | |
algorithmia = function(key, address) { | |
return new AlgorithmiaClient(key, address); | |
}; | |
algorithmia.client = function(key, address) { | |
return new AlgorithmiaClient(key, address); | |
}; | |
algorithmia.algo = function(path) { | |
this.defaultClient = this.defaultClient || new AlgorithmiaClient(); | |
return this.defaultClient.algo(path); | |
}; | |
algorithmia.file = function(path) { | |
this.defaultClient = this.defaultClient || new AlgorithmiaClient(); | |
return this.defaultClient.file(path); | |
}; | |
Algorithm = (function() { | |
function Algorithm(client, path) { | |
this.client = client; | |
this.algo_path = path; | |
this.promise = { | |
then: (function(_this) { | |
return function(callback) { | |
return _this.callback = callback; | |
}; | |
})(this) | |
}; | |
} | |
Algorithm.prototype.pipe = function(input) { | |
var contentType, data; | |
data = input; | |
if (Buffer.isBuffer(input)) { | |
contentType = 'application/octet-stream'; | |
} else if (typeof input === 'string') { | |
contentType = 'text/plain'; | |
} else { | |
contentType = 'application/json'; | |
data = JSON.stringify(input); | |
} | |
this.req = this.client.req('/v1/algo/' + this.algo_path, 'POST', data, { | |
'Content-Type': contentType | |
}, (function(_this) { | |
return function(response, status) { | |
return _this.callback(new AlgoResponse(response, status)); | |
}; | |
})(this)); | |
return this.promise; | |
}; | |
Algorithm.prototype.pipeJson = function(input) { | |
if (typeof input !== 'string') { | |
throw "Cannot convert " + (typeof input) + " to string"; | |
} | |
this.req = this.client.req('/v1/algo/' + this.algo_path, 'POST', input, { | |
'Content-Type': 'application/json' | |
}, (function(_this) { | |
return function(response, status) { | |
return _this.callback(new AlgoResponse(response, status)); | |
}; | |
})(this)); | |
return this.promise; | |
}; | |
return Algorithm; | |
})(); | |
AlgoResponse = (function() { | |
function AlgoResponse(response, status) { | |
this.status = status; | |
this.result = response.result; | |
this.error = response.error; | |
this.metadata = response.metadata; | |
} | |
AlgoResponse.prototype.get = function() { | |
if (this.error) { | |
throw "" + this.error.message; | |
} | |
switch (this.metadata.content_type) { | |
case "void": | |
return null; | |
case "text": | |
case "json": | |
return this.result; | |
case "binary": | |
return new Buffer(this.result, 'base64'); | |
default: | |
throw "Unknown result content_type: " + this.metadata.content_type + "."; | |
} | |
}; | |
return AlgoResponse; | |
})(); | |
Data = (function() { | |
function Data(client, path) { | |
this.client = client; | |
if (path.indexOf('data://') !== 0) { | |
throw 'Supplied path is invalid.'; | |
} | |
this.data_path = path.replace(/data\:\/\//, ''); | |
} | |
Data.prototype.putString = function(content, callback) { | |
var headers; | |
headers = { | |
'Content-Type': 'text/plain' | |
}; | |
return this.client.req('/v1/data/' + this.data_path, 'PUT', content, headers, callback); | |
}; | |
Data.prototype.putJson = function(content, callback) { | |
var headers; | |
headers = { | |
'Content-Type': 'application/JSON' | |
}; | |
return this.client.req('/v1/data/' + this.data_path, 'PUT', content, headers, callback); | |
}; | |
Data.prototype.getString = function(callback) { | |
var headers; | |
headers = { | |
'Accept': 'text/plain' | |
}; | |
return this.client.req('/v1/data/' + this.data_path, 'GET', '', headers, callback); | |
}; | |
Data.prototype.getJson = function(callback) { | |
var headers; | |
headers = { | |
'Accept': 'text/plain' | |
}; | |
return this.client.req('/v1/data/' + this.data_path, 'GET', '', headers, callback); | |
}; | |
return Data; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
After testing the lambda function, I am getting the following error
The area below shows the result returned by your function execution. Learn more about returning results from your function.
""failed to download files: 'Please provide a valid image/URL.'""