【问题标题】:Using Go-IPFS Programmatically以编程方式使用 Go-IPFS
【发布时间】: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 及其命令 subscribepublish

如何将文件添加和固定到 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

标签: go ipfs


【解决方案1】:

你可能想把你的问题分解成更小的部分,我一直在玩 go-ipfs 的源代码,这是我可以给你的一般说明:

  1. 大多数数据结构,如上下文、DAG、IPFSNode 等都是以 go 结构的形式定义的,您应该能够在 gx/.../... 目录中找到它们,您也应该在该目录中找到它们能够查看有关使用的每个变量的详细信息(通过目录进行简单的字符串搜索应该可以找到所需的源文件)
  2. 所有方法都定义在文件夹 github.com/../.. 目录中
  3. 清除指针的概念,因为它们大部分时间都使用指针将参数传递给函数

【讨论】:

    猜你喜欢
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2021-08-08
    • 1970-01-01
    • 2012-01-25
    • 2012-06-15
    • 2014-09-17
    相关资源
    最近更新 更多