Nodejs Nestjs Minio对象存储插件的使用

Nodejs Nestjs Minio对象存储插件的使用
NestJS 是一个用于构建高效、可扩展的服务器端应用程序的框架,而 MinIO 是一个高性能的对象存储系统。在 NestJS 中使用 MinIO 可以通过 nestjs-minio-client 这个库来实现。下面是一个简单的示例,展示如何在 NestJS 项目中设置和使用 MinIO。

安装依赖

首先,你需要安装 @nestjs/corenestjs-minio-client

npm install @nestjs/core
npm install nestjs-minio-client

如果你还没有安装 MinIO 的客户端库,可以安装 minio

npm install minio

配置 MinIO

接下来,在你的 NestJS 应用程序中配置 MinIO 客户端。你可以在 app.module.ts 文件中进行配置:

import { Module } from '@nestjs/common';
import { MinioClientModule } from 'nestjs-minio-client';

@Module({
  imports: [
    MinioClientModule.register({
      endPoint: 'localhost', // MinIO 服务器地址
      port: 9000,           // MinIO 服务器端口
      useSSL: false,        // 是否使用 SSL
      accessKey: 'YOUR_ACCESS_KEY', // MinIO 访问密钥
      secretKey: 'YOUR_SECRET_KEY', // MinIO 秘密密钥
    }),
  ],
})
export class AppModule {}

使用 MinIO

在服务或控制器中注入 MinIO 客户端,并使用它来执行存储操作。例如,创建一个服务 file.service.ts 来处理文件上传:

import { Injectable } from '@nestjs/common';
import { InjectMinioClient } from 'nestjs-minio-client';
import * as Minio from 'minio';

@Injectable()
export class FileService {
  constructor(@InjectMinioClient() private readonly minioClient: Minio.Client) {}

  async uploadFile(bucketName: string, fileStream: NodeJS.ReadableStream, fileName: string): Promise<void> {
    await this.minioClient.putObject(bucketName, fileName, fileStream, fileStream.size);
  }

  async downloadFile(bucketName: string, fileName: string): Promise<Buffer> {
    return await this.minioClient.getObject(bucketName, fileName);
  }
}

在控制器中使用服务

现在你可以创建一个控制器来处理上传和下载请求:

import { Controller, 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.stream, file.originalname);
    return { message: 'File uploaded successfully' };
  }

  @Get(':fileName/download')
  async downloadFile(@Param('fileName') fileName: string) {
    const file = await this.fileService.downloadFile('my-bucket', fileName);
    return file;
  }
}

以上步骤展示了如何在 NestJS 应用程序中设置和使用 MinIO。请确保替换示例中的 MinIO 服务器地址、端口、访问密钥和秘密密钥为实际值。


3 回复

当然,很高兴为你解答关于NestJS与MinIO对象存储插件的使用问题!首先,你需要安装@ nestjs/azure-storage或类似的库来与MinIO交互,但请注意,目前官方并没有直接支持MinIO的NestJS包,所以你可能需要使用minio这个npm包作为替代。

  1. 安装minio包:npm install minio
  2. 创建一个服务来初始化MinIO客户端:
import { Injectable } from '@nestjs/common';
import * as Minio from 'minio';

@Injectable()
export class MinioService {
  private client: Minio.Client;

  constructor() {
    this.client = new Minio.Client({
      endPoint: 'your-minio-endpoint',
      port: 9000, // 默认端口
      useSSL: false, // 如果你的MinIO使用SSL,则设为true
      accessKey: 'your-access-key',
      secretKey: 'your-secret-key',
    });
  }

  // 在这里添加你的方法,如上传文件、获取文件等
}
  1. 使用你的MinioService在控制器中进行操作。

希望这能帮到你,如果有更具体的问题或者需要实现的功能,随时告诉我!


