Skip to content

Instantly share code, notes, and snippets.

@hereisfahad
Created October 15, 2023 21:57
Show Gist options
  • Save hereisfahad/3198c83751cebc2e9847b7cd84c2619a to your computer and use it in GitHub Desktop.
Save hereisfahad/3198c83751cebc2e9847b7cd84c2619a to your computer and use it in GitHub Desktop.
Move files from one folder to the other AWS S3
const AWS = require("aws-sdk");
const { MongoClient } = require("mongodb");
const dotenv = require("dotenv");
dotenv.config();
const s3 = new AWS.S3({
region: "us-west-2",
});
const sourceBucket = "my-s3-bucket";
const moveFiles = async () => {
const keysToMove = await getKeysFromCollection();
for (const sourceKey of keysToMove) {
const destinationKey = `templates/${sourceKey}`;
try {
// Check if the destination file already exists
const destinationExists = await s3
.headObject({ Bucket: sourceBucket, Key: destinationKey })
.promise();
if (destinationExists) {
console.log(
`File '${sourceKey}' already exists in the destination folder.`
);
}
} catch (error) {
if (error.code === "Forbidden" || error.code === "NotFound") {
console.log(`File '${sourceKey}' does not exist in the folder.`);
await s3
.copyObject({
Bucket: sourceBucket,
CopySource: `${sourceBucket}/images/${sourceKey}`,
Key: destinationKey,
})
.promise();
// Remove the source file
await s3
.deleteObject({ Bucket: sourceBucket, Key: `images/${sourceKey}` })
.promise();
console.log(
`File '${sourceKey}' moved to '${destinationKey}' successfully.`
);
} else {
console.error("Error:", error);
}
}
}
};
async function getKeysFromCollection() {
const uri = "";
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
try {
await client.connect();
const databaseName = "";
const collectionName = "";
const database = client.db(databaseName);
const collection = database.collection(collectionName);
const documents = await collection.find({}).toArray();
if (documents.length === 0) {
console.log("Collection is empty.");
return;
}
// Extract keys (field names) from the first document
const keys = documents
.map((document) => document.fileUploads?.template)
.filter(Boolean);
console.log("Keys in the collection:", keys);
return keys;
} catch (err) {
console.error("Error:", err);
} finally {
client.close();
}
}
moveFiles();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment