example of adding, pinning, and unpinning using IPFS as library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "log" | |
| "net" | |
| core "github.com/ipfs/go-ipfs/core" | |
| corerepo "github.com/ipfs/go-ipfs/core/corerepo" | |
| coreunix "github.com/ipfs/go-ipfs/core/coreunix" | |
| ) | |
| func main() { | |
| ctx, cancel := context.WithCancel(context.Background()) | |
| defer cancel() | |
| nd, err := core.NewNode(ctx, &core.BuildCfg{}) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| list, err := net.Listen("tcp", ":0") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| log.Println("listening on: ", list.Addr()) | |
| // add directory | |
| hash, err := coreunix.AddR(nd, "test_directory") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| log.Println("added directory with multihash: ", hash) | |
| // pin a hash | |
| pinlist, err := corerepo.Pin(nd, ctx, []string{hash}, true) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| log.Println("pinned cid: ", pinlist) | |
| // unpin a hash | |
| unpinlist, err := corerepo.Unpin(nd, ctx, []string{hash}, true) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| log.Println("unpinned cid: ", unpinlist) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment