Last active
September 8, 2023 22:48
-
-
Save jlouros/9abc14239b0d9d8947a3345b99c4ebcb to your computer and use it in GitHub Desktop.
Upload folder to S3 (Node.JS)
This file contains hidden or 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"); // from AWS SDK | |
| const fs = require("fs"); // from node.js | |
| const path = require("path"); // from node.js | |
| // configuration | |
| const config = { | |
| s3BucketName: 'your.s3.bucket.name', | |
| folderPath: '../dist' // path relative script's location | |
| }; | |
| // initialize S3 client | |
| const s3 = new AWS.S3({ signatureVersion: 'v4' }); | |
| // resolve full folder path | |
| const distFolderPath = path.join(__dirname, config.folderPath); | |
| // get of list of files from 'dist' directory | |
| fs.readdir(distFolderPath, (err, files) => { | |
| if(!files || files.length === 0) { | |
| console.log(`provided folder '${distFolderPath}' is empty or does not exist.`); | |
| console.log('Make sure your project was compiled!'); | |
| return; | |
| } | |
| // for each file in the directory | |
| for (const fileName of files) { | |
| // get the full path of the file | |
| const filePath = path.join(distFolderPath, fileName); | |
| // ignore if directory | |
| if (fs.lstatSync(filePath).isDirectory()) { | |
| continue; | |
| } | |
| // read file contents | |
| fs.readFile(filePath, (error, fileContent) => { | |
| // if unable to read file contents, throw exception | |
| if (error) { throw error; } | |
| // upload file to S3 | |
| s3.putObject({ | |
| Bucket: config.s3BucketName, | |
| Key: fileName, | |
| Body: fileContent | |
| }, (res) => { | |
| console.log(`Successfully uploaded '${fileName}'!`); | |
| }); | |
| }); | |
| } | |
| }); |
We've just released a package for that at https://github.com/thousandxyz/s3-lambo, fully tested. You can use the code or the package at your need.
I fixed few minor errors with paths / other things and
published this as a package
https://www.npmjs.com/package/s3-upload-folder/v/latest
the source (MIT)
https://github.com/Prozi/s3-upload-folder/blob/main/index.js
uses standard S3 sdk authentication (need to read about this if you don't know what I mean)
enjoy!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Important notes:
paramsandoptionsare the same as in the AWS documentation so theses functions are very flexible;rootKeyis the root AWS key to use, by default it is the S3 root, e.g. sayingrootKeyispublic/imagesand you want to upload/Users/you/my-project/images, files will be uploaded tos3://bucket/public/images;aws-sdkwill automatically check forAWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEYenvironment variables, it is the safest way to deal with credentials imo;syncmethod (it's Python underneath, Node.js should be faster);application/octet-streamby default and lead to unexpected behaviors;console;const x = { ...params };is the same asObject.assignBUT will not deeply clone objects which could lead to unexpected object mutations, prefer a safe clone function or similar;