【问题标题】:Using multer-gridfs stream in nestjs在nestjs中使用multer-gridfs流
【发布时间】:2019-08-11 00:10:38
【问题描述】:

我正在尝试在 nestjs 中显式使用 multer-gridfs-storage 中间件。我想存储一个文件并使用 gridfs-stream 包检索它。但没有什么对我有用。我无法用文件本身填充请求。每当我尝试打印请求对象时,它都没有与 req.file 或缓冲区元素相关的任何内容。而且我不确定如何使用 multer 获取文件后如何处理该文件。

这里是示例控制器方法

@Post('/uploadFiles')
    public async postFileToMongo(@Req() req, @Res() res) {

        //logger.info('this is the mutler upload details',req.body);
        //logger.info('coming inside upload files', req);
        logger.info('this is the sample value set');
        logger.info('this is the request body', req);
        logger.info('this is the request files', req.file);
        await this.fileStorageService.createFilesIntoGridFsUsingGridFSStream();
        res.send('Done');
    }

这里是 FileStorageService。我还没有实现 multerStorage,因为我没有从控制器得到任何东西(req.file 是空的)

 @Injectable()
    export class FileStorageService {
      mongooseConnection: mongoose.Connection;
      gridfsConnection: GridFsStream.Grid;
      gridfsStorage: GridFSStorage;
      uploadStorage;
      constructor(
        @Inject(MONGO_DB_PROVIDER_TOKEN_TEST) private readonly mongoCon: Connection,
        private readonly configService : ConfigProviderService,
      ) {
        this.mongooseConnection = this.mongoCon as mongoose.Connection;

     this.gridfsConnection = GridFsStream(
            this.mongooseConnection.db,
            mongoose.mongo,
          );
        this.gridfsConnection.collection('default_collection');
          // now intialize the storage engine

        this.gridfsStorage = new GridFSStorage({
          db: this.mongooseConnection,
          cache: true,
          file: (req, file) => {

            // instead of just returning fileName or bucket Name as below
            // we could return an object 
            // let bucket;
            // if (file.mimetype === 'multipart/*' || file.mimetype === 'img/jpeg') {
            //   bucket = 'first_collection';
            // } else {
            //   bucket = 'second_collection';
            // }
            return {
              filename: file.originalname + Date.now(),
              bucketName: 'default_collection',
            }
          },
        });
        this.uploadStorage = multer({ storage: this.gridfsStorage });


        logger.info('gridfsConnection obj', isNullOrUndefined(this.gridfsConnection));
        //logger.info('upload storage vale', this.uploadStorage);
      }

    async createFilesIntoGridFsUsingGridFSStream() {
        //console.log(this.uploadStorage);
       const writeStream  =  this.gridfsConnection.createWriteStream({
            filename: 'doodle_test.png',
            mode: 'w',
            root: 'first_collect',
            chunkSize: 1024,
            metadata: {
              file_desc: 'This is a sample metadata added',
            },
        });
       const filestring = path.resolve('./doodle.png');
       await fs.createReadStream(filestring).pipe(writeStream).on('error', err => {
          logger.info('err ob', err);
        });
       writeStream.on('close', (file) => {
          logger.info('file name is', file.filename);
        });
      }

      async createFilesUsingMulter(req, ){

      }


    }

我什至尝试创建一个单独的中间件,但它也不起作用

