Created
April 6, 2019 22:04
-
-
Save krhoyt/50709e6c7322df57f744ecc3bee475ce to your computer and use it in GitHub Desktop.
IBM Cloud Function to handle Watson classification of image files placed into Cloud Object Storage.
This file contains 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
async function main( params ) { | |
if( params.status === 'added' ) { | |
const authorization = await rp( { | |
url: 'https://iam.ng.bluemix.net/oidc/token', | |
method: 'POST', | |
form: { | |
apikey: params.COS_API_KEY, | |
response_type: 'cloud_iam', | |
grant_type: 'urn:ibm:params:oauth:grant-type:apikey' | |
}, | |
json: true | |
} ); | |
console.log( authorization ); | |
const contents = await rp( { | |
url: `https://${params.endpoint}/${params.bucket}/${params.key}`, | |
method: 'GET', | |
headers: { | |
Authorization: `${authorization.token_type} ${authorization.access_token}`, | |
'ibm-service-instance-id': params.COS_SERVICE_INSTANCE | |
}, | |
encoding: null | |
} ); | |
console.log( contents ); | |
const classification = await rp( { | |
url: 'https://gateway.watsonplatform.net/visual-recognition/api/v3/classify', | |
method: 'POST', | |
auth: { | |
user: params.WATSON_USERNAME, | |
pass: params.WATSON_PASSWORD | |
}, | |
formData: { | |
images_file: { | |
value: contents, | |
options: { | |
filename: params.key | |
} | |
} | |
}, | |
qs: { | |
version: '2018-03-19' | |
}, | |
json: true | |
} ); | |
console.log( classification ); | |
classification.bucket = params.bucket; | |
classification.key = params.key; | |
classification.endpoint = params.endpoint; | |
const doc = await rp( { | |
url: `https://${params.CLOUDANT_ACCOUNT}.cloudant.com/${params.CLOUDANT_DATABASE}`, | |
method: 'POST', | |
auth: { | |
user: params.CLOUDANT_USERNAME, | |
pass: params.CLOUDANT_PASSWORD | |
}, | |
json: true, | |
body: classification | |
} ); | |
console.log( doc ); | |
return doc; | |
} | |
} |
This file contains 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 rp = require( 'request-promise' ); | |
function main( params ) { | |
if( params.status === 'added' ) { | |
return rp( { | |
url: 'https://iam.ng.bluemix.net/oidc/token', | |
method: 'POST', | |
form: { | |
apikey: params.COS_API_KEY, | |
response_type: 'cloud_iam', | |
grant_type: 'urn:ibm:params:oauth:grant-type:apikey' | |
}, | |
json: true | |
} ) | |
.then( ( authorization ) => { | |
return rp( { | |
url: `https://${params.endpoint}/${params.bucket}/${params.key}`, | |
method: 'GET', | |
headers: { | |
Authorization: `${authorization.token_type} ${authorization.access_token}`, | |
'ibm-service-instance-id': params.COS_SERVICE_INSTANCE | |
}, | |
encoding: null | |
} ); | |
} ) | |
.then( ( contents ) => { | |
return rp( { | |
url: 'https://gateway.watsonplatform.net/visual-recognition/api/v3/classify', | |
method: 'POST', | |
auth: { | |
user: params.WATSON_USERNAME, | |
pass: params.WATSON_PASSWORD | |
}, | |
formData: { | |
images_file: { | |
value: contents, | |
options: { | |
filename: params.key | |
} | |
} | |
}, | |
qs: { | |
version: '2018-03-19' | |
}, | |
json: true | |
} ); | |
} ) | |
.then( ( classification ) => { | |
classification.bucket = params.bucket; | |
classification.key = params.key; | |
classification.endpoint = params.endpoint; | |
return rp( { | |
url: `https://${params.CLOUDANT_ACCOUNT}.cloudant.com/${params.CLOUDANT_DATABASE}`, | |
method: 'POST', | |
auth: { | |
user: params.CLOUDANT_USERNAME, | |
pass: params.CLOUDANT_PASSWORD | |
}, | |
json: true, | |
body: classification | |
} ) | |
} ) | |
.then( ( doc ) => { | |
console.log( doc ); | |
return doc; | |
} ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment