当前位置:网站首页>IPFS deployment and file upload (golang)
IPFS deployment and file upload (golang)
2022-08-02 02:46:00 【Asimov__】
IPFS Node Deployment
downloadwget https://dist.ipfs.io/kubo/v0.14.0/kubo_v0.14.0_linux-amd64.tar.gzdecompresstar xvfz kubo_v0.14.0_linux-amd64.tar.gzInstallcd kubo./install.shinitializationipfs initView the node information returned after initializationipfs cat /ipfs/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc/readmeConfigure ipfs cross domainipfs 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"]'Configure IPFS external network access consolecd ~/.ipfsvi configChange 127.0.0.1 to 0.0.0.0IPFS port description4001 main port for p2p connection and data synchronization5001 The api port of ipfs, the port of the management page, can read and write data8080 ipfs gateway port, used to read ipfs node data, read-only by defaultIf you use a firewall, remember to open these ports, otherwise the external network will still be inaccessibleStart IPFSnohup ipfs daemon >> ./log/nohup`date +%Y-%m-%d`.out 2>&1 &View connected nodesipfs swarm peersBrowser access http://localhost:5001/webui (management page, file upload, etc.) will be redirected to its own nodeFile upload code
package mainimport ("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.gofileHash, 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)}边栏推荐
猜你喜欢

Unable to log in to the Westward Journey

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

pyqt上手体验

GTK RGB图像绘制

AI target segmentation capability for fast video cutout without green screen

svm.SVC application practice 1--Breast cancer detection

因为WiFi原因navicat 无法连接数据库Mysql

Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products

Nacos源码分析专题(一)-环境准备

局部敏感哈希:如何在常数时间内搜索Embedding最近邻
随机推荐
【web】Understanding Cookie and Session Mechanism
第10章_索引优化与查询优化
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
How ReentrantLock works
OC中成员变量,实例变量和属性之间的区别和联系
罗德里格斯公式(Rodrigues‘ Rotation Formula)推导
线程的不同状态
KICAD 小封装拉线卡顿问题 解决方法
The state status is displayed incorrectly after the openGauss switch
永磁同步电机36问(二)——机械量与电物理量如何转化?
【LeetCode】145.二叉树的后序遍历
qt点云配准软件
内卷的正确打开方式
NIO's Sword
记一次gorm事务及调试解决mysql死锁
【LeetCode】94.二叉树的中序遍历
【LeetCode】104.二叉树的最大深度
【web】理解 Cookie 和 Session 机制
Chapter 7 Noise analysis
菜刀webshell特征分析