IPFS+IPNS应用1
朱起运
2023-12-01
1、在ipfs新增文件
1.1 新建文件
tianlei@tianlei:~$ mkdir aaa
tianlei@tianlei:~$ cd aaa
tianlei@tianlei:~/aaa$ nano file.txt
tianlei@tianlei:~/aaa$ cat file.txt
hello world
hello me
1.2 查看ipfs相关命令
tianlei@tianlei:~/aaa$ 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
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
EXIT STATUS
The CLI will exit with one of the following values:
0 Successful execution.
1 Failed executions.
1.3 将file.txt添加到ipfs节点
tianlei@tianlei:~/aaa$ ipfs add file.txt
added QmakCchtpckRmdRSiMdkseTEZ2yCuwNZLBososmYv2hcFn file.txt
tianlei@tianlei:~/aaa$ ipfs cat QmakCchtpckRmdRSiMdkseTEZ2yCuwNZLBososmYv2hcFn
hello world
hello me
tianlei@tianlei:~/aaa$
1.4 浏览器查看文件
https://ipfs.io/ipfs/QmakCchtpckRmdRSiMdkseTEZ2yCuwNZLBososmYv2hcFn
hello world
hello me
2. 通过ipfs创建目录存储文件
tianlei@tianlei:~$ ipfs files cp /ipfs/QmakCchtpckRmdRSiMdkseTEZ2yCuwNZLBososmYv2hcFn /tian/
tianlei@tianlei:~$ ipfs files ls /
tian
tianlei@tianlei:~$ ipfs files ls /tian/
QmakCchtpckRmdRSiMdkseTEZ2yCuwNZLBososmYv2hcFn
tianlei@tianlei:~$ ipfs files read /tian/QmakCchtpckRmdRSiMdkseTEZ2yCuwNZLBososmYv2hcFn
hello world
hello me
3.IPFS新增一个目录
3.1上传一整个目录
tianlei@tianlei:~$ mkdir bbb
tianlei@tianlei:~$ cd bbb/
tianlei@tianlei:~/bbb$ nano 1.txt
tianlei@tianlei:~/bbb$ nano 2.txt
tianlei@tianlei:~/bbb$ ipfs add -r ~/bbb/
added QmaxiY2vKzNbPdg5eC6zqce61gVYpAPwGkF2h6n6gwXYjR bbb/1.txt
added QmVPYseYS571a4hKe9KwaNg1ctQZ6Hqv8hLfAv7rwzcjyC bbb/2.txt
added QmQm2jn5SRDY1SGbmkvBYRsXQqS9HxTsApYzBcebmjUkio bbb
tianlei@tianlei:~/bbb$
3.2通过路径访问文件数据
tianlei@tianlei:~/bbb$ ipfs cat QmaxiY2vKzNbPdg5eC6zqce61gVYpAPwGkF2h6n6gwXYjR
hfkahf;fahk
tianlei@tianlei:~/bbb$ ipfs cat /ipfs/QmaxiY2vKzNbPdg5eC6zqce61gVYpAPwGkF2h6n6gwXYjR
hfkahf;fahk
tianlei@tianlei:~/bbb$ ipfs cat /ipfs/QmaxiY2vKzNbPdg5eC6zqce61gVYpAPwGkF2h6n6gwXYjR/1.txt
4.通过Hash查看数据IPFS网络数据
https://ipfs.io/ipfs/QmaxiY2vKzNbPdg5eC6zqce61gVYpAPwGkF2h6n6gwXYjR
通过目录
https://ipfs.io/ipfs/QmQm2jn5SRDY1SGbmkvBYRsXQqS9HxTsApYzBcebmjUkio
5、创建简易的网页发布到IPFS
5.1创建文件
tianlei@tianlei:~$ mkdir site
tianlei@tianlei:~$ cd site/
tianlei@tianlei:~/site$ nano 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>
tianlei@tianlei:~/site$ nano style.css
h1 {
color: green;
}
5.2添加到ipfs
tianlei@tianlei:~/site$ cd ..
tianlei@tianlei:~$ ipfs add -r site/
added QmWG5rbgT9H77TGq49RXNoqN8M7DNKMnMX425nkmCB6BjS site/index.html
added QmNr7LKsFErswJLmXMZNwXKYwWtsgeTcdXiU3pHCNnu1Vw site/style.css
added QmRSyV1J8DNwQRGdsYLYkz9ohntYJfehmSDyJkxcxUqy33 site
tianlei@tianlei:~$
5.3访问网站
https://ipfs.io/ipfs/QmRSyV1J8DNwQRGdsYLYkz9ohntYJfehmSDyJkxcxUqy33/
Hello IPFS!
6、发布到IPNS
当我们修改网站内容重新添加到ipfs时,hash会发生变化,当我们网站更新时,我们可以将网站发布到IPNS,在IPNS中,允许我们节点的域名空间中引用一个IPFS hash,也就是说我们可以通过节点ID对项目根目录的IPFS HASH进行绑定,以后我们访问网站时直接通过节点·ID访问即可,当我们更新博客时,重新发布到IPNS`即可。
tianlei@tianlei:~$ ipfs name publish QmRSyV1J8DNwQRGdsYLYkz9ohntYJfehmSDyJkxcxUqy33
Published to QmWkj9HDMMydjjvzjEqcdtVkCgCGyGdShxRvbiaF4G769J: /ipfs/QmRSyV1J8DNwQRGdsYLYkz9ohntYJfehmSDyJkxcxUqy33
tianlei@tianlei:~$ ipfs id
{
"ID": "QmWkj9HDMMydjjvzjEqcdtVkCgCGyGdShxRvbiaF4G769J",
"PublicKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCoWPqpWmO4tHrzaIQW1WxNJZ7JxOKXYuRwdIEdmxdR76xJQNQCx0uDkMiqW7NbapTYuK7waCXgjF6dF3MSAePE1JyTJMfGIsGDkARwuAC0/9Ij/Q3eRT66B+6qEXvi7QqgsC8wVnCigxCQsy0TyFBRbAO5pAyQ+xRZiqmK6RrGSxZrjymwBcl0q3W5VPfQ6uuMRBUlU8YtULqMF09WzuzSuyOvf64Co9yRkSBUAhUfbkhaAt2ZlqyJZuq3JILxp8yKB8vwF4dYsc8FMm1Uos4AVcS5tZiVBDNgK0C5yKK5+nnIS9rRU26FSRfSs19jQAL5hsXbkbLfQVoL6B36Qj1JAgMBAAE=",
"Addresses": [
"/ip6/::1/tcp/4001/ipfs/QmWkj9HDMMydjjvzjEqcdtVkCgCGyGdShxRvbiaF4G769J",
"/ip4/127.0.0.1/tcp/4001/ipfs/QmWkj9HDMMydjjvzjEqcdtVkCgCGyGdShxRvbiaF4G769J",
"/ip4/192.168.112.185/tcp/4001/ipfs/QmWkj9HDMMydjjvzjEqcdtVkCgCGyGdShxRvbiaF4G769J",
"/ip4/223.255.243.157/tcp/2067/ipfs/QmWkj9HDMMydjjvzjEqcdtVkCgCGyGdShxRvbiaF4G769J",
"/ip4/223.255.243.157/tcp/2073/ipfs/QmWkj9HDMMydjjvzjEqcdtVkCgCGyGdShxRvbiaF4G769J",
"/ip4/223.255.243.157/tcp/2103/ipfs/QmWkj9HDMMydjjvzjEqcdtVkCgCGyGdShxRvbiaF4G769J",
"/ip4/223.255.243.157/tcp/2057/ipfs/QmWkj9HDMMydjjvzjEqcdtVkCgCGyGdShxRvbiaF4G769J",
"/ip4/223.255.243.157/tcp/2062/ipfs/QmWkj9HDMMydjjvzjEqcdtVkCgCGyGdShxRvbiaF4G769J"
],
"AgentVersion": "go-ipfs/0.4.13/",
"ProtocolVersion": "ipfs/0.1.0"
}
当我们执行ipfs name publish命令时,会返回我们的节点ID,你可以通过ipfs id进行查看验证是否是你的节点ID。
tianlei@tianlei:~$ ipfs name resolve QmWkj9HDMMydjjvzjEqcdtVkCgCGyGdShxRvbiaF4G769J
/ipfs/QmRSyV1J8DNwQRGdsYLYkz9ohntYJfehmSDyJkxcxUqy33
当然我们现在就可以通过IPNS进行访问了。
https://ipfs.io/ipns/QmWkj9HDMMydjjvzjEqcdtVkCgCGyGdShxRvbiaF4G769J/
7、发布个人博客