当前位置:网站首页>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)}边栏推荐
- Remember a gorm transaction and debug to solve mysql deadlock
- 周鸿祎称微软抄袭,窃取360安全模式
- 数仓:为什么说 ETL 的未来不是 ELT,而是 EL (T)
- svm.SVC应用实践1--乳腺癌检测
- 【web】理解 Cookie 和 Session 机制
- OC和Swift语言的区别
- What to study after the PMP exam?The soft exam ahead is waiting for you~
- Analysis of the status quo of digital transformation of manufacturing enterprises
- NIO's Sword
- 53. 最小的k个数
猜你喜欢

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

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?

mockjs生成假数据的基本使用

2022牛客多校四_G M

Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided

Talking about the "horizontal, vertical and vertical" development trend of domestic ERP

MySQL索引优化实战

【web】理解 Cookie 和 Session 机制

一次SQL优化,数据库查询速度提升 60 倍

灰度传感器、、、diy原理。。图
随机推荐
周鸿祎称微软抄袭,窃取360安全模式
【LeetCode】206.反转链表
OperatingSystemMXBean获取系统性能指标
aws s3上传文件
The state status is displayed incorrectly after the openGauss switch
罗德里格斯公式(Rodrigues‘ Rotation Formula)推导
mysql 查看死锁
pyqt上手体验
微信小程序异步回调函数恶梦和解决办法
搭建zabbix监控及邮件报警(超详细教学)
ReentrantLock工作原理
IPFS部署及文件上传(golang)
【LeetCode】20.有效的括号
欧拉公式的证明
Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
NIO's Sword
【LeetCode】104.二叉树的最大深度
The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
MySQL索引优化实战
FOFAHUB usage test