MongoDB GridFS API returns 损坏的 .zip
MongoDB GridFS API returns corrupt .zip
上下文:
Node.js / Loopback 应用程序,它的数据库填充了一个 .zip 文件,其中包含来自遗留企业 CRM 应用程序的数据。我正在尝试使用 GridFS 将 .zip 文件的二进制文件存储到我的数据库中以用于生产调试目的,因为该文件可以 >16mb 并使用管理端点随时检索它。
问题:
我可以在我的模块中存储带有函数 storeLatestZipFile
的 zip 文件,我可以使用函数 createLatestZipEndpoint
创建的端点检索它。
但是,我取回的 .zip 文件比原始文件大(14.7mb 对 21.1mb),而且它也已损坏。
我假设我没有对我的数据进行编码或者只是没有正确使用 GridFS API。有人会碰巧发现我的代码中的错误/在使用 GridFS 存储 .zips 方面有更多经验吗?
有问题的模块:
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
};
你试过 createReadStream
没有 buffer
编码吗?
const zipReadStream = fs.createReadStream(path);
上下文:
Node.js / Loopback 应用程序,它的数据库填充了一个 .zip 文件,其中包含来自遗留企业 CRM 应用程序的数据。我正在尝试使用 GridFS 将 .zip 文件的二进制文件存储到我的数据库中以用于生产调试目的,因为该文件可以 >16mb 并使用管理端点随时检索它。
问题:
我可以在我的模块中存储带有函数 storeLatestZipFile
的 zip 文件,我可以使用函数 createLatestZipEndpoint
创建的端点检索它。
但是,我取回的 .zip 文件比原始文件大(14.7mb 对 21.1mb),而且它也已损坏。
我假设我没有对我的数据进行编码或者只是没有正确使用 GridFS API。有人会碰巧发现我的代码中的错误/在使用 GridFS 存储 .zips 方面有更多经验吗?
有问题的模块:
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
};
你试过 createReadStream
没有 buffer
编码吗?
const zipReadStream = fs.createReadStream(path);