import * as Minio from 'minio'; import { secrets } from './secret-manager.js'; import { Readable } from 'stream'; export class MinioClient { private client: Minio.Client; private readonly bucketName = 'wiki-image'; constructor() { const endpoint = secrets?.['MINIO_ENDPOINT'] as string; const accessKey = secrets?.['MINIO_ACCESS_KEY'] as string; const secretKey = secrets?.['MINIO_SECRET_KEY'] as string; if (!endpoint || !accessKey || !secretKey) { throw new Error('Missing MinIO credentials in secrets'); } this.client = new Minio.Client({ endPoint: endpoint, port: 443, useSSL: true, accessKey: accessKey, secretKey: secretKey, }); } async downloadFile(s3ObjectKey: string): Promise { const stream = await this.client.getObject(this.bucketName, s3ObjectKey); return new Promise((resolve, reject) => { const chunks: Buffer[] = []; stream.on('data', (chunk: Buffer) => chunks.push(chunk)); stream.on('end', () => resolve(Buffer.concat(chunks))); stream.on('error', reject); }); } async uploadFile(s3ObjectKey: string, buffer: Buffer, contentType: string): Promise { const metaData = { 'Content-Type': contentType, 'X-Amz-Meta-serviceName': 'image-derivatives-generation', }; await this.client.putObject( this.bucketName, s3ObjectKey, buffer, buffer.length, metaData ); } async fileExists(s3ObjectKey: string): Promise { try { await this.client.statObject(this.bucketName, s3ObjectKey); return true; } catch (err) { return false; } } }