Skip to main content

upload_file/
upload_file.rs

1use baidu_netdisk_sdk::BaiduNetDiskClient;
2use log::info;
3
4#[tokio::main]
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6    env_logger::init();
7
8    let client = BaiduNetDiskClient::builder().build()?;
9    info!("Client created successfully");
10
11    client.load_token_from_env()?;
12    info!("Token loaded successfully");
13
14    let args: Vec<String> = std::env::args().collect();
15
16    if args.len() < 3 {
17        println!("Usage: {} <local_file> <remote_path>", args[0]);
18        println!("Example: {} test.txt /apps/test/test.txt", args[0]);
19        return Ok(());
20    }
21
22    let local_file = &args[1];
23    let remote_path = &args[2];
24
25    println!("=== Baidu NetDisk File Upload (Simple) ===");
26    println!("Local file: {}", local_file);
27    println!("Remote path: {}", remote_path);
28    println!();
29
30    let start_time = std::time::Instant::now();
31
32    let response = client.upload().upload_file(local_file, remote_path).await?;
33
34    println!("File uploaded successfully!");
35    println!("  FS ID: {}", response.fs_id);
36    println!("  Server filename: {:?}", response.server_filename);
37    println!("  Path: {}", response.path);
38    println!("  Size: {} bytes", response.size);
39    println!("  Category: {}", response.category);
40    println!("  MD5: {}", response.md5.unwrap_or_default());
41    println!("  Upload time: {:?}", start_time.elapsed());
42
43    Ok(())
44}