src/upload-center/providers/minio.provider.ts
Provider for interacting with MinIO storage. Provides methods for uploading, downloading, and managing files in MinIO buckets.
Properties |
|
Methods |
|
constructor(minioConfiguration: ConfigType<>)
|
||||||||
|
Initializes the MinIO client with configuration parameters.
Parameters :
|
| Async ensureBucket | ||||||||
ensureBucket(bucket: string)
|
||||||||
|
Ensures that the specified bucket exists in MinIO. If the bucket does not exist, it creates a new bucket.
Parameters :
Returns :
any
|
| Async generatePresignedUrl | ||||||||||||||||||||
generatePresignedUrl(bucket: string, objectName: string, expires: number)
|
||||||||||||||||||||
|
Generates a presigned URL for uploading an object to a specified bucket.
Parameters :
Returns :
unknown
A presigned URL for uploading the object. |
| getClient |
getClient()
|
|
Returns the MinIO client instance.
Returns :
Client
The MinIO client. |
| Async getObjectUrl | ||||||||||||||||
getObjectUrl(bucket: BufferBucketNames | StreamBucketNames | PublicBucketNames, objectName: string, expires?: number)
|
||||||||||||||||
|
Generates a presigned URL for downloading an object from a specified bucket.
Parameters :
Returns :
unknown
A presigned URL for downloading the object. |
| Public getPublicUrl | ||||||||||||
getPublicUrl(bucket: string, objectName: string)
|
||||||||||||
|
Returns the public URL for an object in a specified bucket.
Parameters :
Returns :
string
The public URL for the object. |
| Async makeAllPublicBucketsPublic |
makeAllPublicBucketsPublic()
|
|
Ensures that all public buckets are created and made public. This method is called during module initialization.
Returns :
any
|
| Async makeBucketPublic | ||||||||
makeBucketPublic(bucket: PublicBucketNames)
|
||||||||
|
Makes a specified bucket public by setting its policy to allow public read access.
Parameters :
Returns :
any
|
| Async objectExists | ||||||||||||
objectExists(bucket: string, objectName: string)
|
||||||||||||
|
Checks if an object exists in a specified bucket.
Parameters :
Returns :
Promise<boolean>
A promise that resolves to true if the object exists, false otherwise. |
| onModuleInit |
onModuleInit()
|
|
Initializes the MinIO provider and makes all public buckets public. This method is called when the module is initialized.
Returns :
void
|
| Async removeObject | ||||||||||||
removeObject(bucket: string, objectName: string)
|
||||||||||||
|
Removes an object from a specified bucket in MinIO.
Parameters :
Returns :
unknown
A promise that resolves when the object is removed. |
| Async statObject | ||||||||||||
statObject(bucket: string, objectName: string)
|
||||||||||||
|
Retrieves the metadata of an object in a specified bucket.
Parameters :
Returns :
unknown
Metadata of the specified object. |
| Async uploadBuffer | ||||||||||||||||||||
uploadBuffer(bucket: string, objectName: string, buffer: Buffer, mimeType: string)
|
||||||||||||||||||||
|
Uploads a buffer to a specified bucket in MinIO.
Parameters :
Returns :
unknown
A promise that resolves when the upload is complete. |
| Async uploadStream | ||||||||||||||||||||||||
uploadStream(bucket: string, objectName: string, input: Buffer | Readable, size: number, mimeType: string)
|
||||||||||||||||||||||||
|
Uploads a stream to a specified bucket in MinIO.
Parameters :
Returns :
unknown
A promise that resolves when the upload is complete. |
| Private Readonly client |
Type : Client
|
|
MinIO client instance for interacting with the MinIO server. |
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
import minioConfig from '../config/minio.config';
import { ConfigType } from '@nestjs/config';
import { Client } from 'minio';
import { Readable } from 'stream';
import {
BufferBucketNames,
PublicBucketNames,
StreamBucketNames,
} from '../enums/bucket-names.enum';
/**
* Provider for interacting with MinIO storage.
* Provides methods for uploading, downloading, and managing files in MinIO buckets.
*/
@Injectable()
export class MinioProvider implements OnModuleInit {
/**
* MinIO client instance for interacting with the MinIO server.
*/
private readonly client: Client;
/**
* Initializes the MinIO provider and makes all public buckets public.
* This method is called when the module is initialized.
*/
onModuleInit() {
this.makeAllPublicBucketsPublic();
}
/**
* Initializes the MinIO client with configuration parameters.
*
* @param minioConfiguration - Configuration for connecting to the MinIO server.
*/
constructor(
@Inject(minioConfig.KEY)
private readonly minioConfiguration: ConfigType<typeof minioConfig>,
) {
this.client = new Client({
endPoint: this.minioConfiguration.endPoint,
port: this.minioConfiguration.port,
useSSL: this.minioConfiguration.useSSL,
accessKey: this.minioConfiguration.accessKey,
secretKey: this.minioConfiguration.secretKey,
});
}
/**
* Ensures that all public buckets are created and made public.
* This method is called during module initialization.
*/
async makeAllPublicBucketsPublic() {
for (const bucket of Object.values(PublicBucketNames)) {
try {
await this.ensureBucket(bucket);
await this.makeBucketPublic(bucket);
} catch (err) {
console.warn(`Cannot make bucket ${bucket} public:`, err.message);
}
}
}
/**
* Returns the MinIO client instance.
*
* @returns The MinIO client.
*/
getClient(): Client {
return this.client;
}
/**
* Makes a specified bucket public by setting its policy to allow public read access.
*
* @param bucket - The name of the bucket to make public.
*/
async makeBucketPublic(bucket: PublicBucketNames) {
const policy = {
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Principal: { AWS: ['*'] },
Action: ['s3:GetObject'],
Resource: [`arn:aws:s3:::${bucket}/*`],
},
],
};
await this.client.setBucketPolicy(bucket, JSON.stringify(policy));
}
/**
* Ensures that the specified bucket exists in MinIO.
* If the bucket does not exist, it creates a new bucket.
*
* @param bucket - The name of the bucket to ensure.
*/
async ensureBucket(bucket: string) {
try {
const exists = await this.client.bucketExists(bucket);
if (!exists) {
await this.client.makeBucket(bucket, 'us-east-1');
}
} catch (error) {
console.error(error);
throw error;
}
}
/**
* Returns the public URL for an object in a specified bucket.
*
* @param bucket - The name of the bucket.
* @param objectName - The name of the object.
* @returns The public URL for the object.
*/
public getPublicUrl(bucket: string, objectName: string): string {
const protocol = this.minioConfiguration.useSSL ? 'https' : 'http';
return `${protocol}://${this.minioConfiguration.endPoint}:${this.minioConfiguration.port}/${bucket}/${objectName}`;
}
/**
* Generates a presigned URL for uploading an object to a specified bucket.
*
* @param bucket - The name of the bucket.
* @param objectName - The name of the object to upload.
* @param expires - The expiration time for the presigned URL in seconds (default is 300 seconds).
* @returns A presigned URL for uploading the object.
*/
async generatePresignedUrl(
bucket: string,
objectName: string,
expires = 300,
) {
await this.ensureBucket(bucket);
return this.client.presignedPutObject(bucket, objectName, expires);
}
/**
* Removes an object from a specified bucket in MinIO.
* @param bucket - The name of the bucket.
* @param objectName - The name of the object to remove.
* @return A promise that resolves when the object is removed.
*/
async removeObject(bucket: string, objectName: string) {
return this.client.removeObject(bucket, objectName);
}
/**
* Retrieves the metadata of an object in a specified bucket.
*
* @param bucket - The name of the bucket.
* @param objectName - The name of the object to retrieve metadata for.
* @returns Metadata of the specified object.
*/
async statObject(bucket: string, objectName: string) {
return this.client.statObject(bucket, objectName);
}
/**
* Generates a presigned URL for downloading an object from a specified bucket.
*
* @param bucket - The name of the bucket.
* @param objectName - The name of the object to download.
* @param permanet - If true, generates a permanent URL (default is false).
* @param expires - The expiration time for the presigned URL in seconds (default is 3600 seconds).
* @returns A presigned URL for downloading the object.
*/
async getObjectUrl(
bucket: BufferBucketNames | StreamBucketNames | PublicBucketNames,
objectName: string,
expires?: number,
) {
const expireTime = expires ?? 60 * 60 * 24 * 1; // 1 days
if (
Object.values(PublicBucketNames).includes(bucket as PublicBucketNames)
) {
return this.getPublicUrl(bucket, objectName);
}
return this.client.presignedGetObject(bucket, objectName, expireTime);
}
/**
* Checks if an object exists in a specified bucket.
*
* @param bucket - The name of the bucket.
* @param objectName - The name of the object to check.
* @returns A promise that resolves to true if the object exists, false otherwise.
*/
async objectExists(bucket: string, objectName: string): Promise<boolean> {
try {
await this.client.statObject(bucket, objectName);
return true;
} catch (err) {
if (err.code === 'NotFound') return false;
throw err;
}
}
/**
* Uploads a buffer to a specified bucket in MinIO.
*
* @param bucket - The name of the bucket.
* @param objectName - The name of the object to upload.
* @param buffer - The buffer containing the file data.
* @param mimeType - The MIME type of the file.
* @returns A promise that resolves when the upload is complete.
*/
async uploadBuffer(
bucket: string,
objectName: string,
buffer: Buffer,
mimeType: string,
) {
await this.ensureBucket(bucket);
return this.client.putObject(bucket, objectName, buffer, buffer.length, {
'Content-Type': mimeType,
});
}
/**
* Uploads a stream to a specified bucket in MinIO.
*
* @param bucket - The name of the bucket.
* @param objectName - The name of the object to upload.
* @param input - The input stream or buffer to upload.
* @param size - The size of the input data.
* @param mimeType - The MIME type of the file.
* @returns A promise that resolves when the upload is complete.
*/
async uploadStream(
bucket: string,
objectName: string,
input: Buffer | Readable,
size: number,
mimeType: string,
) {
await this.ensureBucket(bucket);
const stream = Buffer.isBuffer(input) ? Readable.from(input) : input;
try {
const result = await this.client.putObject(
bucket,
objectName,
stream,
size,
{
'Content-Type': mimeType,
},
);
return result;
} catch (err) {
throw new Error(`Upload to MinIO failed: ${err.message}`);
}
}
}