区块链:ipfs安装、使用;

010.环境

Centos 7
go-ipfs v0.4.18

020.安装go-ipfs

官       网:https://ipfs.io/

下载页面:https://dist.ipfs.io/#go-ipfs

下载地址:https://dist.ipfs.io/go-ipfs/v0.4.18/go-ipfs_v0.4.18_linux-amd64.tar.gz

tar zxvf go-ipfs_v0.4.18_linux-amd64.tar.gz -C /usr/
cd /usr/go-ipfs/
./install.sh

021.使用ipfs

初始化:ipfs init

[root@localhost go-ipfs]# ipfs init
initializing IPFS node at /root/.ipfs
generating 2048-bit RSA keypair...done
peer identity: Qmdy9uJmipnZPQLEf2nF39uwSbGphqmLRjLXkHWmwQoeDC
to get started, enter:

        ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme

[root@localhost go-ipfs]# 

 其中 Qmdy9uJmipnZPQLEf2nF39uwSbGphqmLRjLXkHWmwQoeDC 为该结点ID,也可通过命令查看:ipfs id

[root@localhost go-ipfs]# ipfs id
{
        "ID": "Qmdy9uJmipnZPQLEf2nF39uwSbGphqmLRjLXkHWmwQoeDC",
        "PublicKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDUOjgDvMlPmUcrBjL/rBJ4RZUshxwzzZoH0t2erzsxW7LtLuiQbsP8t7Nse8TudCvpR8YWW01HTlcGdKvDmHj/FfCeavrYSJQ5ZWiuGTSlOJE9SAbYdCWefAEZ8cVBz3RUFSmtTCAElgxeooBG7vH8SzwYoWYIT1G41rj/A63DHUGJtggVf4w7w5qnMA8WWHSlddukY4LPJTVGdirKe4fv1+JNwA9Zm0kLlcQTjrXygxa0uvzsVhhd8hSqERpCDALs0AyCS6gCdDO3sSpXDYCu2WhNhT1DMdi3RLZ406st8g44JBHXIZkVHC5RIIXyPCLOsnXotDditjLPZnq3RAiFAgMBAAE=",
        "Addresses": null,
        "AgentVersion": "go-ipfs/0.4.18/",
        "ProtocolVersion": "ipfs/0.1.0"
}

[root@localhost go-ipfs]# 
[root@localhost go-ipfs]# ipfs cat /ipfs/Qmdy9uJmipnZPQLEf2nF39uwSbGphqmLRjLXkHWmwQoeDC/readme
Hello and Welcome to IPFS!

██╗██████╗ ███████╗███████╗
██║██╔══██╗██╔════╝██╔════╝
██║██████╔╝█████╗  ███████╗
██║██╔═══╝ ██╔══╝  ╚════██║
██║██║     ██║     ███████║
╚═╝╚═╝     ╚═╝     ╚══════╝

If you're seeing this, you have successfully installed
IPFS and are now interfacing with the ipfs merkledag!

 -------------------------------------------------------
| Warning:                                              |
|   This is alpha software. Use at your own discretion! |
|   Much is missing or lacking polish. There are bugs.  |
|   Not yet secure. Read the security notes for more.   |
 -------------------------------------------------------

Check out some of the other files in this directory:

  ./about
  ./help
  ./quick-start     <-- usage examples
  ./readme          <-- this file
  ./security-notes
[root@localhost go-ipfs]# 

 查看目录

[root@localhost go-ipfs]# ll ~/.ipfs/
total 16
drwxr-xr-x. 17 root root  210 Dec 12 23:44 blocks
-rw-rw----.  1 root root 5273 Dec 12 23:42 config
drwxr-xr-x.  2 root root  121 Dec 12 23:44 datastore
-rw-------.  1 root root  190 Dec 12 23:42 datastore_spec
drwx------.  2 root root    6 Dec 12 23:42 keystore
-rw-r--r--.  1 root root    2 Dec 12 23:42 version
[root@localhost go-ipfs]# 

查看配置:ipfs config show

修改配置:ipfs config edit

[root@localhost go-ipfs]# ipfs config edit
Error: ENV variable $EDITOR not set
[root@localhost go-ipfs]# 

需要为ipfs指定编辑器,本人指定vim

export EDITOR=vim
ipfs config edit

022.添加文件

编辑文件: test.txt

echo "xxoo" > test.txt

添加文件到ipfs:ipfs add test.txt

[root@localhost ~]# ipfs add test.txt 
added QmWwQpzop8hgRhd8Jt2AbvuDZ51K4PDJJfrm94V7tQBmK9 test.txt
 5 B / 5 B [===========================================================================================================================================================================================================================================] 100.00%
[root@localhost ~]# 

查看文件内容(命令):

[root@localhost ~]# ipfs cat QmWwQpzop8hgRhd8Jt2AbvuDZ51K4PDJJfrm94V7tQBmK9
xxoo
[root@localhost ~]# 

023.上传文件到网络

运行命令:ipfs daemon,自动同步;再开个终端里执行

[root@localhost ~]# ipfs daemon
Initializing daemon...
go-ipfs version: 0.4.18-
Repo version: 7
System version: amd64/linux
Golang version: go1.11.1
Successfully raised file descriptor limit to 2048.
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/192.168.1.121/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /p2p-circuit
Swarm announcing /ip4/127.0.0.1/tcp/4001
Swarm announcing /ip4/192.168.1.121/tcp/4001
Swarm announcing /ip6/::1/tcp/4001
API server listening on /ip4/127.0.0.1/tcp/5001
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
Daemon is ready

