当前位置:网站首页>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)
}
边栏推荐
- OC中成员变量,实例变量和属性之间的区别和联系
- 搭建zabbix监控及邮件报警(超详细教学)
- MySQL - CRUD operations
- Chopper webshell feature analysis
- Use DBeaver for mysql data backup and recovery
- Win Go development kit installation configuration, GoLand configuration
- pyqt上手体验
- TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
- Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
- Curriculum Vitae;CV
猜你喜欢
Flask入门学习教程
How engineers treat open source
工程师如何对待开源
240...循迹
Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
The state status is displayed incorrectly after the openGauss switch
2022-08-01 mysql/stoonedb慢SQL-Q18分析
canal同步Mariadb到Mysql
FOFAHUB使用测试
Electronic Manufacturing Warehouse Barcode Management System Solution
随机推荐
Safety (1)
机器人领域期刊会议汇总
MySQL8 download, start, configure, verify
qt点云配准软件
拼多多借力消博会推动国内农产品品牌升级 看齐国际精品农货
线程的不同状态
Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
Flask 报错:WARNING This is a development server. Do not use it in a production deployment
架构:应用架构的演进以及微服务架构的落地实践
Swift运行时(派发机制)
Remember a pit for gorm initialization
Oracle19c安装图文教程
analog IC layout-Parasitic effects
leetcode/字符串中的变位词-s1字符串的某个排列是s2的子串
[ORB_SLAM2] SetPose, UpdatePoseMatrices
使用DBeaver进行mysql数据备份与恢复
Qt自定义控件和模板分享
BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
Outsourcing worked for three years, it was abolished...
AI target segmentation capability for fast video cutout without green screen