오윤석

gif download

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