当前位置:网站首页>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)}边栏推荐
- IPFS部署及文件上传(golang)
- Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
- Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
- (一)Redis: 基于 Key-Value 的存储系统
- AcWing 1285. Word Problem Solving (AC Automata)
- 架构:分布式任务调度系统(SIA-Task)简介
- Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
- Unable to log in to the Westward Journey
- 2022牛客多校三_F G
- KICAD 拉线宽度无法修改,解决方法
猜你喜欢

FOFAHUB使用测试

BI - SQL 丨 WHILE

Nanoprobes纳米探针丨Nanogold偶联物的特点和应用

Service discovery of kubernetes

ros多客户端请求服务

aws s3上传文件

The state status is displayed incorrectly after the openGauss switch

Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!

字典常用方法

How engineers treat open source
随机推荐
EFCore 反向工程
欧拉公式的证明
2022 NPDP take an examination of how the results?How to query?
240...循迹
指针数组和数组指针
51. 数字排列
【LeetCode】102.二叉树的层序遍历
【web】Understanding Cookie and Session Mechanism
790. 数的三次方根
【每日一道LeetCode】——9. 回文数
Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
What to study after the PMP exam?The soft exam ahead is waiting for you~
永磁同步电机36问(三)——SVPWM代码实现
analog IC layout-Design for reliability
数值积分方法:欧拉积分、中点积分和龙格-库塔法积分
字符串常用方法
IMU预积分的简单理解
【CNN记录】tensorflow slice和strided_slice
【LeetCode】94.二叉树的中序遍历
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!