-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsftp_download_files.js
More file actions
86 lines (71 loc) · 1.98 KB
/
sftp_download_files.js
File metadata and controls
86 lines (71 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Client from 'ssh2-sftp-client@^12.0.1'
/**
* Walks a directory on the remote SFTP server and returns a list of files. Returns an object with filenames as keys
* and content as values.
*
* @param {Client} sftp
* @param {string} remotePath
* @param {Record<string, string>} files
* @returns {Promise<Object>}
*/
async function walkDir(sftp, remotePath, files = {}) {
const list = await sftp.list(remotePath)
for (const item of list) {
if (item.type === '-') {
const fullPath = `${remotePath}/${item.name}`
const file = await sftp.get(fullPath)
files[item.name] = file.toString()
}
}
return files
}
export default {
name: 'Download SFTP Files',
description: 'Downloads all files from a directory on an SFTP host. Returns an object with filenames ' +
'as keys and content as values.',
key: 'sftp_download_files',
version: '0.0.1',
type: 'action',
props: {
sftp: {
type: 'app',
app: 'sftp',
label: 'SFTP Account app',
description: 'SFTP Account connection app established in Pipedream Accounts tab',
},
directory: {
type: 'string',
label: 'Directory on SFTP host',
description: 'The directory on the SFTP server from which to download files.',
default: '/',
optional: true,
}
},
async run({ $ }) {
const { host, username, privateKey } = this.sftp.$auth
if (!host) {
throw Error('missing host')
}
if (!username) {
throw Error('missing username')
}
if (!privateKey) {
throw Error('missing privateKey')
}
const config = {
host,
username,
privateKey,
}
const sftp = new Client()
await sftp.connect(config)
const files = await walkDir(sftp, this.directory)
await sftp.end()
const fileCount = Object.keys(files).length
const summary = fileCount > 0 ? `Successfully downloaded ${fileCount} files.` : `No files found.`
$.export('$summary', summary)
return {
files,
}
},
}