Skip to content

Commit

Permalink
feat: download existing files from s3 (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
aemmadi committed Aug 20, 2020
1 parent e8daa7f commit 9fd0500
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const AWS = require("aws-sdk");
const fs = require("fs");

const { AWS_ACCESS_KEY, AWS_SECRET_KEY, AWS_BUCKET } = require("./aws");

const s3 = new AWS.S3({
accessKeyId: AWS_ACCESS_KEY,
secretAccessKey: AWS_SECRET_KEY,
});

const downloadFiles = (user, repo) => {
const path = `${user}-${repo}`;
const indexFile = "index.html";
const readmeFile = "README.md";

pullFromS3(path, indexFile);
pullFromS3(path, readmeFile);
};

const pullFromS3 = (path, file) => {
const params = {
Bucket: AWS_BUCKET,
Key: `${path}/${file}`,
};

s3.headObject(params)
.on("success", (response) => {
s3.getObject(params, (err, data) => {
if (err) throw err;
writeFile(path, file, data);
});
})
.on("error", (err) => {
return "";
})
.send();
};

const writeFile = (path, file, data) => {
if (!fs.existsSync("./_docs/")) {
fs.mkdirSync("./_docs/");
}

if (!fs.existsSync(`./_docs/${path}/`)) {
fs.mkdirSync(`./_docs/${path}/`);
}

fs.writeFileSync(`./_docs/${path}/${file}`, data.Body.toString());
console.log(`Downloaded ${file}`);
};

module.exports = { downloadFiles };

0 comments on commit 9fd0500

Please sign in to comment.