上下文:
Node.js /回送应用程序,其数据库填充有一个.zip文件,该文件包含来自旧企业CRM应用程序的数据。我正在尝试使用GridFS将.zip文件的二进制文件存储到我的数据库中,以进行生产调试,因为该文件的大小可以大于16mb,并且可以随时使用管理端点对其进行检索。
问题:
我可以将具有功能storeLatestZipFile
的zip文件存储在模块中,并可以使用由功能createLatestZipEndpoint
创建的端点进行检索。
但是,我返回的.zip大于原始文件(14.7mb和21.1mb),并且它也已损坏。
我假设我没有对数据进行编码,或者只是没有正确使用GridFS API。会有人碰巧发现我的代码中的错误/是否有使用GridFS存储.zip的更多经验?
有问题的模块
const { pino } = require('amf-logger');
const fs = require('fs');
const mongo = require('mongodb');
const log = pino({ name: 'bot-zip-upload-storage' });
/**
* @param {string} path Path to the zip file to be persisted.
* @param {object} app Loopback application instance.
*/
function storeLatestZipFile(path = './', app = {}) {
log.info('**** Starting streaming current uploaded zip to DB ****');
const zipReadStream = fs.createReadStream(path, { encoding: 'binary' });
const { db } = app.dataSources.mongo.connector;
const bucket = new mongo.GridFSBucket(db);
bucket.delete('zipfile', () => {
log.info('deleted old zipfile');
const uploadStream = bucket.openUploadStreamWithId(
'zipfile',
`bot-data-${new Date().toISOString()}`,
{
contentType: 'application/zip'
}
);
zipReadStream.pipe(uploadStream);
});
}
/**
* @param {object} app Loopback application instance.
*/
async function createLatestZipEndpoint(app = {}) {
if (!app.get) {
log.error("app object does not have 'get' property.");
return;
}
app.get('/api/admin/latestzip', async (req, res) => {
if (!req.headers.latestfile || req.headers.latestfile !== process.env.ADMIN_LATESTFILE) {
res.sendStatus(403);
return;
}
try {
const { db } = app.dataSources.mongo.connector;
const bucket = new mongo.GridFSBucket(db);
res.writeHead(200, { 'Content-Type': 'application/zip' });
const downloadStream = bucket.openDownloadStream('zipfile');
log.info('download stream opened, begin streaming');
downloadStream.pipe(res);
} catch (err) {
log.error(`error getting zipfile: ${err}`);
res.sendStatus(500);
}
});
}
module.exports = {
storeLatestZipFile,
createLatestZipEndpoint
};
答案 0 :(得分:1)
您是否尝试过createReadStream
而不进行buffer
编码?
const zipReadStream = fs.createReadStream(path);