Connecting to SFTP
While not strictly required, you can explicitly connect to the SFTP subsystem:The SFTP connection is established automatically when you call any SFTP method (like
sftpLs(), sftpUpload(), etc.), so calling connectSFTP() explicitly is optional.Listing Directories
UsesftpLs() to list files and directories:
Understanding the Response
ThesftpLs() method returns an array of LsResult objects with detailed file information:
Practical Examples
- List Current Directory
- Filter Files
- Sort Results
- Format Output
Creating Directories
Create a new directory on the remote server:Creating Nested Directories
To create nested directories, you need to create each level individually:Removing Directories
Remove an empty directory:Removing Directory Recursively
Here’s a helper function to remove a directory and all its contents:File Operations
Removing Files
Delete a single file:Renaming Files and Directories
Rename or move files and directories:Changing File Permissions (Android Only)
The
sftpChmod() method is only available on Android.Common Permission Values
Error Handling
All SFTP operations return promises that reject on error:Disconnecting SFTP
Explicitly close the SFTP connection:- Disconnecting SFTP will unregister upload/download progress listeners
- The SSH connection remains active after disconnecting SFTP
- On iOS,
disconnectSFTP()has limited functionality; usedisconnect()to close both SSH and SFTP
Best Practices
1
Check before operations
Use
sftpLs() to verify paths exist before performing operations:2
Handle errors gracefully
Different errors require different handling strategies:
3
Use absolute paths
Always use absolute paths when possible to avoid confusion:
Next Steps
- Learn about file uploads and downloads with progress tracking
- Explore SSH command execution for system operations
- Check platform-specific considerations for iOS and Android differences