Nodejs Nestjs对象存储插件nestjs-minio的使用
Nodejs Nestjs对象存储插件nestjs-minio的使用nestjs-minio
是一个用于 NestJS 应用程序的 MinIO 客户端库。MinIO 是一个高性能的对象存储系统,兼容 Amazon S3 云存储服务接口。以下是如何在 NestJS 项目中安装和使用 nestjs-minio
插件的步骤。
1. 安装依赖
首先,你需要安装 nestjs-minio
和 minio
:
npm install @quentin-saint-emilion/nestjs-minio minio
2. 配置模块
接下来,在你的 NestJS 模块中配置 MinioModule
。假设你有一个名为 StorageModule
的模块:
import { Module } from '@nestjs/common';
import { MinioModule } from '@quentin-saint-emilion/nestjs-minio';
@Module({
imports: [
MinioModule.forRoot({
endPoint: 'play.min.io', // MinIO 服务器地址
port: 9000, // 端口号
useSSL: false, // 是否使用 SSL
accessKey: 'YOUR_ACCESS_KEY', // 访问密钥
secretKey: 'YOUR_SECRET_KEY', // 秘密密钥
}),
],
})
export class StorageModule {}
3. 使用服务
然后,你可以通过注入 MinioService
来与 MinIO 进行交互。例如,在一个服务中:
import { Injectable } from '@nestjs/common';
import { MinioService } from '@quentin-saint-emilion/nestjs-minio';
@Injectable()
export class FileService {
constructor(private readonly minioService: MinioService) {}
async uploadFile(bucketName: string, filePath: string, objectName: string): Promise<void> {
const fileStream = fs.createReadStream(filePath);
await this.minioService.client.putObject(bucketName, objectName, fileStream, fileStream.stat());
}
async downloadFile(bucketName: string, objectName: string, destinationPath: string): Promise<void> {
const fileStream = fs.createWriteStream(destinationPath);
await this.minioService.client.getObject(bucketName, objectName).pipe(fileStream);
}
}
4. 控制器
最后,创建一个控制器来处理 HTTP 请求:
import { Controller, Get, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { FileService } from './file.service';
@Controller('files')
export class FilesController {
constructor(private readonly fileService: FileService) {}
@Post('upload')
@UseInterceptors(FileInterceptor('file'))
async uploadFile(@UploadedFile() file: Express.Multer.File) {
await this.fileService.uploadFile('my-bucket', file.path, file.originalname);
return { message: 'File uploaded successfully' };
}
@Get('download/:filename')
async downloadFile(@Param('filename') filename: string) {
const tempFilePath = path.join(os.tmpdir(), filename);
await this.fileService.downloadFile('my-bucket', filename, tempFilePath);
return attachment(tempFilePath, filename);
}
}
以上就是如何在 NestJS 中使用 nestjs-minio
插件的基本步骤。确保替换 endPoint
, accessKey
, secretKey
等配置项为你的 MinIO 实例的实际值。
当然,NestJS 的 nestjs-minio
插件可以让你轻松地与 MinIO 对象存储进行交互。首先,确保你已经安装了这个插件和 MinIO 客户端:
npm install @ nestia/minio
然后,在你的 NestJS 模块中配置 MinIO 客户端:
import { Module } from '@nestjs/common';
import { MinioClientModule } from '@nestia/minio';
@Module({
imports: [
MinioClientModule.forRoot({
endPoint: 'localhost',
port: 9000,
useSSL: false,
accessKey: 'YOUR_ACCESS_KEY',
secretKey: 'YOUR_SECRET_KEY',
}),
],
})
export class AppModule {}
现在,你可以在服务中注入 MinioClient
来上传、下载文件等:
import { Injectable } from '@nestjs/common';
import { MinioClient } from '@nestia/minio';
@Injectable()
export class FileService {
constructor(private readonly minioClient: MinioClient) {}
async uploadFile(bucketName: string, fileStream: any, fileName: string) {
await this.minioClient.bucket(bucketName).file(fileStream, fileName).upload();
}
async downloadFile(bucketName: string, fileName: string) {
const fileStream = await this.minioClient.bucket(bucketName).file(fileName).download();
return fileStream;
}
}
这样,你就有了一个基本的对象存储系统!记得替换示例中的 YOUR_ACCESS_KEY
和 YOUR_SECRET_KEY
为你的 MinIO 实例的凭据。祝你编程愉快!
nestjs-minio
是一个用于 NestJS 应用程序的 MinIO 客户端插件,它可以帮助你轻松地将文件上传到 MinIO 对象存储。以下是如何安装和使用 nestjs-minio
的步骤。
1. 安装
首先,你需要安装 nestjs-minio
包:
npm install @quentinvenez/minio
2. 配置
接下来,你需要在你的 NestJS 应用中配置 MinIO 客户端。你可以在 app.module.ts
中进行配置:
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MinioModule } from '@quentinvenez/minio';
@Module({
imports: [
MinioModule.forRoot({
endPoint: 'localhost', // MinIO 服务器地址
port: 9000, // MinIO 服务器端口
useSSL: false, // 是否使用 SSL
accessKey: 'YOUR_ACCESS_KEY', // MinIO 访问密钥
secretKey: 'YOUR_SECRET_KEY', // MinIO 秘密密钥
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
3. 使用
现在你可以在你的服务或控制器中注入 MinioService
来使用 MinIO 客户端:
import { Injectable } from '@nestjs/common';
import { InjectMinio } from '@quentinvenez/minio';
import { MinioService } from '@quentinvenez/minio';
@Injectable()
export class AppService {
constructor(@InjectMinio() private readonly minioClient: MinioService) {}
async uploadFile(bucketName: string, fileStream: any, fileName: string): Promise<string> {
const metaData = {
'Content-Type': 'application/octet-stream',
};
await this.minioClient.client.putObject(bucketName, fileName, fileStream, metaData);
return `${bucketName}/${fileName}`;
}
}
4. 控制器示例
你可以在控制器中调用这个服务方法来实现文件上传功能:
import { Controller, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { AppService } from './app.service';
@Controller('files')
export class FilesController {
constructor(private readonly appService: AppService) {}
@Post('upload')
@UseInterceptors(FileInterceptor('file'))
async uploadFile(@UploadedFile() file) {
const bucketName = 'my-bucket';
const filePath = await this.appService.uploadFile(bucketName, file.buffer, file.originalname);
return { filePath };
}
}
以上就是如何在 NestJS 应用程序中使用 nestjs-minio
插件的基本步骤。你可以根据自己的需求调整配置和代码。
Nestjs-minio是用于NestJS应用的MinIO对象存储插件。首先安装该插件:
npm install nestjs-minio --save
然后,在app.module.ts
中配置并使用它:
import { Module } from '@nestjs/common';
import { MinioModule } from 'nestjs-minio';
@Module({
imports: [
MinioModule.register({
endPoint: 'minio-endpoint',
port: minio-port,
useSSL: true/false, // 根据需要设置
accessKey: 'your-access-key',
secretKey: 'your-secret-key',
}),
],
})
export class AppModule {}
之后,可以在服务中注入MinioService
来操作文件。例如,上传文件:
import { Injectable } from '@nestjs/common';
import { MinioService } from 'nestjs-minio';
@Injectable()
export class AppService {
constructor(private readonly minioService: MinioService) {}
async uploadFile(bucketName: string, fileStream: any, fileName: string) {
await this.minioService.uploadObject(bucketName, fileName, fileStream);
}
}