ipfs 使用測試
Alpine 3 安裝
- 安裝環境 Alpine Linux 3.16
- ipfs 0.12.2
su - apk add go-ipfs
~# ipfs version ipfs version 0.12.2
CentOS 6 安裝
- 安裝環境 CentOS 6.8 x86_64
- ipfs v0.4.2
wget https://dist.ipfs.io/go-ipfs/v0.4.2/go-ipfs_v0.4.2_linux-amd64.tar.gz tar xvfz go-ipfs_*.tar.gz mv go-ipfs/ipfs /usr/local/bin/ipfs
- 測試是否可正常運作
ipfs help
[root@localhost ipfs]# ipfs help USAGE ipfs - Global p2p merkle-dag filesystem. ipfs [<flags>] <command> [<arg>] ... SUBCOMMANDS BASIC COMMANDS init Initialize ipfs local configuration add <path> Add a file to ipfs cat <ref> Show ipfs object data get <ref> Download ipfs objects ls <ref> List links from an object refs <ref> List hashes of links from an object DATA STRUCTURE COMMANDS block Interact with raw blocks in the datastore : diag Print diagnostics TOOL COMMANDS config Manage configuration version Show ipfs version information update Download and apply go-ipfs updates commands List all available commands Use 'ipfs <command> --help' to learn more about each command. ipfs uses a repository in the local file system. By default, the repo is located at ~/.ipfs. To change the repo location, set the $IPFS_PATH environment variable: export IPFS_PATH=/path/to/ipfsrepo
Windows 10 安裝
- 安裝環境 Windows 10 64 bit
- ipfs v0.4.2
- 解開至 c:\ipfs\
2016/05/17 13:45 0 build-log 2016/05/17 13:45 377 install.sh 2016/05/17 13:45 28,201,984 ipfs.exe 2016/05/17 13:45 1,083 LICENSE 2016/05/17 13:45 455 README.md
cd c:\ipfs ipfs.exe help
C:\ipfs>ipfs.exe help USAGE ipfs - Global p2p merkle-dag filesystem. ipfs [<flags>] <command> [<arg>] ... SUBCOMMANDS BASIC COMMANDS init Initialize ipfs local configuration add <path> Add a file to ipfs : : update Download and apply go-ipfs updates commands List all available commands Use 'ipfs <command> --help' to learn more about each command. ipfs uses a repository in the local file system. By default, the repo is located at ~/.ipfs. To change the repo location, set the $IPFS_PATH environment variable: export IPFS_PATH=/path/to/ipfsrepo
Rapsberry Pi 安裝
sudo su - wget https://dist.ipfs.io/go-ipfs/v0.4.2/go-ipfs_v0.4.2_linux-arm.tar.gz
啟用設定
- 初始化 ipfs 目錄
ipfs init
[root@localhost ~]# ipfs init initializing ipfs node at /root/.ipfs generating 2048-bit RSA keypair...done peer identity: QmaU6Qwnu75JjiKr23VMEfT191sEJVcYcTLPnVSMmbua7n to get started, enter: ipfs cat /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme
- 查看初始化建立的目錄
ipfs ls /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG
[root@localhost ~]# ipfs ls /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V 1688 about QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y 200 contact QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 322 help QmdncfsVm2h5Kqq9hPmU7oAVX2zTSVP3L869tgTbPYnsha 1728 quick-start QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB 1102 readme QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ 1027 security-notes
- 啟動 ipfs daemon 加入 ipfs 的服務群集
ipfs daemon
[root@localhost ~]# ipfs daemon Initializing daemon... Swarm listening on /ip4/127.0.0.1/tcp/4001 : : Swarm listening on /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
- 查看目前可以看到的連結點
ipfs swarm peers
[root@localhost ~]# ipfs swarm peers /ip4/104.223.59.174/tcp/4001/ipfs/QmeWdgoZezpdHz1PX8Ly8AeDQahFkBNtHn6qKeNtWP1jB6 /ip4/104.232.114.65/tcp/35609/ipfs/QmWnpsGugiw4YNFXvXr3iN86LDnJx6UsebzeqnpBiybebR /ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z : : /ip4/81.236.22.69/tcp/61588/ipfs/QmXKd1pJxTqTWNgGENcX2daiGLgWRPDDsXJe8eecQCr6Vh /ip4/91.126.205.225/tcp/39174/ipfs/QmdP9LNSHeBYH5TvRgVsCs8XyuFuKbPhpijsCxjA7PGyBH
- 要建立自己封閉的 ipfs Cluster 可透過修改 config 內的 “Bootstrap” 定義為內部的 ipfs 節點 Exp.
cd .ipfs vi config
: : "Bootstrap": [ "/ip4/140.92.143.147/tcp/4001/ipfs/QmaU6Qwnu75JjiKr23VMEfT191sEJVcTcTLPnVSMmbua7n", "/ip4/140.92.143.147/tcp/4001/ipfs/QmWnk36dXLzbMAJijkYgH4JTs6Y2UKG1THeAA9Vx9VmemK" ], : :
基本操作
- 將 /tmp/test 目錄與底下檔案上傳至 ipfs 內
ipfs add -r /tmp/test
[root@xpc-ideas tmp]# ipfs add -r /tmp/test added QmR3Y6ZHkvVyEvjVAjbqMQfrAyC5mCfH5CcDg9ZcYhu3f6 test/IMAG5471.jpg added QmUPGSG2BR9zSJYRtXRVhDK5thJEE7kXgfjV7RgXXqtM5e test/IMAG5472.jpg added QmaVZADgMeibd7xX2MJuoBk9yJe6DWdAWuiN3hYpkbdJoX test/IMAG5473.jpg added QmWSQKyVbtWz3B4GjPSj68gtWQKVwQnZjiyFLRTe2jrXbq test/IMAG5474.jpg added QmaP366L4P2zRzGHRW7qa4VThckk2KjiqtNM6QmSMw6PNu test/IMAG5475.jpg added QmdujYFtXRpqdjoxc2rvbp5LEYTi1J2Qxe7x74eJsCHdQx test/IMAG5476.jpg added QmWR7tngmYs17rcKfBe19xuzfTf3DSbR5CsRymtpAfCCin test/IMAG5477.jpg added QmQbnDQXboZv9TDgTQJf1T2kniEjQof3ue9eUWhEPnvbuW test/IMAG5478.jpg added QmbzTnPVzPgRvVJQMWXNsGiA48WRnV7JMGuSmyDCvBaMbA test/IMAG5479.jpg added QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu test
- 將檔案 /tmp/test1.txt 上傳至 ipfs 內
ipfs add /tmp/test1.txt
[root@xpc-ideas tmp]# ipfs add /tmp/test1.txt added QmZRLdzkexfEiFkwwdyJ5g46BFtrCQwa47TL1e4gHok8rf test1.txt
- 查詢 QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu 目錄內容
ipfs ls QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu
[root@kvm-ipfs3 ~]# ipfs ls QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu QmR3Y6ZHkvVyEvjVAjbqMQfrAyC5mCfH5CcDg9ZcYhu3f6 1712841 IMAG5471.jpg QmUPGSG2BR9zSJYRtXRVhDK5thJEE7kXgfjV7RgXXqtM5e 2046221 IMAG5472.jpg QmaVZADgMeibd7xX2MJuoBk9yJe6DWdAWuiN3hYpkbdJoX 1322887 IMAG5473.jpg QmWSQKyVbtWz3B4GjPSj68gtWQKVwQnZjiyFLRTe2jrXbq 1489588 IMAG5474.jpg QmaP366L4P2zRzGHRW7qa4VThckk2KjiqtNM6QmSMw6PNu 1637953 IMAG5475.jpg QmdujYFtXRpqdjoxc2rvbp5LEYTi1J2Qxe7x74eJsCHdQx 1486240 IMAG5476.jpg QmWR7tngmYs17rcKfBe19xuzfTf3DSbR5CsRymtpAfCCin 1369639 IMAG5477.jpg QmQbnDQXboZv9TDgTQJf1T2kniEjQof3ue9eUWhEPnvbuW 2208829 IMAG5478.jpg QmbzTnPVzPgRvVJQMWXNsGiA48WRnV7JMGuSmyDCvBaMbA 1800607 IMAG5479.jpg
- 直接顯示 QmZRLdzkexfEiFkwwdyJ5g46BFtrCQwa47TL1e4gHok8rf 檔案內容
ipfs cat QmZRLdzkexfEiFkwwdyJ5g46BFtrCQwa47TL1e4gHok8rf
[root@kvm-ipfs3 ~]# ipfs cat QmZRLdzkexfEiFkwwdyJ5g46BFtrCQwa47TL1e4gHok8rf # chkweblog.pl Ver 1.00 (2014/10/16) Hostname:[xpc-ideas] Procdate:[2014-10-16 20:40] Taketime:[2]Sec. Keyword#3 : [/main/images/info.php] ----- : :
- 下載 QmZRLdzkexfEiFkwwdyJ5g46BFtrCQwa47TL1e4gHok8rf 檔案至 /tmp/t1.txt
ipfs get QmZRLdzkexfEiFkwwdyJ5g46BFtrCQwa47TL1e4gHok8rf -o /tmp/t1.txt
[root@kvm-ipfs3 ~]# ipfs get QmZRLdzkexfEiFkwwdyJ5g46BFtrCQwa47TL1e4gHok8rf -o /tmp/t1.txt Saving file(s) to /tmp/t1.txt 6.50 KB 0
- 下載 QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu 目錄至 /tmp/t2/
ipfs get QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu -o /tmp/t2
[root@kvm-ipfs3 ~]# ipfs get QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu -o /tmp/t2 Saving file(s) to /tmp/t2 14.38 MB 0 [root@kvm-ipfs3 ~]# cd /tmp/t2/ [root@kvm-ipfs3 t2]# ls IMAG5471.jpg IMAG5472.jpg IMAG5473.jpg IMAG5474.jpg IMAG5475.jpg IMAG5476.jpg IMAG5477.jpg IMAG5478.jpg IMAG5479.jpg
進階操作
- 當上傳至 ipfs 後,一開始只會存在本機的 ipfs repos 內, 當其他節點有 get / cat 時,才會將檔案區塊同步過去, 但也可以透過 pin add 的語法對特定目錄或檔案進行該節點立即同步, Exp.
ipfs pin add -r QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu
[root@kvm-ipfs1 ~]# ipfs pin add -r QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu pinned QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu recursively
- 上傳至 ipfs 的檔案原則上應該很難完全刪除掉, 除非對每個節點都可以進入維護. 在這樣條件下要刪除一個目錄或檔案, 可以透過 pin rm 以及 repo gc 的語法對每個節點的特定目錄或檔案進行標注刪除, Exp.
ipfs pin rm -r QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu ipfs repo gc
[root@xpc-ideas tmp]# ipfs pin rm -r QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu unpinned QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu [root@xpc-ideas tmp]# ipfs repo gc removed QmeHRZmBmeGLuszjAZczMA1f9uYrrxTggjNaLnKhMG1iw2 removed QmeUeLi59BU7szstx95vWSdKBXqojraGTACPXHxGqJMXz2 removed QmfBiCoFsu5jyETpywAUBWDWyj6Lru9PHYxcqgeTZQqnE8 removed QmfNHKknAV5qEdg8nktPj4qUAH6Hi9r7JEKexDPw7ZxJrp [root@kvm-ipfs1 ~]# ipfs pin rm -r QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu unpinned QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu [root@kvm-ipfs1 ~]# ipfs repo gc removed QmeHRZmBmeGLuszjAZczMA1f9uYrrxTggjNaLnKhMG1iw2 removed QmeUeLi59BU7szstx95vWSdKBXqojraGTACPXHxGqJMXz2 removed QmfBiCoFsu5jyETpywAUBWDWyj6Lru9PHYxcqgeTZQqnE8 removed QmfNHKknAV5qEdg8nktPj4qUAH6Hi9r7JEKexDPw7ZxJrp [root@kvm-ipfs2 ~]# ipfs pin rm -r QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu Error: not pinned [root@kvm-ipfs2 ~]# ipfs repo gc [root@kvm-ipfs3 t2]# ipfs pin rm -r QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu Error: not pinned [root@kvm-ipfs3 t2]# ipfs repo gc
如果該節點有同步過就會回應 unpinned , 沒同步過則回應 Error: not pinned
ipfs ls QmVavKj5mD7z37Z32YHGhAgML4PadDbE3j1JzEgNoBo5Qu
如果將所有節點都清除就會查不到上傳的資料