brokenbot / sftp-promises Goto Github PK
View Code? Open in Web Editor NEWSFTP Promise Wrapper for ssh2
License: BSD 3-Clause "New" or "Revised" License
SFTP Promise Wrapper for ssh2
License: BSD 3-Clause "New" or "Revised" License
sftp.put(
__filename,
pathOfNotExistOnSFTPServer,
);
or
sftp.putBuffer(
Buffer.from(''),
path.join('/home/mulps/www/app/build', __filename),
);
result:
Error: No such file
If the file does not exist, I want to create it and write it in the file.
Hi,
I'm trying a basic stat on an non existing file:
const sftp = new SFTPClient(credentials);
return sftp.stat(path)
.catch((err) => {
console.error('Oups', err);
})
.then((entry) => {
var filename = entry.path.split('/').pop();
return {
size: entry.size,
modified: entry.mtime,
name: filename,
isDir: entry.type === 'directory',
mime: Mime.lookup(filename)
}
});
I've got an error trace:
{ Error: No such file
at SFTPStream._transform (/home/jbrichardet/Documents/workspace/unifile/node_modules/ssh2-streams/lib/sftp.js:405:27)
at SFTPStream.Transform._read (_stream_transform.js:167:10)
at SFTPStream._read (/home/jbrichardet/Documents/workspace/unifile/node_modules/ssh2-streams/lib/sftp.js:181:15)
at SFTPStream.Transform._write (_stream_transform.js:155:12)
at doWrite (_stream_writable.js:334:12)
at writeOrBuffer (_stream_writable.js:320:5)
at SFTPStream.Writable.write (_stream_writable.js:247:11)
at Channel.ondata (_stream_readable.js:555:20)
at emitOne (events.js:96:13)
at Channel.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Channel.Readable.push (_stream_readable.js:134:10)
at SSH2Stream.<anonymous> (/home/jbrichardet/Documents/workspace/unifile/node_modules/ssh2/lib/Channel.js:166:15)
at emitOne (events.js:96:13)
at SSH2Stream.emit (events.js:188:7)
at parsePacket (/home/jbrichardet/Documents/workspace/unifile/node_modules/ssh2-streams/lib/ssh.js:3439:10) code: 2, lang: '' }`
but I'm unable to catch it, read the code and return a informative message to the user. Is my .catch
at the wrong place?
Thank you
Hi,
I am getting wrong modification time and access time as json value, even though the directory is created long back it is giving today date please help.
Thanks in advance
Hi, brokenbot,
can u please help me for readStream I tried but it is not coming as the proper response can u please help me.
Here is my code
var SFTPClient = require('sftp-promises');
var path = require('path');
var config = {host: 'xx.xx.xx.xx', username: 'user', password: 'user' };
var sftp = new SFTPClient(config);
var localPath = path.normalize('test.txt');
var remotePath = path.normalize('/home/sftpfiles');
sftp.put(localPath, remotePath).then(function(res) { console.log(res) });
sftp.ls('/home/sftpfiles').then(function(list) { console.log(list) });
i am getting result as true, but no files get transferred in remote path.
i am using windows 10 with node.0.12.x
Very nice, but a bit twitchy about the format of paths. The example shown that uses:
sftp.ls('~/').then(function(list) { console.log(list) })
doesn't work as shown on my Mint/Ubuntu box. Mine requires:
sftp.ls('.').then(function(list) { console.log(list) })
to get the expected result.
Probably OS and sftp server dependent. I am running the OpenSSH daemon.
Dave
I'm uploading large files using a stream. How can I figure out the upload percentage? I don't see any methods (since this is a promise) that give me access to the stream and the amount of data being chunked at a time.
We recently upgraded from an older version of node to Node 10, and ever since I've been experiencing an issue with a read stream from a remote server just not working when attempting to pipe from the SFTP read stream.
An example of our setup:
console.log('Creating the stream');
let streamPromise = new Promise((resolve, reject) => {
sftp.createReadStream('/Mydata/download_report.csv').then(stream => {
console.log('Read stream created! Creating the write stream');
let writeStream = fs.createWriteStream('download_report.csv');
console.log('Write stream created!');
stream.on('error', (error) => {
console.error('Error in read stream', error);
return reject(error);
});
stream.on('data', (data) => {
console.log('data in read stream', Buffer.from(data, 'base64').toString());
});
stream.on('end', () => {
try {
writeStream.end();
} catch(err) {
// Suppress warning, but still resolve
return resolve('Done! Resolved from stream.on end in the error handler for writeStream.end()');
}
});
writeStream.on('data', (data) => {
console.log('data in write stream', Buffer.from(data, 'base64').toString());
});
writeStream.on('error', error => {
console.error('Error in write stream', error);
return reject(error);
});
writeStream.on('finish', () => {
console.log('Finish called in writeStream.')
return resolve('Done! Resolved from stream.on finish in writeStream.');
});
console.log('Piping the stream');
stream.pipe(writeStream);
}).catch(error => {
console.error('Error creating read stream', error);
return reject(error);
});
});
streamPromise.then((results) => {
console.log('Stream Promise resolved!');
console.log(results);
}).catch(console.error);
In our old Node installation, this logs out fine:
Creating the stream
Read stream created! Creating the write stream
Write stream created!
Piping the stream
data in read stream
Col A,Col B,Col C,Col D
a,b,c,d
Finish called in writeStream.
Done! Resolved from stream.on finish in writeStream.
Checking the local FS shows the file with all the expected data.
But in Node 10, the log is just:
Creating the stream
Read stream created! Creating the write stream
Write stream created!
Piping the stream
Then... nothing else. It just stops, and the local file is empty.
Hello there,
I've encounter a compatibility break by switching from Node 6.2.2 to Node 6.3.0 (or higher, also try 6.3.1, 6.4.0, 6.5.0). I'm doing a ls command and my program is not detecting the file type (at least for directories). Here is my code:
let ftpConnection = new SftpClient(config.dataSourceOptions.sftpOptions)
return ftpConnection.ls(config.dataSourceOptions.sftpOptions.directory)
.then(function(result)
{
console.log(result)
})
Result with Node 6.2.2:
{
path: "path/to/directory",
type: "directory",
attrs: {
// ...
},
entries: [
// ...
]
}
Result with Node >= 6.3.0
{
path: "path/to/directory",
type: "other",
attrs: {
// ...
}
}
The directory type is not detected anymore. This is embarassing because I can't iterate through the "entry" option.
Now, I don't know if this is a (non-wanted) regression from Node or if it comes from this lib using deprecated code...
Thanks for your help.
Here's the situation:
I have an SFTP server which has a folder within it. (I'd rather not use our actual SFTP server and folder names, so let's call this folder 'Mydata', matching the capitalization used in our setup).
When I ls
the root directory using "."
and loop over all of the folders within it, I successfully get a result whose entries list includes an entry with filename
of "Mydata". I can subsequently stat
that data using stat("./Mydata")
to show that it is indeed a folder.
However, when I try to directly ls("./Mydata")
, it consistently fails with "No such file". Trying ls("/Mydata")
and ls("Mydata")
also fail.
I'm absolutely baffled. I know I've successfully ls
d other subdirectories of other FTP servers before, so what could be the problem with this one?
One of the changes to 1.8.0 was updating SSH2 from 0.8.2 to 1.4.0. That should have resulted in a major version number for sftp-promises, since ssh2 had a major version update and was no longer backward compatible. And in fact, it was not. Version 0.8.2 supports Node as far back as v5, but version 1.4 requires v10+.
Hi,
I just saw that putBuffer()
resolve before the connection is closed. Looking in the code I saw a resolve
outside of the close
callback (and, I think, unnecessary).
Removing it solved my issue but I don't know if it has an other purpose.
stack:
ssh2-streams/lib/sftp.js:826
throw new Error('offset is out of bounds');
^
Error: offset is out of bounds
at SFTPStream.readData (/home/vagrant/Projects/sftp-rest/node_modules/koa-sftp/node_modules/sftp-promises/node_modules/ssh2/node_modules/ssh2-streams/lib/sftp.js:826:11)
at SFTPWrapper.read (/home/vagrant/Projects/sftp-rest/node_modules/koa-sftp/node_modules/sftp-promises/node_modules/ssh2/lib/SFTPWrapper.js:45:23)
at /home/vagrant/Projects/sftp-rest/node_modules/koa-sftp/node_modules/sftp-promises/index.js:211:16
at SFTPStream._transform (/home/vagrant/Projects/sftp-rest/node_modules/koa-sftp/node_modules/sftp-promises/node_modules/ssh2/node_modules/ssh2-streams/lib/sftp.js:470:15)
at SFTPStream.Transform._read (_stream_transform.js:167:10)
at SFTPStream._read (/home/vagrant/Projects/sftp-rest/node_modules/koa-sftp/node_modules/sftp-promises/node_modules/ssh2/node_modules/ssh2-streams/lib/sftp.js:173:15)
at SFTPStream.Transform._write (_stream_transform.js:155:12)
at doWrite (_stream_writable.js:292:12)
at writeOrBuffer (_stream_writable.js:278:5)
at SFTPStream.Writable.write (_stream_writable.js:207:11)
Need to check for this before attempting transfer.
Hi,
I am using the following code in order to verify the credentials.
const config = {
host: req.body.hostname,
username: req.body.username,
password: req.body.password,
port: req.body.port
};
try {
const sftp = new SFTPClient(config);
const session = yield sftp.session(config); // get session
session.end(); //end the session if exists
} catch (error) {
throw `Could not validate the credentials.`
}
With wrong credentials, the function session() should reject the promise but it doesn't. It just gets struck forever.
I also have the following code:
//handling all the uncaught exception
process.on('uncaughtException', error => console.log(`Caught exception: ${error}` ));
The exception is caught at this level (twice):
Caught exception: Error: All configured authentication methods failed
Caught exception: Error: Timed out while waiting for handshake
Can you please look into the issue.
Version: 1.4.0
Node Version: v6.9.4
NPM Version: 3.10.10
Note: Don't get confuse by yield
I am using co
library to handle the promises.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.