Overview
TheFTPClient class handles connections to FTP servers using Cloudflare Workers’ TCP Sockets API. You can connect with basic authentication or secure your connection with TLS/FTPS.
Basic connection
To connect to an FTP server, create anFTPClient instance and call the connect() method:
Call connect()
The
connect() method establishes the connection, performs feature discovery, and authenticates.Connection options
You can customize the connection behavior by passing aConnectionOptions object:
Username for authentication. Defaults to
anonymous if not specified.Password for authentication. Defaults to
anonymous if not specified.FTP server port. Standard FTP uses port 21.
Enable TLS/FTPS with STARTTLS. When
true, the client upgrades the connection to TLS.Port for active mode data connections.
IP address for active mode data connections.
Use IPv6 for active mode connections.
Secure connections (TLS/FTPS)
To establish a secure connection using TLS, set thesecure option to true:
How TLS works
When you enable thesecure option, the client:
Connection lifecycle
Theconnect() method performs several operations automatically:
- Connection sequence
- Implementation
Feature discovery
During connection, the client automatically discovers server features using theFEAT command. This determines which advanced features are available:
- MLST/MLSD - Machine-readable directory listings
- AUTH TLS - TLS authentication methods
- PROT - Data channel protection
- MDTM - File modification time
- REST - Resume capability
- EPSV - Extended passive mode
The client stores discovered features internally and uses them to optimize operations. For example, if
MLST is available, the stat() method will use it for detailed file information.Closing connections
Always close your connection when you’re done to avoid resource leaks:close() method terminates both control and data connections.
Example: Complete connection workflow
Next steps
File operations
Upload, download, and manage files
Directory operations
Navigate and manage directories
Security
Best practices for secure FTP
Streaming
Stream large files efficiently