I am writing this code where im using aws sdk to copy files from one place to another dir, here destinationprefix is supposed to handle to destination directory, there the variable has to be put, idk why it is always copying the file in the dir -> main-dir/[object object] even that is not what I want
here is the code
main function
async function copyS3Folder(sourcePrefix, destinationPrefix, continuationToken) {
try {
const listParams = {
Bucket: process.env.S3_BUCKET || "",
Prefix: sourcePrefix,
ContinuationToken: continuationToken
};
// console.log(` ${sourcePrefix}, ${destinationPrefix}`)
const listedObjects = await s3.listObjectsV2(listParams).promise();
if (!listedObjects.Contents || listedObjects.Contents.length === 0) return;
await Promise.all(listedObjects.Contents.map(async (object) => {
if (!object.Key) return;
let decodedKey = decodeURIComponent(object.Key); // Ensure to decode the object key
let destinationKey = decodedKey.replace(sourcePrefix, destinationPrefix);
let copyParams = {
Bucket: process.env.S3_BUCKET || "",
CopySource: `${process.env.S3_BUCKET}/${encodeURIComponent(decodedKey)}`, // Properly encode the object key
Key: destinationKey
};
await s3.copyObject(copyParams).promise();
console.log(`Copied ${decodedKey} to ${destinationKey}`);
}));
if (listedObjects.IsTruncated) {
listParams.ContinuationToken = listedObjects.NextContinuationToken;
await copyS3Folder(sourcePrefix, destinationPrefix, listParams.ContinuationToken);
}
} catch (error) {
console.error('Error copying folder:', error);
}
}
and here is the calling function… where I initiate the function
socket.on("createStartUp", async (data) => {
console.log(data)
await copyS3Folder(`group-source`, `main-dir/${data}`)
console.log("success!")
I tried using ChatGpt and I debugged it to be faulting on the destination prefix where I found that the end directory is hardcoded to [object object ] but I dont know how to fix it!