其中 Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080 网关地址配置为127.0.0.1,只能本地访问,如果想远程访问,需要将其修改为0.0.0.0,修改方法,运行命令:ipfs config edit 同时也可以修改8080端口。

  "Addresses": {
    "Swarm": [
      "/ip4/0.0.0.0/tcp/4001",
      "/ip6/::/tcp/4001"
    ],
    "Announce": [],
    "NoAnnounce": [],
    "API": "/ip4/0.0.0.0/tcp/5001",
    "Gateway": "/ip4/0.0.0.0/tcp/8080"
  },

上面"API"和 "Gateway"中的IP,改为0.0.0.0。保存后,重启ipfs

浏览器(服务器ip:192.168.1.121)

http://192.168.1.121:8080/ipfs/QmWwQpzop8hgRhd8Jt2AbvuDZ51K4PDJJfrm94V7tQBmK9

浏览器(远程,需要):

https://ipfs.io/ipfs/QmWwQpzop8hgRhd8Jt2AbvuDZ51K4PDJJfrm94V7tQBmK9

https://gateway.ipfs.io/ipfs/QmWwQpzop8hgRhd8Jt2AbvuDZ51K4PDJJfrm94V7tQBmK9

https://dweb.link/ipfs/QmWwQpzop8hgRhd8Jt2AbvuDZ51K4PDJJfrm94V7tQBmK9

024.IPNS

ipfs基于内容存储,与文件名没有关系。只要内容一致,生成hash就一致:

例1:内容一致,文件名不一致,生成的hash是一样的,都为QmWwQpzop8hgRhd8Jt2AbvuDZ51K4PDJJfrm94V7tQBmK9

[root@localhost ~]# echo 'xxoo' > test1.txt  
[root@localhost ~]# echo 'xxoo' > test2.txt
[root@localhost ~]# ipfs add test1.txt 
added QmWwQpzop8hgRhd8Jt2AbvuDZ51K4PDJJfrm94V7tQBmK9 test1.txt
 5 B / 5 B [===========================================================================================================================================================================================================================================] 100.00%[root@localhost ~]# ipfs add test2.txt  
added QmWwQpzop8hgRhd8Jt2AbvuDZ51K4PDJJfrm94V7tQBmK9 test2.txt
 5 B / 5 B [===========================================================================================================================================================================================================================================] 100.00%[root@localhost ~]# 

例2:内容不一致,文件名一致,则生成两个不同的hash

[root@localhost ~]# echo 'xxoo' > test1.txt
[root@localhost ~]# ipfs add test1.txt 
added QmWwQpzop8hgRhd8Jt2AbvuDZ51K4PDJJfrm94V7tQBmK9 test1.txt
 5 B / 5 B [===========================================================================================================================================================================================================================================] 100.00%[root@localhost ~]# echo 'xxooyy' > test1.txt
[root@localhost ~]# ipfs add test1.txt       
added QmNvDf8sHg1vdG7v7RghssFfGmYBccEZDXyhW91DWKmgys test1.txt
 7 B / 7 B [===========================================================================================================================================================================================================================================] 100.00%[root@localhost ~]# 

如果向外开放一个服务(界面),如果版本更新了(内容变化了),不可能要求客户端更换地址,此时需要映射到ipns

[root@localhost ~]# ipfs name publish 文件hash
[root@localhost ~]# 
[root@localhost ~]# ipfs name publish QmWwQpzop8hgRhd8Jt2AbvuDZ51K4PDJJfrm94V7tQBmK9
Published to Qmdy9uJmipnZPQLEf2nF39uwSbGphqmLRjLXkHWmwQoeDC: /ipfs/QmWwQpzop8hgRhd8Jt2AbvuDZ51K4PDJJfrm94V7tQBmK9
[root@localhost ~]# 

结果为Published to A: /ipfs/B

其中A为该ipfs结点ID,B为文件hash

验证:

[root@localhost ~]# ipfs name resolve 
[root@localhost ~]# 
[root@localhost ~]# ipfs name resolve Qmdy9uJmipnZPQLEf2nF39uwSbGphqmLRjLXkHWmwQoeDC
/ipfs/QmWwQpzop8hgRhd8Jt2AbvuDZ51K4PDJJfrm94V7tQBmK9
[root@localhost ~]# 

访问地址:

https://ipfs.io/ipfn/结点ID

https://ipfs.io/ipfn/Qmdy9uJmipnZPQLEf2nF39uwSbGphqmLRjLXkHWmwQoeDC

如果文件内容有更新,重新publish一次;

030.版本管理

待补充。。。。

040.添加ipfs服务

vim /lib/systemd/system/ipfs.service

添加以下内容:

[Unit]
Description=IPFS
[Service]
ExecStart=/usr/local/bin/ipfs daemon
Restart=always
User=root
Group=root
[Install]
WantedBy=multi-user.target

 运行下面命令,开启、停止ipfs服务

service ipfs start
service ipfs stop

090.参考资料:

https://ipfs.io/

https://github.com/ipfs

https://blog.csdn.net/liyuechun520/article/details/78599374?locationNum=2&fps=1

https://blog.csdn.net/guiziwen/article/details/80291702 ---- 让IPFS处于后台服务中,guiziwen;

你可能感兴趣的:(区块链)