gpt4 book ai didi

javascript - 创建目录的读取流并将整个目录通过管道传输到 MongoDB Bucket

转载 作者:行者123 更新时间:2023-12-02 22:58:26 25 4
gpt4 key购买 nike

我正在尝试将整个目录保存到 MongoDB 存储桶

当我压缩文件时它可以工作,但我需要将文件解压缩到存储桶中。

当我尝试流式传输目录时,出现错误“对目录进行非法操作”

我一直在尝试寻找解决方案,但找不到目录的解决方案。

let conn = mongoose.connection;
let gfb;

conn.once('open', () => {
//init the stream
console.log('- Connection open -');
gfb = new mongoose.mongo.GridFSBucket(conn.db, {
chunkSizeBytes: 1024,
bucketName: 'uploads'
})
})

const uploaddb = (req, res, vile) => {
conn.once('open', () => {
console.log('- Connection open -');
gfb = new mongoose.mongo.GridFSBucket(conn.db, {
chunkSizeBytes: 1024,
bucketName: 'uploads'
})
try {
uploadDir('/Users/nancycollins/virtuload-beta/backend/uploads/unzipped/')
} catch (e) {
console.error(e);
}
});
}

async function uploadDir (folder, subFolder = '') {
const actualFolder = path.join(folder, subFolder)
const contents = await fs.readdir(actualFolder, { withFileTypes: true })
await Promise.all(contents.map((fileOrDirectory) =>
fileOrDirectory.isDirectory()
? uploadDir(folder, path.join(subFolder, fileOrDirectory.name))
: uploadFile(folder, path.join(actualFolder, fileOrDirectory.name))
))
}

async function uploadFile (baseFolder, filePath) {
return new Promise((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(gfb.openUploadStream(filePath, { metadata: path.relative(baseFolder, filePath) }))
.on('error', function(error) {
reject(error);
})
.on('finish', () => {
resolve()
})
})
}

-我也尝试将 try-catch 更改为:

    try {
const folder = '/Users/nancycollins/virtuload-beta/backend/uploads/unzipped/';
uploadDir(folder)
} catch (e) {
console.error(e);
}

编辑:

这是更新版本。

当前错误为:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object
at validateString (internal/validators.js:125:11)
at Object.join (path.js:1147:7)

等等...

最佳答案

解决方法:

上传文件夹文件并将其相对文件夹结构存储在 GridFS 的元数据中。在此示例中,uploadDir 是一个递归函数,它遍历给定的文件夹及其子文件夹,并为找到的每个文件调用 uploadFile

在此示例中,我假设您使用的是 fs 的 promise 版本。 .

async function uploadDir (folder, subFolder = '') {
const actualFolder = path.join(folder, subFolder)
const contents = await fs.readdir(actualFolder, { withFileTypes: true })
await Promise.all(contents.map((fileOrDirectory) =>
fileOrDirectory.isDirectory()
? uploadDir(folder, path.join(subFolder, fileOrDirectory.name))
: uploadFile(folder, path.join(actualFolder, fileOrDirectory.name))
))
}

async function uploadFile (baseFolder, filePath) {
return new Promise((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(gfb.openUploadStream(filePath, { metadata: path.relative(baseFolder, filePath) }))
.on('error', function(error) {
reject(error);
})
.on('finish', () => {
resolve()
})
})
}

编辑:

如果您不需要存储更多信息,只需将 GridFS 文件的name 设置为相对路径即可。

关于javascript - 创建目录的读取流并将整个目录通过管道传输到 MongoDB Bucket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57878320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com