目錄
- 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 + ethereum``Dapp
開發(fā)入門
- 6.1
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
USAGE
ipfs - Global p2p merkle-dag filesystem.
ipfs [--config=<config> | -c] [--debug=<debug> | -D] [--help=<help>] [-h=<h>] [--local=<local> | -L] [--api=<api>] <command> ...
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
object Interact with raw dag nodes
files Interact with objects as if they were a unix filesystem
dag Interact with IPLD documents (experimental)
ADVANCED COMMANDS
daemon Start a long-running daemon process
mount Mount an IPFS read-only mountpoint
resolve Resolve any type of name
name Publish and resolve IPNS names
key Create and list IPNS name keypairs
dns Resolve DNS links
pin Pin objects to local storage
repo Manipulate the IPFS repository
stats Various operational stats
p2p Libp2p stream mounting
filestore Manage the filestore (experimental)
NETWORK COMMANDS
id Show info about IPFS peers
bootstrap Add or remove bootstrap peers
swarm Manage connections to the p2p network
dht Query the DHT for values or peers
ping Measure the latency of a connection
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
1.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
文件生成一個唯一的hash``QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T
加矛,如果想查看本地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-tutorial
3.2 通過路徑訪問contactme.txt文件數(shù)據(jù)
如果我們上傳的是目錄,那么可以通過下面幾種方式訪問到contactme.txt
文件的數(shù)據(jù)怎虫。
localhost:1121 yuechunli$ ipfs cat QmYx4BnhnLXeMWF5mKu16fJgUBiVP7ECXh7qcsUZnXiRxc
localhost:1121 yuechunli$ ipfs cat /ipfs/QmYx4BnhnLXeMWF5mKu16fJgUBiVP7ECXh7qcsUZnXiRxc
localhost:1121 yuechunli$ ipfs cat /ipfs/QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek/contactme.txt
localhost:1121 yuechunli$
3.3 通過Hash查看數(shù)據(jù)IPFS網(wǎng)絡(luò)數(shù)據(jù)
-
通過目錄訪問文件:https://ipfs.io/ipfs/QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek/contactme.txt
image 通過文件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 daemon
4.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
localhost:ipfs_pin yuechunli$ ipfs id
{
"ID": "《your peer id》"
}
localhost:ipfs_pin yuechunli$
在上面的源碼中全局搜索將源碼里面的QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP
替換成你自己的ID
匾二。
接下來重復(fù)4. 創(chuàng)建簡易的網(wǎng)頁發(fā)布到IPFS的操作步驟即可哮独。
5.3 瀏覽博客
瀏覽器打開https://ipfs.io/ipns/QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP/查看項目效果。
6. 下篇預(yù)報
6.1
ipfs + ethereum``Dapp
開發(fā)入門