1. 项目地址:
    github.com/ipfs/go-ipfs-api

  2. 示例代码:

     package main
     import (
         "bufio"
         "bytes"
         "fmt"
         shell "github.com/ipfs/go-ipfs-api"
         "io/ioutil"
         "os"
     )
     var sh *shell.Shell
     func main() {
         sh = shell.NewShell("http://10.0.0.11:5001")
         // 添加字符串
         hashStr, err := sh.Add(bytes.NewBufferString("hello world"))
         if err != nil {
             fmt.Println("添加字符串失败:", err)
         }
         fmt.Println("hashStr:", hashStr)
         // 上传文件
         file, err := os.Open("test.txt")
         hashFile, err := sh.Add(bufio.NewReader(file))
         if err != nil {
             fmt.Println("上传文件失败:", err)
         }
         fmt.Println(hashFile)
         // 上传目录
         hashDir, err := sh.AddDir("tmp")
         if err != nil {
             fmt.Println("上传目录失败:", err)
         }
         fmt.Println("hashDir:", hashDir)
         // 查看hash对应的内容
         read, err := sh.Cat(hashFile)
         if err != nil {
             fmt.Println("查看hash对应的内容失败:", err)
         }
         body, err := ioutil.ReadAll(read)
         fmt.Println("body:", string(body))
     }
文档更新时间: 2024-03-24 15:25   作者:lee