區塊鏈: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 <peerId>
[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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章