Skip to content

Commit

Permalink
Add S3Client, upload image to S3
Browse files Browse the repository at this point in the history
  • Loading branch information
NguyenAnhTuan1912 committed Aug 20, 2024
1 parent d8c7e7c commit d51ec84
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 29 deletions.
4 changes: 2 additions & 2 deletions backend/nodejs/nodemon.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ignore": ["**/*.test.js", "**/*.spec.js", "node_modules"],
"watch": ["."],
"exec": "npm start",
"exec": "npm run dev",
"ext": "js"
}
}
5 changes: 4 additions & 1 deletion backend/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon",
"dev": "node index.js",
"start": "pm2 start index.js"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.633.0",
"bcrypt": "^5.1.1",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"excel4node": "^1.8.2",
"express": "^4.18.2",
"mime-types": "^2.1.35",
"multer": "^1.4.5-lts.1"
},
"devDependencies": {
"@types/mime-types": "^2.1.4",
"nodemon": "^3.0.1"
},
"author": "Nguyen Anh Tuan",
Expand Down
1 change: 1 addition & 0 deletions backend/nodejs/scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export NVM_DIR="$HOME/.nvm"
cd /home/backend/nodejs

export PYTHON_PATH="/home/backend/python/venv/bin/python"
export BUCKET_NAME="your_bucket_name";

# Start NodeJS Server
npm start
Expand Down
65 changes: 65 additions & 0 deletions backend/nodejs/src/classes/AWS_S3Client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import fs from "fs";
import { lookup } from "mime-types";
import {
S3Client,
PutObjectCommand,
ListObjectsV2Command,
} from "@aws-sdk/client-s3";

export class AWS_S3Client {
constructor() {
this._client = new S3Client();
}

async upload(bucketName, name, objectPath) {
if (!bucketName) {
console.error("No bucket name provided in environment variables.");
return null;
}

const params = {
Bucket: bucketName,
Body: fs.createReadStream(objectPath),
Key: name,
ContentType: lookup(objectPath) || "text/plain",
};

try {
// Send the PutObjectCommand to S3
const data = await this._client.send(new PutObjectCommand(params));

return { result: data };
} catch (err) {
// Log the detailed error for troubleshooting
console.error("Error put objects:", JSON.stringify(err, null, 2));
return Promise.reject(null);
}
}

async list(bucketName) {
if (!bucketName) {
console.error("No bucket name provided in environment variables.");
return null;
}

const params = {
Bucket: bucketName,
};

try {
// Send the ListObjectsV2Command to S3
const data = await this._client.send(new ListObjectsV2Command(params));

// Extract object keys if Contents exists
const objectKeys = data.Contents
? data.Contents.map((obj) => obj.Key)
: [];

return { objectKeys: objectKeys };
} catch (err) {
// Log the detailed error for troubleshooting
console.error("Error listing objects:", JSON.stringify(err, null, 2));
return Promise.reject(null);
}
}
}
69 changes: 43 additions & 26 deletions backend/nodejs/src/handlers/datatableImageToExcel.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import fs from 'fs';
import excel from 'excel4node';
import path from 'path';
import fs from "fs";
import excel from "excel4node";
import path from "path";

// Import from classes
import { PyProcess } from '../classes/PyProcess.js';
import { AWS_S3Client } from "../classes/AWS_S3Client.js";
import { PyProcess } from "../classes/PyProcess.js";

import { createHandler } from '../templates/handler/index.js';
import { createHandler } from "../templates/handler/index.js";

const tempFolderPath = path.resolve('../', 'temp');
const tempFolderPath = path.resolve("../", "temp");

export const DatatableImageToExcelHandler = createHandler(
"/datatable_image_to_excel",
function({ Utils }) {
function ({ Utils }) {
const pp = new PyProcess();
const s3Client = new AWS_S3Client();
const workbook = new excel.Workbook();
return async function(req, res) {
const bucketName = process.env.BUCKET_NAME;

return async function (req, res) {
try {
let files = req.files;
let image = files["image"][0];
let fileName = image.filename.split(".")[0];
let dataSheet = workbook.addWorksheet("Data");
let excelFileFullPath = path.resolve(tempFolderPath, fileName + ".xlsx");
let excelFileFullPath = path.resolve(
tempFolderPath,
fileName + ".xlsx"
);
let currentRow = 1;
let currentCol = 1;

Expand All @@ -33,8 +40,8 @@ export const DatatableImageToExcelHandler = createHandler(
);

// Thêm dữ liệu của từng ô vào trong worksheet.
for(let row of data.data) {
for(let cell of row) {
for (let row of data.data) {
for (let cell of row) {
dataSheet.cell(currentCol, currentRow).string(cell);
currentRow += 1;
}
Expand All @@ -43,27 +50,37 @@ export const DatatableImageToExcelHandler = createHandler(
}

// Viết vào file.
workbook.write(excelFileFullPath, function(err) {
if(err) {
workbook.write(excelFileFullPath, function (err) {
if (err) {
console.log(err);
return;
}

res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader("Content-Disposition", "attachment; filename=" + fileName);
res.setHeader(
"Content-Type",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
);
res.setHeader(
"Content-Disposition",
"attachment; filename=" + fileName
);

return res.download(excelFileFullPath, function(err) {
if(err) throw err;
return res.download(excelFileFullPath, function (err) {
if (err) throw err;

// Xóa ảnh và file.
fs.unlink(image.path, function(err) {
if(err) throw err;
console.log("Image in ", image.path, " was deleted.");
// Upload ảnh lên S3
s3Client.upload(bucketName, fileName, image.path).then(() => {
console.log("Image in", image.path, "is uploaded to S3");
// Tải lên rồi thì xóa ảnh và file.
fs.unlink(image.path, function (err) {
if (err) throw err;
console.log("Image in", image.path, " was deleted.");
});
});

fs.unlink(excelFileFullPath, function(err) {
if(err) throw err;
console.log("File in ", excelFileFullPath, " was deleted.");
fs.unlink(excelFileFullPath, function (err) {
if (err) throw err;
console.log("File in", excelFileFullPath, " was deleted.");
});
});
});
Expand All @@ -75,6 +92,6 @@ export const DatatableImageToExcelHandler = createHandler(
Utils.RM.getResponseMessage(true, undefined, error.message)
);
}
}
};
}
);
);

0 comments on commit d51ec84

Please sign in to comment.