当前位置:网站首页>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)
}
边栏推荐
猜你喜欢

Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!

canal同步Mariadb到Mysql

KICAD 拉线宽度无法修改,解决方法

永磁同步电机36问(二)——机械量与电物理量如何转化?

esp32经典蓝牙和单片机连接,,,手机蓝牙作为主机

240...循迹

Software testing Interface automation testing Pytest framework encapsulates requests library Encapsulates unified request and multiple base path processing Interface association encapsulation Test cas
![[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline](/img/08/d693c7e2fff8343b55ff3c1f9317c6.jpg)
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline

2022牛客多校三_F G

四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料
随机推荐
Curriculum Vitae;CV
列表常用方法
openGauss切换后state状态显示不对
Remember a pit for gorm initialization
使用docker安装mysql
2022年NPDP考完多久出成绩?怎么查询?
数值积分方法:欧拉积分、中点积分和龙格-库塔法积分
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
Service discovery of kubernetes
简单的页面跳转活动
MySQL - CRUD operations
to-be-read list
qt点云配准软件
TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
字符串常用方法
【web】Understanding Cookie and Session Mechanism
NAS和私有云盘的区别?1篇文章说清楚
Unable to log in to the Westward Journey
架构:微服务网关(SIA-Gateway)简介