Sample code for 30+ languages & platforms
Rust

SFTP Upload and Download to a StringBuilder Object

See more SFTP Examples

Demonstrates how to SFTP upload from a Chilkat StringBuilder object, and download into a StringBuilder object.

Chilkat Rust Downloads

Rust
let mut success = false;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let sftp = chilkat::SFtp::new();

// Set some timeouts, in milliseconds:
sftp.set_connect_timeout_ms(5000);
sftp.set_idle_timeout_ms(10000);

// Connect to the SSH server and then authenticate.
let port = 22;
success = sftp.connect("MY-SSH-SERVER-DOMAIN-OR-IP", port).is_ok();
if success {
    success = sftp.authenticate_pw("MY-SSH-LOGIN", "MY-SSH-PASSWORD").is_ok();
    if success {
        success = sftp.initialize_sftp().is_ok();
    }

}

if !success {
    println!("{}", sftp.last_error_text());
    return;
}

let sb_a = chilkat::StringBuilder::new();
let _ = sb_a.load_file("qa_data/hamlet.xml", "utf-8");

// Upload the contents of sbA to the SSH/SFTP server.
let b_include_bom = false;
let remote_path = "sftpTesting/hamlet.xml".to_string();
if sftp.upload_sb(&sb_a, &remote_path, "utf-8", b_include_bom).is_err() {
    println!("{}", sftp.last_error_text());
    return;
}

// Download the file..
let sb_b = chilkat::StringBuilder::new();
if sftp.download_sb(&remote_path, "utf-8", &sb_b).is_err() {
    println!("{}", sftp.last_error_text());
    return;
}

// Verify that sbA and sbB have the exact same contents.
println!("size of sbA: {}", sb_a.length());
let b_case_sensitive = true;
if sb_a.contents_equal_sb(&sb_b, b_case_sensitive) {
    println!("Contents are equal. Success.");
} else {
    println!("Contents are NOT equal.  Failed.");
}

sftp.disconnect();