Node.js 中的 SFTP 正在上传空文件
SFTP in Node.js uploading empty files
我正在处理的项目有问题,我正在使用名为 sftp-ssh2-client 的节点模块从 FTP 服务器下载文件,然后尝试传递流回到我的 FTP 服务器,但进入不同的目录。
代码如下所示:
const stream = sftp.get(`path/to/file.csv.gz`).then(() => {
}).catch((err) => {
});
sftp.put(stream, 'path/to/new/file.csv.gz').then(() => {
}).catch((err) => {
})
然而,当我拨入我的 FTP 服务器到文件被 PUT 到的位置时,文件大小为 0 并且下载后已损坏。谁能告诉我我在这里做错了什么?
非常感谢,
G
sftp.get()
不是 return 流,它 return 是 解析 流的承诺,因此您的代码应该看起来像这样:
sftp.get('path/to/file.csv.gz').then(stream => {
return sftp.put(stream, 'path/to/new/file.csv.gz');
}).catch(err => {
...
});
但是,在我看来,您可以只使用 sftp.rename()
,这不需要下载和上传整个文件:
sftp.rename('path/to/file.csv.gz', 'path/to/new/file.csv.gz').then(...);
此外,如果您确实想采用先下载后上传的方式,请务必阅读 documentation regarding encoding。
function uploadFile(req, res) {
req.pipe(req.busboy);
req.busboy.on('file', function(fieldname, file, filename) {
// debug("Uploading: " + filename);
filename = (new Date()).getTime() + '-' + filename;
//Path where file will be uploaded
var fstream = fs.createWriteStream(FILE_STORE + filename);
file.pipe(fstream);
fstream.on('close', function() {
// debug("Upload Finished of " + filename);
var newFileName = FILE_STORE + filename;
if (!fs.existsSync(newFileName)) {
res.status(404).json({ 'message': 'file not found' })
return;
}
res.status(200).json({ file: filename });
})
})
}
我正在处理的项目有问题,我正在使用名为 sftp-ssh2-client 的节点模块从 FTP 服务器下载文件,然后尝试传递流回到我的 FTP 服务器,但进入不同的目录。
代码如下所示:
const stream = sftp.get(`path/to/file.csv.gz`).then(() => {
}).catch((err) => {
});
sftp.put(stream, 'path/to/new/file.csv.gz').then(() => {
}).catch((err) => {
})
然而,当我拨入我的 FTP 服务器到文件被 PUT 到的位置时,文件大小为 0 并且下载后已损坏。谁能告诉我我在这里做错了什么?
非常感谢,
G
sftp.get()
不是 return 流,它 return 是 解析 流的承诺,因此您的代码应该看起来像这样:
sftp.get('path/to/file.csv.gz').then(stream => {
return sftp.put(stream, 'path/to/new/file.csv.gz');
}).catch(err => {
...
});
但是,在我看来,您可以只使用 sftp.rename()
,这不需要下载和上传整个文件:
sftp.rename('path/to/file.csv.gz', 'path/to/new/file.csv.gz').then(...);
此外,如果您确实想采用先下载后上传的方式,请务必阅读 documentation regarding encoding。
function uploadFile(req, res) {
req.pipe(req.busboy);
req.busboy.on('file', function(fieldname, file, filename) {
// debug("Uploading: " + filename);
filename = (new Date()).getTime() + '-' + filename;
//Path where file will be uploaded
var fstream = fs.createWriteStream(FILE_STORE + filename);
file.pipe(fstream);
fstream.on('close', function() {
// debug("Upload Finished of " + filename);
var newFileName = FILE_STORE + filename;
if (!fs.existsSync(newFileName)) {
res.status(404).json({ 'message': 'file not found' })
return;
}
res.status(200).json({ file: filename });
})
})
}