-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminioClient.ts
323 lines (276 loc) · 9.04 KB
/
minioClient.ts
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import crypto from 'crypto';
import { readFile, rm } from 'fs/promises';
import * as Minio from 'minio';
import { getConfig } from '../config/config';
import logger from '../logger';
import {
createOrganizationsTable,
createTable,
createTableFromJson,
insertFromS3,
insertFromS3Json,
insertOrganizationIntoTable,
} from './clickhouse';
import { getCsvHeaders, validateJsonFile } from './file-validators';
export interface Bucket {
bucket: string;
region?: string;
}
export class BucketDoesNotExistError extends Error {
constructor(message: string) {
super(message);
}
}
const { endPoint, port, useSSL, bucketRegion, accessKey, secretKey, buckets, prefix, suffix } =
getConfig().minio;
const registeredBuckets: Set<string> = new Set();
// Create a shared Minio client instance
const minioClient = new Minio.Client({
endPoint,
port,
useSSL,
accessKey,
secretKey,
});
interface MinioResponse {
success: boolean;
message: string;
}
interface FileExistsResponse extends MinioResponse {
exists: boolean;
}
/**
* Get the first field of a json object
* @param {any} json - The json object
* @returns {string} - The first field
*/
function getFirstField(json: any) {
let obj: any;
if (Array.isArray(json) && json.length > 0) {
obj = json[0];
} else {
obj = json;
}
const fields = Object.keys(obj);
return fields[0];
}
/**
* Ensures a bucket exists, creates it if it doesn't
* @param {string} bucket - Bucket name
* @param {string} [region] - Bucket region
* @param {boolean} [createBucketIfNotExists] - Whether to create the bucket if it doesn't exist
* @returns {Promise<void>}
*/
export async function ensureBucketExists(
bucket: string,
createBucketIfNotExists = false
): Promise<void> {
try {
const exists = await minioClient.bucketExists(bucket);
if (!exists && createBucketIfNotExists) {
await minioClient.makeBucket(bucket);
logger.info(
`Bucket ${bucket} created`
);
}
await createMinioBucketListeners([bucket]);
if (!exists && !createBucketIfNotExists) {
throw new BucketDoesNotExistError(`Bucket ${bucket} does not exist`);
}
} catch (error) {
logger.error(`Error ensuring bucket ${bucket} exists: ${error}`);
throw error;
}
}
/**
* Checks if a file exists in the specified Minio bucket
* @param {string} fileName - Name of the file to check
* @param {string} bucket - Bucket name
* @param {string} fileType - Expected file type
* @returns {Promise<FileExistsResponse>}
*/
export async function checkFileExists(
fileName: string,
bucket: string,
fileType: string
): Promise<FileExistsResponse> {
try {
const bucketExists = await minioClient.bucketExists(bucket);
if (!bucketExists) {
return {
exists: false,
success: false,
message: `Bucket ${bucket} does not exist`,
};
}
const stats = await minioClient.statObject(bucket, fileName);
const exists = stats.metaData?.['content-type'] === fileType;
return {
exists,
success: true,
message: exists
? `File ${fileName} exists in bucket ${bucket}`
: `File ${fileName} does not exist in bucket ${bucket}`,
};
} catch (err) {
const error = err as Error;
if ((error as any).code === 'NotFound') {
return {
exists: false,
success: true,
message: `File ${fileName} not found in bucket ${bucket}`,
};
}
logger.error('Error checking file existence:', error);
return {
exists: false,
success: false,
message: `Error checking file existence: ${error.message}`,
};
}
}
/**
* Uploads a file to Minio storage
* @param {string} sourceFile - Path to the file to upload
* @param {string} destinationObject - Name for the uploaded object
* @param {string} bucket - Bucket name
* @param {string} fileType - Type of file being uploaded
* @param {Object} [customMetadata={}] - Optional custom metadata
* @returns {Promise<MinioResponse>}
*/
export async function uploadToMinio(
sourceFile: string,
destinationObject: string,
bucket: string,
fileType: string,
customMetadata = {}
): Promise<MinioResponse> {
try {
logger.info(`Uploading file ${sourceFile} to bucket ${bucket}`);
const metaData = {
'Content-Type': fileType,
'X-Upload-Id': crypto.randomUUID(),
...customMetadata,
};
await minioClient.fPutObject(bucket, destinationObject, sourceFile, metaData);
const successMessage = `File ${sourceFile} uploaded as object ${destinationObject} in bucket ${bucket}`;
logger.info(successMessage);
return {
success: true,
message: successMessage,
};
} catch (error) {
const errorMessage = `Error uploading file: ${error instanceof Error ? error.message : String(error)}`;
logger.error(errorMessage);
throw new Error(`Filed to upload file ${sourceFile}`);
}
}
/**
* Uploads a file buffer to Minio storage
* @param {Buffer} fileBuffer - Path to the file to upload
* @param {string} destinationObject - Name for the uploaded object
* @param {string} bucket - Bucket name
* @param {string} fileType - Type of file being uploaded
* @param {Object} [customMetadata={}] - Optional custom metadata
* @returns {Promise<MinioResponse>}
*/
export async function uploadFileBufferToMinio(
fileBuffer: Buffer,
destinationObject: string,
bucket: string,
fileType: string,
customMetadata = {}
): Promise<MinioResponse> {
try {
logger.info(`Uploading file buffer ${customMetadata} to bucket ${bucket}`);
const fileCheck = await checkFileExists(destinationObject, bucket, fileType);
if (fileCheck.exists) {
return {
success: false,
message: fileCheck.message,
};
}
const metaData = {
'Content-Type': fileType,
'X-Upload-Id': crypto.randomUUID(),
...customMetadata,
};
await minioClient.putObject(bucket, destinationObject, fileBuffer, fileBuffer.length, metaData);
const successMessage = `File buffer ${customMetadata} uploaded as object ${destinationObject} in bucket ${bucket}`;
logger.info(successMessage);
return {
success: true,
message: successMessage,
};
} catch (error) {
const errorMessage = `Error uploading file: ${error instanceof Error ? error.message : String(error)}`;
logger.error(errorMessage);
return {
success: false,
message: errorMessage,
};
}
}
export async function createMinioBucketListeners(listOfBuckets: string[]) {
for (const bucket of listOfBuckets) {
if (registeredBuckets.has(bucket)) {
logger.debug(`Bucket ${bucket} already registered`);
continue;
}
const listener = minioClient.listenBucketNotification(bucket, prefix, suffix, [
's3:ObjectCreated:*',
]);
registeredBuckets.add(bucket);
logger.info(`Listening for notifications on bucket ${bucket}`);
listener.on('notification', async (notification) => {
//@ts-ignore
const file = notification.s3.object.key;
//@ts-ignore
const tableName = notification.s3.bucket.name + '_predictions';
logger.info(`File received: ${file} from bucket ${tableName}`);
try {
await minioClient.fGetObject(bucket, file, `tmp/${file}`);
const fileBuffer = await readFile(`tmp/${file}`);
//get the file extension
const extension = file.split('.').pop();
logger.info(`File Downloaded - Type: ${extension}`);
const minioUrl = `http://${endPoint}:${port}/${bucket}/${file}`;
if (extension === 'json' && validateJsonFile(fileBuffer)) {
logger.debug('Now inserting ' + file + 'into clickhouse');
const groupByColumnName = getFirstField(JSON.parse(fileBuffer.toString()));
// Create table from json
await createTableFromJson(minioUrl, { accessKey, secretKey }, tableName, groupByColumnName);
// Insert data into clickhouse
await insertFromS3Json(tableName, minioUrl, {
accessKey,
secretKey,
});
} else if (extension === 'csv' && getCsvHeaders(fileBuffer)) {
//get the first line of the csv file
// const fields = (await readFile(`tmp/${file}`, 'utf8')).split('\n')[0].split(',');
// await createTable(fields, tableName);
// // If running locally and using docker compose, the minio host is 'minio'. This is to allow clickhouse to connect to the minio server
// // Construct the S3-style URL for the file
// // Insert data into clickhouse
// await insertFromS3(tableName, minioUrl, {
// accessKey,
// secretKey,
// });
} else {
logger.warn(`Unknown file type - ${extension}`);
}
await rm(`tmp/${file}`);
logger.debug(`File ${file} deleted from tmp directory`);
} catch (error) {
logger.error(`Error processing file ${file}: ${error}`);
}
});
}
}
export function sanitizeBucketName(name: string) {
return name.toLowerCase()
.replace(/[^a-z0-9-]/g, '')
.replace(/^-+|-+$/g, '')
.replace(/\.{2,}/g, '.')
.substring(0, 63);
}