当前位置:网站首页>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 node
File 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)}
边栏推荐
- Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
- IPFS部署及文件上传(golang)
- canal同步Mariadb到Mysql
- Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
- 永磁同步电机36问(二)——机械量与电物理量如何转化?
- BI - SQL 丨 WHILE
- Chrome浏览器无法加载已解压的.crx文件的解决办法
- 数值积分方法:欧拉积分、中点积分和龙格-库塔法积分
- 2022牛客多校四_G M
- 【web】理解 Cookie 和 Session 机制
猜你喜欢
随机推荐
BI-SQL丨WHILE
网络层解析——IP协议、地址管理、路由选择
EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
永磁同步电机36问(三)——SVPWM代码实现
微信小程序异步回调函数恶梦和解决办法
53. 最小的k个数
数仓:为什么说 ETL 的未来不是 ELT,而是 EL (T)
790. 数的三次方根
yaml
29. 删除链表中重复的节点
Use DBeaver for mysql data backup and recovery
mockjs生成假数据的基本使用
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
详解最强分布式锁工具:Redisson
使用DBeaver进行mysql数据备份与恢复
Oracle19c安装图文教程
Remember a gorm transaction and debug to solve mysql deadlock
Curriculum Vitae;CV
AcWing 1285. 单词 题解(AC自动机)
2022年NPDP考完多久出成绩?怎么查询?