-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
61 lines (52 loc) · 1.72 KB
/
main.js
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
const Express = require("express");
const Multer = require("multer");
const Minio = require("minio");
const BodyParser = require("body-parser");
const app = Express();
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
const BucketName = 'myupload';
app.use(BodyParser.json({limit: "4mb"}));
const minioClient = new Minio.Client({
endPoint: 'minio.serverapi.host',
port: 9001,
useSSL: false,
accessKey: 'minio',
secretKey: 'minio123'
});
app.post("/upload", Multer({storage: Multer.memoryStorage()}).single("file"), function(request, response) {
minioClient.putObject(BucketName, request.file.originalname, request.file.buffer, function(error, etag) {
if(error) {
return console.log(error);
}
response.send(request.file);
});
});
app.post("/uploads", Multer({dest: "./uploads/"}).single("file"), function(request, response) {
minioClient.fPutObject(BucketName, request.file.originalname, request.file.path, function(error, etag) {
if(error) {
return console.log(error);
}
response.send(request.file);
});
});
app.get("/download", function(request, response) {
minioClient.getObject(BucketName, request.query.name, function(error, stream) {
if(error) {
return response.status(500).send(error);
}
stream.pipe(response);
});
});
minioClient.bucketExists(BucketName, function(error) {
if(error) {
return console.log(error);
}
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
})
const server = app.listen(PORT,HOST, function() {
console.log("Ready Upload to MinIO in %s on port %s...", BucketName,server.address().port);
});
});