我正在尝试在 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');
}
这是文件存储服务。我还没有实现 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 作为端口,使用“开发”作为上下文路径
这是我使用 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) DeprecationWarning: collection.ensureIndex 已弃用。请改用 createIndexes。(节点:29332)DeprecationWarning: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 中应该使用什么样的流?