Last active
November 22, 2022 12:27
-
-
Save oismaelash/ded8e5881f0c6e6063a4abfd886d5e61 to your computer and use it in GitHub Desktop.
upload text to aws s3 with node.js and aws 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
const AWS = require('aws-sdk') | |
exports.handler = async (event) => { | |
console.log('event', event) | |
const payload = JSON.parse(event.body) | |
const url = await uploadFile(payload.text) | |
console.log('url', url); | |
const response = { | |
statusCode: 200, | |
body: url, | |
}; | |
return response; | |
}; | |
async function uploadFile(fileContent) { | |
const s3 = new AWS.S3({ apiVersion: '2006-03-01', region: process.env.AWS_REGION }); | |
const params = { | |
Bucket: process.env.AWS_S3_BUCKET, | |
Key: process.env.FILENAME, | |
Body: fileContent, | |
ACL: "public-read", | |
}; | |
const data = await s3.upload(params).promise(); | |
return data.Location; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment