当前位置:网站首页>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)
}
边栏推荐
- 记一次gorm事务及调试解决mysql死锁
- Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
- 简单的页面跳转活动
- 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
- 架构:微服务网关(SIA-Gateway)简介
- 2022年NPDP考完多久出成绩?怎么查询?
- 详解最强分布式锁工具:Redisson
- 2022-08-01 Install mysql monitoring tool phhMyAdmin
- Remember a gorm transaction and debug to solve mysql deadlock
- GTK RGB图像绘制
猜你喜欢
随机推荐
使用docker安装mysql
CASE2023
接口测试神器Apifox究竟有多香?
指针数组和数组指针
Safety (2)
Can Youxuan database import wrongly be restored?
项目场景 with ERRTYPE = cudaError CUDA failure 999 unknown error
Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
The state status is displayed incorrectly after the openGauss switch
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
leetcode/字符串中的变位词-s1字符串的某个排列是s2的子串
AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
OC和Swift语言的区别
【 wheeled odometer 】
记一次gorm事务及调试解决mysql死锁
How engineers treat open source
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
详解最强分布式锁工具:Redisson
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?








