index.js
1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const aws = require('aws-sdk');
const s3 = new aws.S3();
exports.handler = async (event) => {
if(!event['queryStringParameters'] || !event['queryStringParameters']['id']) {
return {
statusCode: 400
}
}
const id = event['queryStringParameters']['id'];
const data = await download(id);
return {
statusCode: 200,
headers:{
"Content-Type":"image/gif"
},
isBase64Encoded:true,
body: data.Body.toString("base64")
}
};
const download = (id) => {
const bucket = 'gif-generator';
const path = `/gif/${id}.gif`;
const params = {
Bucket: bucket,
Key: path
}
return new Promise((resolve, reject) => {
s3.getObject(params, (err, data) => {
if(err){
console.log("download err");
console.log(err);
reject(err);
}else{
console.log("download success");
console.log(data);
resolve(data);
}
});
});
}