ipfs

目标

  • ipfs 安装
  • ipfs command
  • go-ipfs-api
  • ipfs test

ipfs 安装

IPFS官网:https://ipfs.io/
目前ipfs主要有俩个版本go-ipfsjs-ipfs
本文使用的是go-ipfs
1、通过源码自己编译安装
2、直接从下载Releases下载对应版本

//查看对应版本,当前最新版本为 v0.5.0-dev
ipfs version

ipfs command

ipfs 基本命令
ipfs bitswap 用来操作bitswap代理
ipfs block 命令用来操作IPFS裸块
ipfs bootstrap 命令用来显示或编辑引导节点列表
ipfs config [] 命令族用来读取或写入ipfs的配置信息
ipfs diag 命令生成诊断报告
ipfs file 命令用来与表征unix文件系统的IPFS对象进行交互
ipfs files 命令用来操作unixfs文件
ipfs xxxx --help 查看各个命令的帮助,
ipfs block --help
ipfs files --help

其他命令参考
http://cw.hubwiz.com/card/c/ipfs/1/1/1/

//初始化 ipfs
ipfs init
//启动 ipfs 本地节点 (启动成功后可以通过http://localhost:5001/webui)
ipfs daemon
//删除所有节点
ipfs bootstrap rm all
//加入私网,也可以通过设置环境变量 LIBP2P_FORCE_PNET=1 来强行启用私网配置,如果没有正确配置 swarm.key 会导致 daemon 启动失败
//生成swarm.key 参考 https://www.jianshu.com/p/dfc61136324c
ipfs bootstrap add <Address>
//显示bitswap代理的诊断信息
ipfs stats bitswap
//上传文件
ipfs add QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH
//上传文件夹 -r=递归
ipfs add -r <dir>
//保存ipfs网络数据
ipfs get QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH
//查看内容
ipfs cat QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH
ipfs cat /ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH
https://ipfs.io/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200313220329761.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2R1eWF5dW4=,size_16,color_FFFFFF,t_70)

go-ipfs-api

https://github.com/ipfs/go-ipfs
所有的api都有对应的命令行,源码中构造命令行的对象
在这里插入图片描述
Shell 实际是就是一个httpClient
在这里插入图片描述
AddAdd
Add方法中的-r,在这里表示recursiveAdd方法中的-r,在这里表示recursive

import (
	"bytes"
	"fmt"
	ipfsapi "github.com/ipfs/go-ipfs-api"
	"io/ioutil"
	"sync"
	"time"
)

const (
	url = "127.0.0.1:5001"
)

var sh *ipfsapi.Shell
var wg sync.WaitGroup

//上传内容
func UploadIPFS(str string) string {
	//defer wg.Done()
	sh = ipfsapi.NewShell(url)
	hash, err := sh.Add(bytes.NewBufferString(str))
	if err != nil {
		fmt.Println("上传ipfs时错误:", err)
	}
	return hash
}

//上传文件
func UploadIPFSFile(str string) string {
	defer wg.Done()
	sh = ipfsapi.NewShell(url)
	hash, err := sh.AddDir(str)
	if err != nil {
		fmt.Println("上传ipfs时错误:", err)
	}
	fmt.Printf("hash:%v\n",hash)
	return hash
}


//上传目录
func UploadIPFSDir(str string) string {
	defer wg.Done()
	sh = ipfsapi.NewShell(url)
	hash, err := sh.AddDir(str)
	if err != nil {
		fmt.Println("上传ipfs时错误:", err)
	}
	fmt.Printf("hash:%v\n",hash)
	return hash
}

//下载文件
func GetIPFS(hash string) (string,error) {
	defer wg.Done()
	downloadPath :="C:\\ipfsdownload\\"
	sh = ipfsapi.NewShell(url)
	err := sh.Get(hash,downloadPath)

	if err != nil {
		fmt.Println(err)
	}
	//path := fmt.Sprintf("",downloadPath,hash)
	return "",err
}


//查看文件
func LsIPFS()  {
	sh = ipfsapi.NewShell(url)

	ls ,err := sh.List("")
	fmt.Print(ls)

	if err != nil {
		fmt.Println(err)
	}
}


//查看文件
func CatIPFS(hash string) string {
	sh = ipfsapi.NewShell(url)

	read, err := sh.Cat(hash)
	sh.IsUp()
	if err != nil {
		fmt.Println(err)
	}
	body, err := ioutil.ReadAll(read)

	return string(body)
}

ipfs test

本地单机测试
硬件环境
在这里插入图片描述
测试结果
在这里插入图片描述
win 生成文件方法,1000000kb
fsutil file createnew F:\go\ipfs-api\testfile1M.txt 1000000

测试方法

func TestUploadIPFSFile(t *testing.T) {
	startTime := time.Now()
	for i := 0; i < 1; i++ {
		wg.Add(1)
		go UploadIPFSFile("F:\go\ipfs-api\testfile1M.txt");
	}
	wg.Wait()
	endTime := time.Since(startTime)
	fmt.Println("age: ", endTime)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值