当前位置:网站首页>IPFS部署及文件上传(golang)
IPFS部署及文件上传(golang)
2022-08-02 02:34:00 【Asimov__】
IPFS节点部署
下载
wget https://dist.ipfs.io/kubo/v0.14.0/kubo_v0.14.0_linux-amd64.tar.gz
解压
tar xvfz kubo_v0.14.0_linux-amd64.tar.gz
安装
cd kubo
./install.sh
初始化
ipfs init
查看初始化后返回的节点信息
ipfs cat /ipfs/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc/readme
配置ipfs跨域
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT","GET", "POST", "OPTIONS"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials '["true"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Headers '["Authorization"]'
ipfs config --json API.HTTPHeaders.Access-Control-Expose-Headers '["Location"]'
配置IPFS外网访问控制台
cd ~/.ipfs
vi config
把127.0.0.1修改成0.0.0.0
IPFS端口说明
4001 主要端口,进行p2p连接和同步数据
5001 ipfs的api端口,管理页面的端口,可以进行数据的读写
8080 ipfs gateway端口,用于读取ipfs节点数据,默认只读
如果使用防火墙,记得把这几个端口开放,否则外网还是无法访问
启动IPFS
nohup ipfs daemon >> ./log/nohup`date +%Y-%m-%d`.out 2>&1 &
查看连接节点
ipfs swarm peers
浏览器访问 http://localhost:5001/webui (管理页面,文件上传等)会重定向到自己节点
文件上传代码
package main
import (
"bytes"
"encoding/json"
"fmt"
shell "github.com/ipfs/go-ipfs-api"
"io/ioutil"
"log"
"os"
)
func Read(filepath string) []byte {
f, err := os.Open(filepath)
if err != nil {
log.Println("read file fail", err)
return nil
}
defer f.Close()
fd, err := ioutil.ReadAll(f)
if err != nil {
log.Println("read to fd fail", err)
return nil
}
return fd
}
func UploadIPFS(raw []byte) (string, error) {
sh := shell.NewShell("localhost:5001")
reader := bytes.NewReader(raw)
// https://github.com/ipfs/go-ipfs-api/blob/master/add.go
fileHash, err := sh.Add(reader)
if err != nil {
return "", err
}
fmt.Println(fileHash)
return fileHash, nil
}
func WriteHash(writeJson string, cont interface{}) {
//
if distFile, err := os.OpenFile(writeJson, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666); err != nil {
log.Println("create file failed", err)
} else {
enc := json.NewEncoder(distFile)
if err1 := enc.Encode(cont); err1 != nil {
log.Println("write failed", err1)
} else {
log.Println("write successful")
}
}
}
func main() {
hashMap := make(map[int]string, 10000)
for i := 0; i < 10000; i++ {
file := fmt.Sprintf("./greencard/green_%d.gif", i)
raw := Read(file)
if raw != nil {
hash, err := UploadIPFS(raw)
if err != nil {
log.Println("UploadIPFS err", err)
} else {
hashMap[i] = fmt.Sprintf("https://ipfs.io/ipfs/%s?filename=%s", hash, hash)
}
log.Println("hash", hash)
}
}
WriteHash("hash.json", hashMap)
}
边栏推荐
猜你喜欢

ros多客户端请求服务

A good book for newcomers to the workplace

2022河南青训联赛第(三)场

The principle and code implementation of intelligent follower robot in the actual combat of innovative projects

ofstream,ifstream,fstream read and write files

Docker-compose安装mysql

yaml

EasyGBS平台播放视频时偶尔出现播放失败是什么原因?

Analysis of the status quo of digital transformation of manufacturing enterprises

Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
随机推荐
Power button 1374. Generate each character string is an odd number
Analysis of the status quo of digital transformation of manufacturing enterprises
优炫数据库导库导错了能恢复吗?
cocos中使用async await异步加载资源
数仓:数仓从ETL到ELT架构的转化以及俩者的区别
Redis for distributed applications in Golang
The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
A good book for newcomers to the workplace
网络层解析——IP协议、地址管理、路由选择
NIO‘s Sword(牛客多校赛)
Chapter 7 Noise analysis
详解最强分布式锁工具:Redisson
JVM调优实战
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
canal同步Mariadb到Mysql
灰度传感器、、、diy原理。。图
2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
2022年NPDP考完多久出成绩?怎么查询?
2022-08-01 Install mysql monitoring tool phhMyAdmin