SiaSia Developer Portal

Track Progress

Both UploadOptions and DownloadOptions accept a per-shard callback. Each invocation receives a ShardProgress; sum shard_size across invocations for the running byte total. In the Rust, Go and Python SDKs the callback can run on several threads at once, so those examples hold a lock while they add to the total and print it. Otherwise lines can print out of order, and in Python an update can be lost.

Upload

The upload callback fires for every shard the SDK pushes — data shards plus parity shards. With the default erasure coding that's roughly 3× the raw input, so use encoded_size(raw_size, data_shards, parity_shards) as the denominator, not the raw size. In the Rust, Python, Dart and JavaScript SDKs, a shard can be reported twice: if the indexer rejects a slab because its upload took too long to pin, the SDK uploads the slab again. Without the cap in the examples below, the total can end above encoded_size and the percentage above 100%.

rust
use std::sync::{Arc, Mutex}; use sia_storage::{Object, ShardProgress, UploadOptions, encoded_size}; let file = tokio::fs::File::open("example.bin").await?; let raw_size = file.metadata().await?.len(); let opts = UploadOptions::default(); let total = encoded_size(raw_size, opts.data_shards, opts.parity_shards); let uploaded = Arc::new(Mutex::new(0u64)); let cb = uploaded.clone(); let opts = opts.on_shard_uploaded(move |p: ShardProgress| { // Hold the lock while printing so the lines come out in order. let mut n = cb.lock().unwrap(); *n += p.shard_size as u64; println!("uploaded {n} / {total} bytes ({:.1}%)", (100.0 * *n as f64 / total as f64).min(100.0)); }); let obj = sdk.upload(Object::default(), file, opts).await?; sdk.pin_object(&obj).await?;

Download

The download callback fires for each shard the SDK uses to rebuild a chunk. The SDK requests more shards than it needs so it doesn't wait on slow hosts, but it reports only the ones it uses, so the byte total tracks the object's raw size. Use obj.size() for the denominator. Each shard read is rounded up, so without the cap in the examples below, the total ends slightly above obj.size() (264,320 bytes for a 262,144-byte object in testing) and the percentage slightly above 100%.

rust
use std::sync::{Arc, Mutex}; use sia_storage::{DownloadOptions, ShardProgress}; let total = obj.size(); let downloaded = Arc::new(Mutex::new(0u64)); let cb = downloaded.clone(); let opts = DownloadOptions::default().on_shard_downloaded(move |p: ShardProgress| { // Hold the lock while printing so the lines come out in order. let mut n = cb.lock().unwrap(); *n += p.shard_size as u64; println!("downloaded {n} / {total} bytes ({:.1}%)", (100.0 * *n as f64 / total as f64).min(100.0)); }); let mut reader = sdk.download(&obj, opts)?; let mut bytes = Vec::new(); tokio::io::copy(&mut reader, &mut bytes).await?;