@Injectable()
export class FileStorageMiddlewareMiddleware implements NestMiddleware {
  mongooseConnection: Connection;
  constructor(@Inject(MONGO_DB_PROVIDER_TOKEN_TEST) private readonly mongoConnection: Connection,
              private readonly configService: ConfigProviderService){
    this.mongooseConnection = this.mongoConnection as mongoose.Connection;
  }
  resolve(...args: any[]): MiddlewareFunction {
    return (req, res, next) => {
      logger.info('inside file storage middleware');
      // const fileStorage = new multerGridfsStorage({
      //   // url: this.configService.getDBCred().$urlString,
      //   db: this.mongoConnection as Connection,
      //   cache: true,
      //   file :(req, file) => {
      //     //determine the default filename
      //     const filename = file.filename+'feedback_file_'+Date.now();
      //     let bucketname = 'default_file_collection';
      //     if(file.mimetype === 'plain/text') {
      //       bucketname = 'files.text';
      //     } else if(file.mimetype === 'plain/html'){
      //       bucketname = 'files.html'; 
      //     } else if(file.mimetype === 'img/png' || file.mimetype === 'img/jpeg' ||
      //       file.mimetype === 'img.jpg'){
      //       bucketname = 'files.image';
      //     }

      //     return {fileName: filename, bucketName: bucketname};
      //     // determing the bucketname
      //   },
      // });
      // const upload = multer({storage: fileStorage});

      // //upload.single('./../../../src/fileUpload');
      // //busboyBodyParser({ limit: '1kb' }, { multi: true });
      // // req.busboyBodyParser([{limit: '20mb'}, {multi: true}]);
      // upload.single('file');
      next();
    };
  }
}

注意:我在模块中包含了中间件并使用它。我可以看到中间件中的日志,但看不到多格式部分数据

这里是“邮递员”工具的详细信息(使用 3002 作为端口,'development' 作为上下文路径

这是我使用 gridfs-stream 模块遵循的过程,从本地文件夹中获取示例文件并将其存储在 gridfs 中并且它工作。

   async createFilesIntoGridFsUsingGridFSStream() {
this.gridfsConnection = GridFsStream(
            this.mongooseConnection.db,
            mongoose.mongo,
          );
        //console.log(this.uploadStorage);
       const writeStream  =  this.gridfsConnection.createWriteStream({
            filename: 'doodle_test.png',
            mode: 'w',
            root: 'first_collect',
            chunkSize: 1024,
            metadata: {
              file_desc: 'This is a sample metadata added',
            },
        });
       const filestring = path.resolve('./doodle.png');
       await fs.createReadStream(filestring).pipe(writeStream).on('error', err => {
          logger.info('err ob', err);
        });
       writeStream.on('close', (file) => {
          logger.info('file name is', file.filename);
        });
      }

但是有一个警告 我认为 gridfs 默认使用 GridStore 并尝试将其存储在集合中

DeprecationWarning:GridStore 已弃用,将在 未来的版本。请改用 GridFSBucket (node:29332) 弃用警告:collection.ensureIndex 已弃用。采用 而是创建索引。 (节点:29332)弃用警告: collection.save 已弃用。使用 insertOne、insertMany、updateOne、 或 updateMany。

因此想使用multer-gridfs-storage,它默认使用GridFSStorage

以防万一,这里是版本详细信息

"@types/mongoose": "^5.3.20",
    "@types/multer": "^1.3.7",
    "@types/multer-gridfs-storage": "^3.1.1", "gridfs": "^1.0.0",
    "gridfs-stream": "^1.1.1",  "mongoose": "^5.4.11",
    "multer": "^1.4.1",
    "multer-gridfs-storage": "^3.2.3",

编辑:这是出于学习目的,并希望深入了解如何在 nestjs 中处理 express 中间件。我知道nestjs 有类似 FileInterceptor 的东西来处理文件,但想获得更多关于这个的信息

另外我想问一下multer-gridfs-storage是如何使用gridfs-stream来存储文件的,有没有规定multer-gridfs-storage应该使用什么样的流?

【问题讨论】:

    标签: mongoose nestjs multer-gridfs-storage


    【解决方案1】:

    【讨论】:

    • 但我正在寻找将它与nestjs一起使用。使用 multer-gridfs0storage 我遇到了问题。我想我找到了一种单独使用 gridfsStream 的方法。我会分享答案
    猜你喜欢
    • 2019-04-27
    • 2015-03-11
    • 2018-08-12
    • 2019-12-05
    • 2018-09-26
    • 2019-01-17
    • 2021-11-09
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多