Skip to content

Instantly share code, notes, and snippets.

@SeanChao
Last active June 8, 2021 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeanChao/8c93372d1b6ac12faa27b12794d53d08 to your computer and use it in GitHub Desktop.
Save SeanChao/8c93372d1b6ac12faa27b12794d53d08 to your computer and use it in GitHub Desktop.
Preliminary Plan for go-storage-ipfs

技术方案

背景

go-storage 是用 Golang 实现的抽象存储模块,支持各种存储服务。本方案的目的是为实现 go-storage 对 IPFS 的适配。go-storage 已经支持了较多的存储服务,例如 azblob, cos, dropbox 等。

go-service-example 是扩展 go-storage 的模板仓库,可以在此基础上进行修改以适配 IPFS。

go-ipfs 是 IPFS 的官方 Go 实现,可以作为依赖库集成到其它 Go 项目中。

实现方案

IPFS Naming Scheme

IPFS 的 API 实现文档中给出了 <ipfs-path> 的命名规则。

主要的类型有以下三种:一般 IPFS 路径,形如 /ipfs/<cid>/..., 另外两种路径为基于 IPNS 的类型,形如 /ipns/<hash>/..., ipns/<domain>/... .

IPFS Hierarchy

IPFS 中存在文件和目录的概念。

IPFS 里的文件是一个不可变对象,通过文件内容哈希得到的 CID 唯一识别。

IPFS 的目录的 CID 由该目录下的所有文件的 CID 决定。

Storager 基本操作

在项目的实现计划中,将需要实现的操作分为了三类:元数据操作、只读操作、写操作。

元数据操作:Metadata , String

只读操作:List, Read

写操作:Create, Delete, Write

只读操作语义比较明确。由于 IPFS 为按内容寻址的,因此对于写操作的语义需要在实现前进一步讨论并定义。

SDK

go-ipfs 是 IPFS 的官方 Go 实现,可以作为依赖库集成到其它 Go 项目中。官方文档中给出了以下集成方式:

  • 直接调用 go-ipfs
  • 在本机运行一个 go-ipfs 守护进程,通过 HTTP 与该进程通信

go-ipfs 初始化可以参照官方示例进行实现。

go-ipfs 作为库使用时,其中的 [CoreAPI](<https://github.com/ipfs/go-ipfs/blob/master/core/coreapi/coreapi.go>) 为暴露给外界的公共接口。其中的 Unixfs 函数提供了 Unix 文件系统风格的 UnixfsAPI 接口,有 Add, Get, Ls 等函数可以调用。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment