Lambda + api gateway + CF = serve zip
Last active
December 13, 2020 11:28
-
-
Save Bnaya/946c89d4060f1aa5a20204c18934471c to your computer and use it in GitHub Desktop.
Lambda + api gateway + CF = serve zip
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
# package directories | |
node_modules | |
jspm_packages | |
# Serverless directories | |
.serverless |
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 path = require('path'); | |
module.exports.hello = async event => { | |
try { | |
const fs = require('fs'); | |
const fileContents = await new Promise((res, rej) => { | |
fs.readFile(path.resolve(__dirname, 'thiswillbezipcontent.txt.zip'), (err, data) => { | |
if (err) { | |
rej(err) | |
} else { | |
res(data) | |
} | |
}); | |
}); | |
return { | |
statusCode: 200, | |
body: fileContents.toString('base64'), | |
headers: { | |
'Content-Type': 'application/zip' | |
}, | |
isBase64Encoded: true | |
}; | |
} catch (e) { | |
return { | |
statusCode: 200, | |
body: `can't read: ${e.message}`, | |
} | |
} | |
}; |
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
service: serve-zip-file | |
frameworkVersion: '2' | |
provider: | |
name: aws | |
runtime: nodejs12.x | |
apiGateway: | |
binaryMediaTypes: | |
- '*/*' | |
functions: | |
hello: | |
handler: handler.hello | |
events: | |
- http: | |
path: hello | |
method: get |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment