Showing
1 changed file
with
44 additions
and
0 deletions
lambda/gif-download/index.js
0 → 100644
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 |
-
Please register or login to post a comment