Created
June 12, 2017 02:17
-
-
Save chj1768/c19c8b6835d577fe76fd267efd4045b8 to your computer and use it in GitHub Desktop.
aws s3 에 파일이 업로드될 경우 트리거되는 람다로 설정하여 외부 sms api 를 통해 업로드한 파일을 mms 메시지로 전송 연동 로직
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
'use strict'; | |
const https = require('https'); | |
const FileAPI = require('file-api'); | |
const File = FileAPI.File; | |
let fs = require('fs'); | |
let async = require('async'); | |
let aws = require('aws-sdk'); | |
let s3 = new aws.S3( { apiVersion: '2006-03-01' } ); | |
exports.handler = ( event, context, callback ) => { | |
let fileName; | |
async.waterfall( | |
[ | |
( callback ) => { | |
authorize( callback ); | |
}, | |
( result, callback ) => { | |
const bucket = event.Records[ 0 ].s3.bucket.name; | |
const key = decodeURIComponent( event.Records[ 0 ].s3.object.key.replace( /\+/g, ' ' ) ); | |
const params = { | |
Bucket: bucket, | |
Key: key | |
}; | |
s3.getObject( params, (er, re) => { | |
let name = key.split('/'); | |
fileName = name[ name.length - 1 ]; | |
console.log(name, fileName); | |
let file = new File( { | |
name: fileName, // required | |
buffer: re.Body } ); | |
uploadFile( result, file, callback ); | |
}); | |
}, | |
( result, callback ) => { | |
const params = result.split( '/' ); | |
const token = params[ 0 ]; | |
const fileKey = params[ 1 ]; | |
sendMessage( token, fileKey, fileName, callback ); //fileName : phoneNum + _ + currentTime, no span | |
} | |
], | |
( er ) => { | |
if ( er ) { | |
callback( er ); | |
} | |
else { | |
callback( 'success' ); | |
} | |
} | |
); | |
} | |
const authorize = ( callback ) => { | |
let options = { | |
hostname: 'auth.supersms.co', | |
port : 7000, | |
path : '/auth/v3/token', | |
method: 'POST', | |
headers : { 'Accept' : 'application/json', 'X-IB-Client-Id' : 'id', 'X-IB-Client-Passwd' : 'password' } | |
}; | |
let response, token; | |
let req = https.request( options, ( res ) => { | |
res.setEncoding('utf8'); | |
res.on('data', (d) => { | |
response = JSON.parse( d ); | |
token = response.schema +" "+ response.accessToken; | |
}).on('error', ( e ) => { | |
throw Error( 'authorize error', e ); | |
}).on('end', () => { | |
callback( null, token ); | |
}); | |
}); | |
req.end(); | |
}; | |
const uploadFile = ( token, file, callback ) => { | |
let boundary = (new Date()).getTime(); | |
let options = { | |
hostname: 'file.supersms.co', | |
port : 7010, | |
path : '/sms/v3/file', | |
method: 'POST', | |
headers : { 'Authorization' : token, 'Content-Type' : 'multipart/form-data; boundary=' + boundary, 'Accept' : 'application/json' }, | |
}; | |
let body = []; | |
body.push( | |
'--' + boundary, | |
'Content-Disposition: form-data; name="testtest"; filename="' + | |
file.name + '"', | |
'Content-Transfer-Encoding: base64', | |
'Content-Type: ' + file.type, | |
'', | |
file.buffer.toString('base64') | |
); | |
body.push('--' + boundary + '--', ''); | |
const bodyString = body.join('\r\n'); | |
let fileKey; | |
let req = https.request(options, (res) => { | |
res.setEncoding('utf8'); | |
res.on('data', (d) => { | |
const data = JSON.parse( d ); | |
fileKey = data.fileKey; | |
}).on('error', (e) => { | |
throw Error( 'uploadFile error', e ); | |
}).on('end', () => { | |
callback( null, token + '/' + fileKey ); | |
}); | |
}); | |
req.write( bodyString ); | |
req.end(); | |
}; | |
const sendMessage = ( token, fileKey, fileName, callback ) => { | |
let options = { | |
hostname: 'sms.supersms.co', | |
port : 7020, | |
path : '/sms/v3/multiple-destinations', | |
method: 'POST', | |
headers : { 'Authorization' : token, 'Content-Type' : 'application/json', 'Accept' : 'application/json' } | |
}; | |
const phoneNumber = fileName.split('_'); | |
const data = { "title":"test","from":"01000000000","text":"estimate","destinations":[ {"to":"+82" + phoneNumber[ 0 ].substr( 1 ) } ], "fileKey" : fileKey, "ttl":"86400" }; | |
let status; | |
console.log(data); | |
let req = https.request(options, (res) => { | |
res.setEncoding('utf8'); | |
res.on('error', (e) => { | |
throw Error( 'sendMessage error', e ); | |
}).on('end', () => { | |
callback( null ); | |
}); | |
}); | |
req.write( JSON.stringify( data ) ); | |
req.end(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment