-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminio2LocalBucketDownload.js
More file actions
100 lines (85 loc) · 3.14 KB
/
minio2LocalBucketDownload.js
File metadata and controls
100 lines (85 loc) · 3.14 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var Minio = require("minio");
var fs = require('fs');
var path = require('path');
// Create MinIO client instance
var minioClient = new Minio.Client({
endPoint: 'minio.iiit.ac.in',
port: 9000,
useSSL: true,
accessKey: '',
secretKey: ''
});
const bucketName = "microlab/IGIB-COVID/MicroLabsGenomeSequences_02.2024_V29.tar.gz";
const localDownloadPath = "/data/codeDumpDfs/farhin";
// Ensure the local download directory exists
if (!fs.existsSync(localDownloadPath)) {
fs.mkdirSync(localDownloadPath, { recursive: true });
}
// Function to download an object and show progress
async function downloadObject(objectName) {
return new Promise((resolve, reject) => {
minioClient.statObject(bucketName, objectName, function (err, stat) {
if (err) {
return reject("Unable to retrieve file size: " + err);
}
const totalBytes = stat.size;
let downloadedBytes = 0;
const filePath = path.join(localDownloadPath, objectName);
// Ensure the directory structure exists
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
minioClient.getObject(bucketName, objectName, function (error, stream) {
if (error) {
return reject(error);
}
const fileStream = fs.createWriteStream(filePath);
stream.pipe(fileStream);
stream.on('data', function (chunk) {
downloadedBytes += chunk.length;
const progress = Math.round((downloadedBytes / totalBytes) * 100);
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`Downloading ${objectName}: ${progress}%`);
});
stream.on('end', function () {
console.log(`\nFile ${objectName} downloaded to ${filePath}`);
resolve();
});
stream.on('error', function (err) {
reject(err);
});
});
});
});
}
// Function to download all objects in the bucket
async function downloadAllObjects() {
const objectsList = [];
const stream = minioClient.listObjects(bucketName, '', true);
return new Promise((resolve, reject) => {
stream.on('data', obj => objectsList.push(obj.name));
stream.on('error', reject);
stream.on('end', async () => {
try {
for (const objectName of objectsList) {
console.log(`Starting download for ${objectName}`);
await downloadObject(objectName);
}
resolve();
} catch (err) {
reject(err);
}
});
});
}
// Start the download process
downloadAllObjects()
.then(() => {
console.log('All files downloaded successfully!');
})
.catch(err => {
console.error('Error downloading files:', err);
});