Created
September 14, 2023 11:30
-
-
Save AyemunHossain/abd853c7a7805c81a4cf04db739f2f09 to your computer and use it in GitHub Desktop.
Get all files form a directory and upload them s3
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 directoryPath = __dirname; | |
const fs = require('fs'); | |
const path = require('path'); | |
const AWS = require('aws-sdk'); | |
AWS.config.update({ | |
endpoint: new AWS.Endpoint("**********************"), | |
accessKeyId: "**********************", | |
secretAccessKey: "********************", | |
}); | |
const s3 = new AWS.S3(); | |
const contentTypeMap = { | |
'jpg': 'image/jpeg', | |
'png': 'image/png', | |
'gif': 'image/gif', | |
'html': 'text/html', | |
'txt': 'text/plain', | |
'xml': 'application/xml', | |
'json': 'application/json', | |
'pdf': 'application/pdf', | |
'doc': 'application/msword', | |
'xls': 'application/vnd.ms-excel', | |
'ppt': 'application/vnd.ms-powerpoint', | |
'zip': 'application/zip', | |
'svga': 'image/svg+xml', | |
}; | |
fs.readdir(directoryPath, (err, files) => { | |
if (err) { | |
console.error('Error reading directory:', err); | |
return; | |
} | |
const allFiles = files.filter((file) => path.extname(file).toLowerCase() != '.js') | |
allFiles.forEach((file) => { | |
const filePath = path.join(directoryPath, file); | |
const targetName = file; | |
const fileExtension = file.split('.')[1]; | |
const contentType = contentTypeMap[fileExtension] || 'application/octet-stream'; // Fallback to 'application/octet-stream' if unknown | |
console.log({targetName, fileExtension, contentType}) | |
fs.readFile(filePath, (readErr, data) => { | |
if (readErr) { | |
console.error(`Error reading file ${file}: ${readErr}`); | |
return; | |
} | |
var putParams = { | |
Bucket: "**********", | |
Key: ('*********'+targetName), | |
ACL: 'public-read', | |
ContentType: "*", | |
Body: data | |
}; | |
s3.putObject(putParams, function (err, data) { | |
if (err) { | |
return err; | |
} | |
else { | |
console.log({targetName}); | |
return null; | |
} | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment