【IPFS + 区块链 系列】 入门篇 - IPFS+IPNS+个人博客搭建
目錄
- 1. 如何在IPFS新增一個文件
- 1.1 新建file.txt文件
- 1.2 查看ipfs相關(guān)命令
- 1.3 將file.txt添加到ipfs節(jié)點
- 2. 通過ipfs創(chuàng)建目錄存儲文件
- 3. 如何在IPFS新增一個目錄
- 3.1 使用ipfs add -r可以上傳一整個目錄
- 3.2 通過路徑訪問contactme.txt文件數(shù)據(jù)
- 3.3 通過Hash查看數(shù)據(jù)IPFS網(wǎng)絡(luò)數(shù)據(jù)
- 4. 創(chuàng)建簡易的網(wǎng)頁發(fā)布到IPFS
- 4.1 創(chuàng)建一個index.html文件
- 4.2 創(chuàng)建一個style.css文件
- 4.3 添加到ipfs
- 4.4 網(wǎng)絡(luò)同步
- 4.5 訪問網(wǎng)站
- 4.6 發(fā)布到IPNS
- 5. 發(fā)布個人博客
- 5.1 搭建靜態(tài)博客
- 5.2 節(jié)點ID替換
- 5.3 瀏覽博客
- 6. 下篇預(yù)報
- 6.1?ipfs + ethereumDapp開發(fā)入門
1. 如何在IPFS新增一個文件
1.1 新建file.txt文件
打開終端,切換到桌面,新建一個文件夾1121,切換到1121中,通過vi新建一個文件file.txt,文件里面輸入春哥微信號liyc1215保存并且退出。
localhost:Desktop yuechunli$ pwd /Users/liyuechun/Desktop localhost:Desktop yuechunli$ mkdir 1121 localhost:Desktop yuechunli$ cd 1121/ localhost:1121 yuechunli$ vi file.txt localhost:1121 yuechunli$ cat file.txt liyc1215 localhost:1121 yuechunli$1.2 查看ipfs相關(guān)命令
localhost:1121 yuechunli$ ipfs help USAGEipfs - Global p2p merkle-dag filesystem.ipfs [--config=<config> | -c] [--debug=<debug> | -D] [--help=<help>] [-h=<h>] [--local=<local> | -L] [--api=<api>] <command> ...SUBCOMMANDSBASIC COMMANDSinit Initialize ipfs local configurationadd <path> Add a file to IPFScat <ref> Show IPFS object dataget <ref> Download IPFS objectsls <ref> List links from an objectrefs <ref> List hashes of links from an objectDATA STRUCTURE COMMANDSblock Interact with raw blocks in the datastoreobject Interact with raw dag nodesfiles Interact with objects as if they were a unix filesystemdag Interact with IPLD documents (experimental)ADVANCED COMMANDSdaemon Start a long-running daemon processmount Mount an IPFS read-only mountpointresolve Resolve any type of namename Publish and resolve IPNS nameskey Create and list IPNS name keypairsdns Resolve DNS linkspin Pin objects to local storagerepo Manipulate the IPFS repositorystats Various operational statsp2p Libp2p stream mountingfilestore Manage the filestore (experimental)NETWORK COMMANDSid Show info about IPFS peersbootstrap Add or remove bootstrap peersswarm Manage connections to the p2p networkdht Query the DHT for values or peersping Measure the latency of a connectiondiag Print diagnosticsTOOL COMMANDSconfig Manage configurationversion Show ipfs version informationupdate Download and apply go-ipfs updatescommands List all available commands1.3 將file.txt添加到ipfs節(jié)點
localhost:1121 yuechunli$ ls file.txt localhost:1121 yuechunli$ ipfs add file.txt added QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T file.txt localhost:1121 yuechunli$ cat file.txt liyc1215 localhost:1121 yuechunli$ ipfs cat QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T liyc1215 localhost:1121 yuechunli$當(dāng)執(zhí)行完ipfs add file.txt這個命令以后,會將file.txt添加到ipfs當(dāng)前的節(jié)點中,并且會對file.txt文件生成一個唯一的hashQmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T,如果想查看本地ipfs節(jié)點的數(shù)據(jù),可以通過ipfs cat QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T進行查看。
??:當(dāng)我試圖通過http://ipfs.io/ipfs/QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T進行數(shù)據(jù)訪問時,無法訪問,如下圖所示:
??:雖然數(shù)據(jù)已經(jīng)添加到當(dāng)前的你自己的IPFS節(jié)點中,但是并沒有同步到IPFS網(wǎng)絡(luò),所以暫時在網(wǎng)絡(luò)上無法訪問。
??:重要:接下來執(zhí)行下面的命令同步節(jié)點數(shù)據(jù)到IPFS網(wǎng)絡(luò),再試圖在網(wǎng)絡(luò)上查看數(shù)據(jù)。
- 同步節(jié)點
新建一個終端,執(zhí)行ipfs daemon。
localhost:.ipfs yuechunli$ ipfs daemon Initializing daemon... Adjusting current ulimit to 2048... Successfully raised file descriptor limit to 2048. Swarm listening on /ip4/111.196.246.151/tcp/3637 Swarm listening on /ip4/127.0.0.1/tcp/4001 Swarm listening on /ip4/169.254.170.167/tcp/4001 Swarm listening on /ip4/192.168.0.107/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網(wǎng)絡(luò)查看數(shù)據(jù)
瀏覽器訪問https://ipfs.io/ipfs/QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T
2. 通過ipfs創(chuàng)建目錄存儲文件
在著上面的步驟走,我們可以通過ipfs cat QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T liyc1215查看添加到ipfs網(wǎng)絡(luò)的file.txt文件的內(nèi)容,如下:
localhost:1121 yuechunli$ ipfs cat QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T liyc1215 localhost:1121 yuechunli$當(dāng)然,我們也可以通過ipfs的相關(guān)命令在ipfs的根目錄下面創(chuàng)建文件夾,并且將file.txt文件移動或者拷貝到我們創(chuàng)建的文件夾中。
??:cp不會改變文件hash,mv會改變hash尋址。
localhost:1121 yuechunli$ ipfs cat QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T liyc1215 localhost:1121 yuechunli$ ipfs files mkdir /LiYueChun localhost:1121 yuechunli$ ipfs files cp /ipfs/QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T /LiYueChun/file.txt localhost:1121 yuechunli$ ipfs files ls / LiYueChun localhost:1121 yuechunli$ ipfs files ls /LiYueChun/ file.txt localhost:1121 yuechunli$ ipfs files read /LiYueChun/file.txt liyc1215 localhost:1121 yuechunli$3. 如何在IPFS新增一個目錄
3.1 使用ipfs add -r可以上傳一整個目錄
localhost:1121 yuechunli$ ipfs add -r ipfs-tutorial/ added QmYx4BnhnLXeMWF5mKu16fJgUBiVP7ECXh7qcsUZnXiRxc ipfs-tutorial/contactme.txt added QmfKdWsguobA3aDPvSxLB3Bq4HMKyqKSgFr2NFUuVH8n31 ipfs-tutorial/eth-fabric.png added QmXe8jTxTh5MZP6BK5cnj19mXNTKVMzNyUJZUHuYyr5dk1 ipfs-tutorial/gongzhonghao.png added QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek ipfs-tutorial3.2 通過路徑訪問contactme.txt文件數(shù)據(jù)
如果我們上傳的是目錄,那么可以通過下面幾種方式訪問到contactme.txt文件的數(shù)據(jù)。
localhost:1121 yuechunli$ ipfs cat QmYx4BnhnLXeMWF5mKu16fJgUBiVP7ECXh7qcsUZnXiRxc 微信:liyc1215 區(qū)塊鏈技術(shù)交流群:348924182 公眾號:區(qū)塊鏈部落 localhost:1121 yuechunli$ ipfs cat /ipfs/QmYx4BnhnLXeMWF5mKu16fJgUBiVP7ECXh7qcsUZnXiRxc 微信:liyc1215 區(qū)塊鏈技術(shù)交流群:348924182 公眾號:區(qū)塊鏈部落 localhost:1121 yuechunli$ ipfs cat /ipfs/QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek/contactme.txt 微信:liyc1215 區(qū)塊鏈技術(shù)交流群:348924182 公眾號:區(qū)塊鏈部落 localhost:1121 yuechunli$3.3 通過Hash查看數(shù)據(jù)IPFS網(wǎng)絡(luò)數(shù)據(jù)
- 訪問目錄:https://ipfs.io/ipfs/QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek
-
通過目錄訪問文件:https://ipfs.io/ipfs/QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek/contactme.txt
-
通過文件hash直接訪問:https://ipfs.io/ipfs/QmYx4BnhnLXeMWF5mKu16fJgUBiVP7ECXh7qcsUZnXiRxc
4. 創(chuàng)建簡易的網(wǎng)頁發(fā)布到IPFS
在這里我先自己寫一個簡單的網(wǎng)頁給大家演示,先在桌面新建一個site文件夾,然后按照下面的步驟在site文件夾中建立index.html和style.css文件。
4.1 創(chuàng)建一個index.html文件
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Hello IPFS!</title><link rel="stylesheet" href="./style.css" /> </head> <body><h1>Hello IPFS!</h1> </body> </html>4.2 創(chuàng)建一個style.css文件
h1 {color: green; }4.3 添加到ipfs
localhost:Desktop yuechunli$ ipfs add -r site/ added QmWG5rbgT9H77TGq49RXNoqN8M7DNKMnMX425nkmCB6BjS site/index.html added QmfGLJ3mryLvicQqzdsghq4QRhptKJtBAPe7yDJxsBGSuy site/style.css added QmdVEGkT5u7LtzzatTrn8JGNEF3fpuMPVs2rPCfvqRykRp site最后一行是項目根目錄的hash,你先通過ipfs daemon同步網(wǎng)絡(luò),然后可以通過https://ipfs.io/ipfs/<你的項目根目錄hash>,即https://ipfs.io/ipfs/QmdVEGkT5u7LtzzatTrn8JGNEF3fpuMPVs2rPCfvqRykRp訪問項目。
4.4 網(wǎng)絡(luò)同步
localhost:Desktop yuechunli$ ipfs daemon4.5 訪問網(wǎng)站
瀏覽器打開https://ipfs.io/ipfs/QmdVEGkT5u7LtzzatTrn8JGNEF3fpuMPVs2rPCfvqRykRp,效果圖如下:
4.6 發(fā)布到IPNS
當(dāng)我們修改網(wǎng)站內(nèi)容重新添加到ipfs時,hash會發(fā)生變化,當(dāng)我們網(wǎng)站更新時,我們可以將網(wǎng)站發(fā)布到IPNS,在IPNS中,允許我們節(jié)點的域名空間中引用一個IPFS hash,也就是說我們可以通過節(jié)點ID對項目根目錄的IPFS HASH進行綁定,以后我們訪問網(wǎng)站時直接通過節(jié)點·ID訪問即可,當(dāng)我們更新博客時,重新發(fā)布到IPNS`即可。
localhost:~ yuechunli$ ipfs name publish QmdVEGkT5u7LtzzatTrn8JGNEF3fpuMPVs2rPCfvqRykRp Published to QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP: /ipfs/QmdVEGkT5u7LtzzatTrn8JGNEF3fpuMPVs2rPCfvqRykRp localhost:~ yuechunli$ ipfs id {"ID": "QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP" }當(dāng)我們執(zhí)行ipfs name publish命令時,會返回我們的節(jié)點ID,你可以通過ipfs id進行查看驗證是否是你的節(jié)點ID。
??:驗證
$ ipfs name resolve <peerId> localhost:~ yuechunli$ ipfs name resolve QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP /ipfs/QmdVEGkT5u7LtzzatTrn8JGNEF3fpuMPVs2rPCfvqRykRp localhost:~ yuechunli$??:當(dāng)然我們現(xiàn)在就可以通過IPNS進行訪問了。
https://ipfs.io/ipns/QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP??????:注意上面是ipns而不是ipfs。
??:如果你網(wǎng)站數(shù)據(jù)修改,需要重新發(fā)布到IPNS。
5. 發(fā)布個人博客
你可以通過Hugo按照官方文檔創(chuàng)建一個漂亮的靜態(tài)博客Hugo官方網(wǎng)站,當(dāng)然你也可以自己編寫,或者使用其他開源項目搭建。
5.1 搭建靜態(tài)博客
大家可以自己搭建,也可以直接下載我的博客源碼直接搭建。
源碼地址:http://github.com/liyuechun/ipfs_blogger
5.2 節(jié)點ID替換
- 查看你的節(jié)點ID
在上面的源碼中全局搜索將源碼里面的QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP替換成你自己的ID。
接下來重復(fù)4. 創(chuàng)建簡易的網(wǎng)頁發(fā)布到IPFS的操作步驟即可。
5.3 瀏覽博客
瀏覽器打開https://ipfs.io/ipns/QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP/查看項目效果。
6. 下篇預(yù)報
6.1?ipfs + ethereumDapp開發(fā)入門
http://liyuechun.org/2017/11/21/ipfs-blockchain-blogger/
總結(jié)
以上是生活随笔為你收集整理的【IPFS + 区块链 系列】 入门篇 - IPFS+IPNS+个人博客搭建的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【IPFS + 区块链 系列】 入门篇
- 下一篇: IPFS + 区块链 系列】 入门篇 -