NestJS 是一个用于构建高效、可扩展的服务器端应用程序的框架。MinIO 是一个高性能的对象存储系统,兼容 Amazon S3 云存储协议。结合 NestJS 和 MinIO,可以方便地进行文件上传和管理。

以下是如何在 NestJS 应用程序中使用 MinIO 的步骤:

  1. 安装依赖: 首先,你需要安装 [@nestjs](/user/nestjs)/common[@nestjs](/user/nestjs)/coreminio 这些库。你可以通过 npm 安装它们:

    npm install [@nestjs](/user/nestjs)/common [@nestjs](/user/nestjs)/core minio
    
  2. 配置 MinIO 客户端: 创建一个服务来初始化 MinIO 客户端,并将其注入到你的模块中。

    // minio.service.ts
    import { Injectable } from '[@nestjs](/user/nestjs)/common';
    import * as Minio from 'minio';
    
    [@Injectable](/user/Injectable)()
    export class MinioService {
      private client: Minio.Client;
    
      constructor() {
        this.client = new Minio.Client({
          endPoint: 'localhost',
          port: 9000,
          useSSL: false,
          accessKey: 'YOUR_ACCESS_KEY',
          secretKey: 'YOUR_SECRET_KEY',
        });
      }
    
      async uploadFile(bucketName: string, objectName: string, filePath: string) {
        return this.client.fPutObject(bucketName, objectName, filePath);
      }
    
      async downloadFile(bucketName: string, objectName: string, filePath: string) {
        return this.client.fGetObject(bucketName, objectName, filePath);
      }
    }
    
  3. 创建控制器: 创建一个控制器来处理文件上传请求。

    // files.controller.ts
    import { Controller, Post, UploadedFile, UseInterceptors } from '[@nestjs](/user/nestjs)/common';
    import { FileInterceptor } from '[@nestjs](/user/nestjs)/platform-express';
    import { MinioService } from './minio.service';
    
    [@Controller](/user/Controller)('files')
    export class FilesController {
      constructor(private readonly minioService: MinioService) {}
    
      @Post('upload')
      @UseInterceptors(FileInterceptor('file'))
      async uploadFile(@UploadedFile() file: Express.Multer.File) {
        const bucketName = 'your-bucket-name';
        const objectName = file.originalname;
        await this.minioService.uploadFile(bucketName, objectName, file.path);
        return { message: 'File uploaded successfully' };
      }
    }
    
  4. 配置模块: 在你的模块中导入必要的模块,并提供 MinioService。

    // app.module.ts
    import { Module } from '[@nestjs](/user/nestjs)/common';
    import { AppController } from './app.controller';
    import { AppService } from './app.service';
    import { FilesController } from './files.controller';
    import { MinioService } from './minio.service';
    
    [@Module](/user/Module)({
      imports: [],
      controllers: [AppController, FilesController],
      providers: [AppService, MinioService],
    })
    export class AppModule {}
    
  5. 运行应用: 确保 MinIO 服务器正在运行,并且你可以访问它。然后启动你的 NestJS 应用程序:

    npm run start
    

这样,你就可以通过发送 POST 请求到 /files/upload 来上传文件到 MinIO 了。你可以根据需要调整这个示例以适应你的具体需求。

NestJS 使用 nestjs-minio-client 插件可以方便地与 MinIO 对象存储进行交互。首先安装插件:

npm install @quentinpied/nestjs-minio-client

然后,在模块中配置并注入 MinioClient:

import { Module } from '@nestjs/common';
import { MinioClientProvider } from '@quentinpied/nestjs-minio-client';

@Module({
  providers: [
    MinioClientProvider({
      options: {
        endPoint: 'your-minio-endpoint',
        port: 9000,
        useSSL: false,
        accessKey: 'your-access-key',
        secretKey: 'your-secret-key',
      },
    }),
  ],
})
export class ApplicationModule {}

之后,您可以在服务中注入 MinioClient 并调用相关方法实现文件上传、下载等功能。

回到顶部