Rust SDK for MEGA.nz cloud storage.
Overview · Capabilities · Installation · Quickstart · Examples · API Notes · Resources
megalib is an async Rust client library for working with MEGA.nz accounts, files, folders, shares, and public links. It is built as a Rust port of the MEGA C++ SDK and exposes high-level SessionHandle APIs for authenticated account operations plus public-link helpers for unauthenticated downloads and folder browsing.
Note
This is an unofficial client library and is not affiliated with, endorsed by, or supported by Mega Limited.
- Authentication and accounts: login, proxy login, registration, email verification, password changes, session loading, account info, and quota lookup.
- Cached node tree:
fetch_nodes, root discovery, children by handle, child lookup by name, descendant traversal, contact roots, and inshare/outshare queries. - Filesystem operations: stat, create folder, move, rename, remove, export, and share operations through node-first APIs or explicit path-based compatibility aliases.
- Transfers: file upload/download, resumable upload/download examples, byte and reader uploads, progress callbacks, parallel transfer workers, and HTTP/HTTPS/SOCKS5 proxy support.
- Public access: parse MEGA links, inspect and download public files, export account files, and open public folders without logging in.
- Optional previews: compile with the
previewfeature and callenable_previews(true)to generate upload thumbnails for supported media.
Add megalib and an async runtime to your Cargo.toml:
[dependencies]
megalib = "0.11.0"
tokio = { version = "1", features = ["full"] }For upload thumbnail generation:
[dependencies]
megalib = { version = "0.11.0", features = ["preview"] }
tokio = { version = "1", features = ["full"] }Log in, populate the cached node tree, and list the Cloud Drive root:
use megalib::{NodeType, SessionHandle};
#[tokio::main]
async fn main() -> megalib::Result<()> {
let session = SessionHandle::login("user@example.com", "password").await?;
session.fetch_nodes().await?;
let root = session
.root_nodes()
.await?
.into_iter()
.find(|node| node.node_type == NodeType::Root)
.expect("missing Cloud Drive root");
for node in session.children_by_handle(&root.handle).await? {
println!("{} ({:?})", node.name, node.node_type);
}
if let Some(documents) = session.child_node_by_name(&root, "Documents").await? {
println!("Documents handle: {}", documents.handle);
}
Ok(())
}Tip
New code should prefer node-first APIs after fetch_nodes(). Path-based helpers are still available as explicit compatibility aliases such as list_by_path, stat_by_path, mkdir_by_path, mv_by_path, rename_by_path, rm_by_path, upload_by_path, upload_resumable_by_path, export_by_path, and share_folder_by_path.
Each example is a runnable binary under examples/. Use --help to inspect the exact flags for any example:
cargo run --example login -- --helpCommon workflows:
# Authentication and browsing
cargo run --example login -- --email you@example.com --password "your-password"
cargo run --example node_api -- --email you@example.com --password "your-password"
cargo run --example ls -- --email you@example.com --password "your-password" --path /Root
cargo run --example cached_session -- --email you@example.com --password "your-password"
# Upload and download
cargo run --example upload -- --email you@example.com --password "your-password" ./local-file.txt /Root
cargo run --example download -- --email you@example.com --password "your-password" /Root/remote-file.txt ./downloaded-file.txt
cargo run --example upload_resume -- --email you@example.com --password "your-password" ./large-file.bin /Root
cargo run --example download_resume -- --email you@example.com --password "your-password" /Root/large-file.bin ./large-file.bin
# Filesystem operations
cargo run --example stat -- --email you@example.com --password "your-password" --path /Root/file.txt
cargo run --example mkdir -- --email you@example.com --password "your-password" /Root/new-folder
cargo run --example rename -- --email you@example.com --password "your-password" /Root/file.txt file-renamed.txt
cargo run --example mv -- --email you@example.com --password "your-password" /Root/file.txt /Root/destination-folder
cargo run --example rm -- --email you@example.com --password "your-password" /Root/old-file.txt
# Sharing and public links
cargo run --example export -- --email you@example.com --password "your-password" --path /Root/file.txt
cargo run --example share -- --email you@example.com --password "your-password" --folder /Root/shared --recipient friend@example.com --level 0
cargo run --example folder -- "https://mega.nz/folder/<FOLDER_ID>#<KEY>"
cargo run --example download_public -- "https://mega.nz/file/<FILE_ID>#<KEY>" ./public-file.bin
# Account management
cargo run --example register -- --email you@example.com --password "your-password" --name "Your Name"
cargo run --example verify -- --state "SESSION_KEY_FROM_STEP_1" --link "CONFIRMATION_LINK_OR_FRAGMENT"
cargo run --example passwd -- --email you@example.com --password "current-password" --new "new-password"Authenticated examples that define --proxy accept proxy URLs such as http://proxy:8080 or socks5://proxy:1080.
SessionHandle::fetch_nodes()populates the cached tree used by node-first APIs and path compatibility aliases.- Cloud Drive paths are rooted at
/Root; public folder paths are rooted at the shared folder name. - Deprecated path-first canonical names remain for compatibility, but new code should use the explicit
*_by_pathaliases or node-first methods. - Public file helpers in
megalib::publiccan inspect and download MEGA links without an authenticated session. - Video thumbnails with the
previewfeature requireffmpegthumbnailerto be installed and available onPATH.