Easily encode your content and add, get, and cat to your heart's content
Connect to the global IPFS network and discover content via bootstrap peers
If your application already uses the IPFS HTTP APIs, you can easily switch to Rust IPFS
First, add ipfs
and tokio
to your Cargo.toml file:
[dependencies] ipfs = { git = "https://github.com/rs-ipfs/rust-ipfs" } tokio = { version = "1", features = ["full"] } tokio-stream = "0.1"
Then, in your src/main.rs:
use ipfs::{Ipfs, IpfsOptions, IpfsPath, TestTypes, UninitializedIpfs}; use std::process::exit; use tokio::io::AsyncWriteExt; use tokio_stream::StreamExt; #[tokio::main] async fn main() { // Initialize an in-memory repo and start a daemon. let opts = IpfsOptions::inmemory_with_generated_keys(); let (ipfs, fut): (Ipfs<TestTypes>, _) = UninitializedIpfs::new(opts).start().await.unwrap(); // Spawn the background task tokio::task::spawn(fut); // Restore the default bootstrappers to enable content discovery ipfs.restore_bootstrappers().await.unwrap(); // Get the IPFS README let path = "/ipfs/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc/readme" .parse::<IpfsPath>() .unwrap(); let stream = ipfs.cat_unixfs(path, None).await.unwrap_or_else(|e| { eprintln!("Error: {}", e); exit(1); }); tokio::pin!(stream); let mut stdout = tokio::io::stdout(); loop { match stream.next().await { Some(Ok(bytes)) => { stdout.write_all(&bytes).await.unwrap(); } Some(Err(e)) => { eprintln!("Error: {}", e); exit(1); } None => break, } } }▲
These are some extra resources to get you started with specific use-cases of Rust IPFS:
Rust IPFS is supported by Equilibrium, Protocol Labs, the Web3 Foundation, and YOU. If you find this work useful, please consider becoming a financial contributor on OpenCollective.