import ( "fmt" "github.com/ipfs/go-ipfs-api" "os" ) func main() { sh := shell.NewShell("localhost:5001") // Start IPFS shell file, err := os.Open("myfile") // Open the file to be streamed if err != nil { fmt.Println(err) return } defer file.Close() reader := ipfs.NewFileReader(file) // Create a reader for the file hash, err := sh.Add(reader) // Add the file to IPFS if err != nil { fmt.Println(err) return } fmt.Println(hash) // Print the IPFS hash of the file }
import ( "fmt" "github.com/ipfs/go-ipfs-api" "os" ) func main() { sh := shell.NewShell("localhost:5001") // Start IPFS shell reader, err := sh.Cat("QmYm9TZDC8W25ixWx5kJp2KxHm6rxjTN5rEQzwEnbm3z4t") // Get the reader for the file from IPFS if err != nil { fmt.Println(err) return } defer reader.Close() file, err := os.Create("myfile") // Create the file to write to if err != nil { fmt.Println(err) return } defer file.Close() _, err = io.Copy(file, reader) // Copy the data from the IPFS reader to the file if err != nil { fmt.Println(err) return } fmt.Println("File saved!") }This package is a part of the go-ipfs library.