@@ -51,40 +51,84 @@ async function main() {
5151 console . log ( 'exit code:' , exitCode ) ;
5252
5353 // ============================================
54- // Example 3: Upload and download files
54+ // Example 3: Upload and download single files
5555 // ============================================
56- console . log ( '\n--- SFTP File Transfer ---' ) ;
56+ console . log ( '\n--- SFTP Single File Transfer ---' ) ;
5757
58- // Create a local test file
5958 const fs = await import ( 'node:fs/promises' ) ;
6059 const os = await import ( 'node:os' ) ;
6160 const path = await import ( 'node:path' ) ;
6261
6362 const testDir = path . join ( os . tmpdir ( ) , `vers-example-${ Date . now ( ) } ` ) ;
6463 await fs . mkdir ( testDir , { recursive : true } ) ;
6564
65+ // Create and upload a single file
6666 const localFile = path . join ( testDir , 'hello.txt' ) ;
6767 await fs . writeFile ( localFile , 'Hello from Vers SDK!' ) ;
6868
69- // Upload file
7069 const uploadResult = await vm . upload ( vmId , localFile , '/tmp/hello.txt' ) ;
7170 console . log ( `Uploaded ${ uploadResult . filesTransferred } file(s), ${ uploadResult . bytesTransferred } bytes` ) ;
7271
7372 // Verify on remote
7473 const catResult = await vm . execute ( vmId , 'cat /tmp/hello.txt' ) ;
7574 console . log ( 'Remote content:' , catResult . stdout ) ;
7675
77- // Download file
76+ // Download the file back
7877 const downloadPath = path . join ( testDir , 'downloaded.txt' ) ;
7978 const downloadResult = await vm . download ( vmId , '/tmp/hello.txt' , downloadPath ) ;
8079 console . log (
8180 `Downloaded ${ downloadResult . filesTransferred } file(s), ${ downloadResult . bytesTransferred } bytes` ,
8281 ) ;
8382
84- // Verify downloaded content
8583 const downloadedContent = await fs . readFile ( downloadPath , 'utf8' ) ;
8684 console . log ( 'Downloaded content:' , downloadedContent ) ;
8785
86+ // ============================================
87+ // Example 3b: Upload and download directories
88+ // ============================================
89+ console . log ( '\n--- SFTP Directory Transfer ---' ) ;
90+
91+ // Create a local directory structure
92+ const appDir = path . join ( testDir , 'myapp' ) ;
93+ await fs . mkdir ( path . join ( appDir , 'src' ) , { recursive : true } ) ;
94+ await fs . mkdir ( path . join ( appDir , 'config' ) , { recursive : true } ) ;
95+ await fs . writeFile ( path . join ( appDir , 'package.json' ) , '{"name": "myapp", "version": "1.0.0"}' ) ;
96+ await fs . writeFile ( path . join ( appDir , 'src' , 'index.js' ) , 'console.log("Hello!");' ) ;
97+ await fs . writeFile ( path . join ( appDir , 'config' , 'settings.json' ) , '{"debug": true}' ) ;
98+
99+ // Upload entire directory with progress callback
100+ console . log ( 'Uploading directory...' ) ;
101+ const dirUploadResult = await vm . upload ( vmId , appDir , '/opt/myapp' , {
102+ recursive : true ,
103+ onProgress : ( transferred , total , filename ) => {
104+ console . log ( ` Progress: ${ filename } - ${ transferred } /${ total } bytes` ) ;
105+ } ,
106+ } ) ;
107+ console . log (
108+ `Uploaded ${ dirUploadResult . filesTransferred } files, ${ dirUploadResult . bytesTransferred } bytes` ,
109+ ) ;
110+
111+ // Verify directory structure on remote
112+ const lsResult = await vm . execute ( vmId , 'find /opt/myapp -type f' ) ;
113+ console . log ( 'Remote files:' , lsResult . stdout ) ;
114+
115+ // Download directory back
116+ const downloadDir = path . join ( testDir , 'downloaded-app' ) ;
117+ console . log ( 'Downloading directory...' ) ;
118+ const dirDownloadResult = await vm . download ( vmId , '/opt/myapp' , downloadDir , {
119+ recursive : true ,
120+ onProgress : ( transferred , total , filename ) => {
121+ console . log ( ` Progress: ${ filename } - ${ transferred } /${ total } bytes` ) ;
122+ } ,
123+ } ) ;
124+ console . log (
125+ `Downloaded ${ dirDownloadResult . filesTransferred } files, ${ dirDownloadResult . bytesTransferred } bytes` ,
126+ ) ;
127+
128+ // Verify downloaded files
129+ const localFiles = await fs . readdir ( downloadDir , { recursive : true } ) ;
130+ console . log ( 'Downloaded files:' , localFiles ) ;
131+
88132 // Cleanup
89133 await fs . rm ( testDir , { recursive : true } ) ;
90134
0 commit comments