当前位置:网站首页>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)}边栏推荐
猜你喜欢
随机推荐
2022年NPDP考完多久出成绩?怎么查询?
(一)Redis: 基于 Key-Value 的存储系统
使用DBeaver进行mysql数据备份与恢复
Nacos源码分析专题(一)-环境准备
CASE2023
Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
【每日一道LeetCode】——1. 两数之和
IPFS部署及文件上传(golang)
项目场景 with ERRTYPE = cudaError CUDA failure 999 unknown error
【LeetCode】94.二叉树的中序遍历
PAT甲级打卡-1001-1004
29. 删除链表中重复的节点
微服务:微智能在软件系统的简述
欧拉公式的证明
Flask 报错:WARNING This is a development server. Do not use it in a production deployment
qt点云配准软件
Oracle数据类型介绍
因为WiFi原因navicat 无法连接数据库Mysql
ALCCIKERS Shane 20191114
OC中new和init的区别









