-
How to upload file to s3 bucket using aws-sdk on node.jsInfra/AWS 2019. 12. 23. 18:11
버킷 생성
IAM 생성
소스코드
// example.js const fs = require("fs"); const AWS = require("aws-sdk"); const s3 = new AWS.S3({ accessKeyId: process.env.ACCESS_KEY_ID, secretAccessKey: process.env.SECRET_ACCESS_KEY }); export const uploadLogFiles = () => { return new Promise((resolve: any, reject: any) => { fs.readdir("./log", function (err: any, filenames: any) { if (err) { console.log("ERROR IN READING LOG FILES FROM /log FOLDER"); console.log(err); return; } console.log(filenames); filenames.forEach(function (filename: string) { fs.readFile("./log/" + filename, `utf-8`, function (err: any, content: any) { if (err) { console.log("ERROR IN LOOPING INDIVIDUAL FILES"); console.log(err); reject(err); } const params = { Bucket: "[BUCKET_NAME]", Key: "log/" + filename, Body: content }; s3.upload(params, function (s3Err: any, data: any) { if (s3Err) { console.log("ERROR IN UPLOADING LOGS TO S3"); console.log(s3Err); reject(s3Err); } console.log(`File uploaded successfully at ${data.Location}`); resolve("LOGS UPLOADED SUCCESSFULLY"); }); }); }); }); }); };
실행
$ node example.js
확인
'Infra > AWS' 카테고리의 다른 글
How to install mysql on Amazon linux 2? (0) 2020.02.05 How to install nodejs on amazon linux2 (0) 2019.12.30 [aws] How to install nodejs on Amazon linux (0) 2019.12.13 [aws] How to change the timezone at Amazon EC2 on Elastic Beanstalk? (0) 2019.12.13 [aws] How to configure elastic beanstalks load balancer & auto scaling on codestar? (0) 2019.12.12