ipfs
ipfs
Purpose
IPFS (InterPlanetary File System) is a peer-to-peer protocol for storing and sharing files in a decentralized network, ensuring data persistence without central servers.
When to Use
Use IPFS when building applications that require distributed storage for resilience, such as blockchain data archiving, peer-to-peer file sharing, or content delivery in unreliable networks. Apply it for scenarios involving large files or censorship-resistant data, like NFT metadata storage or decentralized apps.
Key Capabilities
- Decentralized storage: Files are addressed by content hashes (e.g., CID like Qm...abc), enabling global access via any node.
- Peer discovery: Automatically connects to peers using DHT; configure with
ipfs config Addresses.Swarm --json '["/ip4/0.0.0.0/tcp/4001"]'. - Versioning: Supports IPLD for linking data structures; use
ipfs dag putto add a DAG node. - Encryption: Files can be encrypted via external tools; integrate with libsodium for symmetric encryption before adding files.
- Pinning: Manually pin files to local storage with
ipfs pin add <CID>to prevent garbage collection.
Usage Patterns
To add a file, run ipfs add path/to/file.txt from CLI, then reference it by CID. In code, use the Go IPFS library: import "github.com/ipfs/go-ipfs-api" and call sh := shell.NewShell("localhost:5001"); cid, err := sh.Add(strings.NewReader("data")). In scripts, wrap commands in try-catch for errors, e.g., check if the daemon is running with ipfs daemon first. For API calls, use HTTP endpoints like POST /api/v0/add with multipart form data.