-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal2MinioBulkUpload.js
More file actions
58 lines (48 loc) · 1.91 KB
/
local2MinioBulkUpload.js
File metadata and controls
58 lines (48 loc) · 1.91 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
var Minio = require("minio");
var fs = require('fs');
var path = require('path');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var minioClient = new Minio.Client({
endPoint: 'IP Address',
port: 9000,
useSSL: true,
accessKey: '',
secretKey: ''
});
const folderPath = "/home/path";
fs.readdir(folderPath, (err, files) => {
if (err) {
console.error("Error reading directory:", err);
process.exit(1); // Exit with error code 1
}
files.forEach(file => {
const localFilePath = path.join(folderPath, file);
// Create a readable stream from the local file
const fileStream = fs.createReadStream(localFilePath);
// Get file stats to determine file size for progress tracking
const stats = fs.statSync(localFilePath);
const totalBytes = stats.size;
let uploadedBytes = 0;
// Upload the file to MinIO
minioClient.putObject("YourBucketName", file, fileStream, totalBytes, function (error, etag) {
if (error) {
console.error(`Error occurred while uploading ${file}:`, error);
process.exit(1); // Exit with error code 1
}
console.log(`File ${file} uploaded successfully. ETag: ${etag}`);
});
// Track upload progress
fileStream.on('data', function (chunk) {
uploadedBytes += chunk.length;
const progress = Math.round((uploadedBytes / totalBytes) * 100);
process.stdout.clearLine(); // Clear the previous progress output
process.stdout.cursorTo(0); // Move cursor to beginning of line
process.stdout.write(`Uploading ${file}: ${progress}%`);
});
// Handle errors
fileStream.on('error', function (err) {
console.error(`Error occurred while reading file ${file}:`, err);
process.exit(1); // Exit with error code 1
});
});
});