I am trying to replace the parent folder of a path with a name of my choice.
exports.uploadDirectory = async function(dir, customer) {
var fullPathArray = [];
var params = [];
// Read all folders, subfolder and files and add them to an array
fs.readdirSync(dir).forEach(file => {
let fullPath = path.join(dir, file);
if (fs.lstatSync(fullPath).isDirectory()) {
exports.uploadDirectory(fullPath);
} else {
fullPathArray.push(fullPath);
}
});
// Loop through each file found and add it to the object params array
fullPathArray.forEach(function(value){
let init_value = value.split(/\/(.+)/)[1]; // Removing old parent folder
console.log(customer + '/' + init_value); // Thought this may work but returns the below
params.push({ Bucket: config.S3.bucket, Key: init_value, Body: fs.createReadStream(value) });
});
// More Stuff here
};
Console Log prints
undefined/New Folder 2/test22.js
myCustomer/index.html
myCustomer/test.js
As you can see the first item which has a sub directory returns undefined. But the files in the parent directory work fine.
Expected
myCustomer/New Folder 2/test22.js
myCustomer/index.html
myCustomer/test.js
EDIT
value is;
New Folder/New Folder 2/test22.js
New Folder/index.html
New Folder/test.js
init_value is;
New Folder 2/test22.js
index.html
test.js
Call
exports.handler = async (event) => {
await content.uploadDirectory("./New Folder", "myCustomer");
};