Last active
March 14, 2018 13:33
Star
You must be signed in to star a gist
images upload using Lambda
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"; | |
var AWS = require("aws-sdk"); | |
var DOC = require("dynamodb-doc"); | |
var s3 = new AWS.S3(); | |
exports.handler = (event, context, callback) => { | |
//imageのデコード | |
let requestImage = Buffer.from(event.body, 'base64') | |
var imageName = getUniqueString(); | |
var imageStatus = putUserImagesOnBucket(requestImage,imageName+".png"); | |
if(imageStatus.status === 200){ | |
console.log("success"); | |
}else{ | |
console.log("error"); | |
} | |
callback(null, (function(){ | |
var obj = { | |
"id":0, | |
"name":"y shinagawa", | |
"genre":"basketball", | |
"genre_cd" : 0, | |
"image" : "test.png", | |
"match_rate" : 80.000001, | |
"description" : "test test test test test test" | |
}; | |
return obj; | |
})()); | |
}; | |
// | |
function putUserImagesOnBucket(image,imageName){ | |
s3.putObject({ | |
Body: image, | |
Bucket: 'my-bucket', | |
ContentType: 'image/png', | |
Key: imageName | |
}).promise().then((result) => { | |
return {"result":result,"status": 200}; | |
}).catch((err) => { | |
return {"result":err,"status": 500}; | |
}); | |
} | |
function getUniqueString(){ | |
//.... | |
return "unique_string"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment