-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
66 lines (56 loc) · 2.32 KB
/
server.js
File metadata and controls
66 lines (56 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const express = require("express");
const AWS = require("aws-sdk");
const multer = require("multer");
const upload = multer();
const app = express();
const path = require("path");
// Cấu hình để phục vụ các tệp tĩnh từ thư mục "public"
app.use(express.static(path.join(__dirname, "public")));
app.use(express.json()); // Dùng để phân tích cú pháp JSON
// Route xử lý đường dẫn "/"
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
// Route xử lý "/test"
app.post("/test", upload.single("file"), async(req, res) => {
const { endpoint, accessKey, secretKey, region, bucket } = req.body;
const file = req.file;
// Kiểm tra tham số đầu vào
if (!endpoint || !accessKey || !secretKey || !region || !bucket || !file) {
return res.status(400).send("Missing required parameters.");
}
const s3 = new AWS.S3({
endpoint: new AWS.Endpoint(endpoint), // Cần đảm bảo endpoint đúng định dạng
accessKeyId: accessKey,
secretAccessKey: secretKey,
region,
s3ForcePathStyle: true, // Thêm tùy chọn này nếu sử dụng dịch vụ S3 không phải AWS
});
try {
// Upload test
const uploadStart = Date.now();
await s3
.upload({
Bucket: bucket,
Key: file.originalname,
Body: file.buffer,
})
.promise();
const uploadEnd = Date.now();
// Download test
const downloadStart = Date.now();
await s3.getObject({ Bucket: bucket, Key: file.originalname }).promise();
const downloadEnd = Date.now();
const uploadTime = (uploadEnd - uploadStart) / 1000; // Thời gian upload (s)
const downloadTime = (downloadEnd - downloadStart) / 1000; // Thời gian download (s)
const fileSizeMB = file.size / (1024 * 1024); // Kích thước file (MB)
res.send({
uploadSpeed: (fileSizeMB / uploadTime).toFixed(2) + " MB/s",
downloadSpeed: (fileSizeMB / downloadTime).toFixed(2) + " MB/s",
});
} catch (err) {
res.status(500).send("Error testing S3 speed: " + err.message);
}
});
// Khởi chạy server
app.listen(3000, () => console.log("Server is running on http://localhost:3000"));