7 stable releases

6.0.0 Nov 4, 2021
5.1.0 Nov 4, 2021
5.0.0 Dec 30, 2020
4.0.4 Nov 7, 2020
4.0.1 Jun 3, 2020

#1865 in Network programming

Download history 288/week @ 2024-01-01 313/week @ 2024-01-08 343/week @ 2024-01-15 361/week @ 2024-01-22 231/week @ 2024-01-29 240/week @ 2024-02-05 260/week @ 2024-02-12 329/week @ 2024-02-19 203/week @ 2024-02-26 279/week @ 2024-03-04 300/week @ 2024-03-11 247/week @ 2024-03-18 286/week @ 2024-03-25 281/week @ 2024-04-01 255/week @ 2024-04-08 184/week @ 2024-04-15

1,044 downloads per month
Used in 7 crates

Apache-2.0/MIT

42KB
660 lines

rust-ftp

FTP client for Rust

Number of Crate Downloads Crate Version Crate License

Documentation

Installation

FTPS support is disabled by default. To enable it secure should be activated in Cargo.toml.

[dependencies]
async_ftp = { version = "<version>", features = ["secure"] }

Usage

use std::str;
use std::io::Cursor;
use async_ftp::FtpStream;

async fn async_main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a connection to an FTP server and authenticate to it.
    let mut ftp_stream = FtpStream::connect("172.25.82.139:21").await?;
    let _ = ftp_stream.login("username", "password").await?;

    // Get the current directory that the client will be reading from and writing to.
    println!("Current directory: {}", ftp_stream.pwd().await?);
    
    // Change into a new directory, relative to the one we are currently in.
    let _ = ftp_stream.cwd("test_data").await?;

    // Retrieve (GET) a file from the FTP server in the current working directory.
    let remote_file = ftp_stream.simple_retr("ftpext-charter.txt").await?;
    println!("Read file with contents\n{}\n", str::from_utf8(&remote_file.into_inner()).await?);

    // Store (PUT) a file from the client to the current working directory of the server.
    let mut reader = Cursor::new("Hello from the Rust \"ftp\" crate!".as_bytes());
    let _ = ftp_stream.put("greeting.txt", &mut reader).await?;
    println!("Successfully wrote greeting.txt");

    // Terminate the connection to the server.
    let _ = ftp_stream.quit();
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    tokio::runtime::Builder::new()
        .threaded_scheduler()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async_main())
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Development environment

All you need to develop rust-ftp and run the tests is Rust and Docker. The tests folder contains a Dockerfile that installs and configures the vsftpd server.

To create the Docker image:

docker build -t ftp-server tests

To start the FTP server that is tested against:

tests/ftp-server.sh

This script runs the ftp-server image in detached mode and starts the vsftpd daemon. It binds ports 21 (FTP) as well as the range 65000-65010 for passive connections.

Once you have an instance running, to run tests type:

cargo test

The following commands can be useful:

# List running containers of ftp-server image
# (to include stopped containers use -a option)
docker ps --filter ancestor=ftp-server

# To stop and remove a container
docker stop container_name
docker rm container_name

# To remove the image
docker rmi ftp-server

Dependencies

~6–21MB
~257K SLoC