我正在尝试创建一个读取流以使用Cloudinary的上载流功能,我还使用resumable.js对初始文件进行分块,而创建读取流的工作状况非常好(因为整个文件被写入的情况都很好)。流/云上传功能似乎甚至没有默默地触发和失败。
router.post("/upload", (req, res, next) => {
console.log("the params are.. ", req.body);
resumable.post(req, function(
status,
filename,
original_filename,
identifier
) {
if (status === "done") {
let timestamp = new Date().getTime().toString();
//stich the chunks
var s = fs.createWriteStream(timestamp + filename);
resumable.write(identifier, s);
var upload_stream = cloudinary.uploader.upload_stream(
{ tags: "basic_sample" },
function(err, image) {
console.log();
console.log("** Stream Upload");
if (err) {
console.warn(err);
}
console.log("* Same image, uploaded via stream");
console.log("* " + image.public_id);
console.log("* " + image.url);
waitForAllUploads(timestamp + filename, err, image);
}
);
fs.createReadStream(timestamp + filename)
.pipe(upload_stream)
.on("error", err => {
console.log(err);
});
s.on("finish", function() {
// Stream upload
console.log("ive finished...");
// delete chunks
setTimeout(() => {
resumable.clean(identifier);
}, 1000);
});
}
res.send(status);
});
});
以下是我正在使用的资源:
https://github.com/cloudinary/cloudinary_npm/blob/master/samples/basic/basic.js
https://github.com/mrawdon/resumable-node
最佳答案
fs.createReadStream(timestamp + filename)接受文件路径,但看起来您也正在传递时间戳。此外,未定义waitForAllUploads。您可以仅使用Node和Cloudinary来测试以下代码。
var upload_stream= cloudinary.uploader.upload_stream({tags: 'basic_sample'},function(err,image) {
console.log("** Stream Upload");
if (err){ console.warn(err);}
console.log("* "+image.url)
});
var file_reader = fs.createReadStream('<file path>').pipe(upload_stream);
关于javascript - 读取流未触发/捕获错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60474118/