first commit
This commit is contained in:
commit
c809bb3893
40 files changed
+3836
No files matched your search
@@ -0,0 +1,60 @@
|
||||
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-server';
|
||||
|
||||
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<Buffer> {
|
||||
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<void> {
|
||||
const metaData = {
|
||||
'Content-Type': contentType,
|
||||
'X-Amz-Meta-serviceName': 'image-derivaties-generation',
|
||||
};
|
||||
|
||||
await this.client.putObject(
|
||||
this.bucketName,
|
||||
s3ObjectKey,
|
||||
buffer,
|
||||
buffer.length,
|
||||
metaData
|
||||
);
|
||||
}
|
||||
|
||||
async fileExists(s3ObjectKey: string): Promise<boolean> {
|
||||
try {
|
||||
await this.client.statObject(this.bucketName, s3ObjectKey);
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user