当前位置:网站首页>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)
}
边栏推荐
- mysql 查看死锁
- 极大似然估计
- Service discovery of kubernetes
- 2022-08-01 安装mysql监控工具phhMyAdmin
- nacos startup error, the database has been configured, stand-alone startup
- Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
- Electronic Manufacturing Warehouse Barcode Management System Solution
- 【 wheeled odometer 】
- 拼多多借力消博会推动国内农产品品牌升级 看齐国际精品农货
- 很有意思的经历,很有意思的项目--文件夹对比工具
猜你喜欢

Use DBeaver for mysql data backup and recovery

IMU预积分的简单理解

CASE2023

ros多客户端请求服务

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

How engineers treat open source

极大似然估计

拼多多借力消博会推动国内农产品品牌升级 看齐国际精品农货

Analysis of the status quo of digital transformation of manufacturing enterprises

Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
随机推荐
esp32经典蓝牙和单片机连接,,,手机蓝牙作为主机
欧拉公式的证明
Qt自定义控件和模板分享
2022-08-01 反思
PHP live source code to achieve simple barrage effect related code
【web】理解 Cookie 和 Session 机制
【web】Understanding Cookie and Session Mechanism
2022-08-01 Reflection
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
to-be-read list
Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
罗德里格斯公式(Rodrigues‘ Rotation Formula)推导
十字光标太小怎么调节、CAD梦想画图算量技巧
详解最强分布式锁工具:Redisson
CASE2023
数值积分方法:欧拉积分、中点积分和龙格-库塔法积分
KICAD 小封装拉线卡顿问题 解决方法
Rasa 3 x learning series - Rasa - 4873 dispatcher Issues. Utter_message study notes
周鸿祎称微软抄袭,窃取360安全模式
Service discovery of kubernetes