【发布时间】:2019-02-10 06:44:27
【问题描述】:
我希望能够在我的 Go 程序中使用 Go-IPFS,但它完全没有文档记录。这就是我的研究引导我的地方:
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreunix"
)
func main() {
ctx := context.Background()
node, err := core.NewNode(ctx, &core.BuildCfg{})
if err != nil {
log.Fatalf("Failed to start IPFS node: %v", err)
}
reader, err := coreunix.Cat(ctx, node, "QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB")
if err != nil {
log.Fatalf("Failed to look up IPFS welcome page: %v", err)
}
blob, err := ioutil.ReadAll(reader)
if err != nil {
log.Fatalf("Failed to retrieve IPFS welcome page: %v", err)
}
fmt.Println(string(blob))
}
但是我不确定
context.Background() VS context.TODO() VS context.WithCancel(context.Background())。
最重要的是,如何选择 IPFS 将 IPFS 存储库放在哪里并确保它也初始化它?
如何启用和使用 Pubsub 及其命令 subscribe 和 publish?
如何将文件添加和固定到 IPFS,同时还可以为大文件输入流?
coreunix.Cat 是否也适合通过流读取文件?
当您从 CLI 运行 ipfs 守护程序并在所有端口(如 webui、swarm 等)上运行所有内容时,如何让节点“监听”?
这样添加文件怎么样?这是否使用流或将整个文件读入内存?如何改进?
func addFile(ctx *context.Context, node *core.IpfsNode, path *string) error {
file, err := os.Open(*path)
if err != nil {
return err
}
adder, err := coreunix.NewAdder(*ctx, node.Pinning, node.Blockstore, node.DAG)
if err != nil {
return err
}
filename := filepath.Base(*path)
fileReader := files.NewReaderFile(filename, filename, file, nil)
adder.AddFile(fileReader)
adder.PinRoot()
return nil
}
【问题讨论】:
-
我收到了
"Unresolved reference 'Cat'"v0.11.0