kavo

IPFS 打造免费静态网站

我是在 Linux 下部署安装:https://docs.ipfs.io/install/command-line/#linux

安装完成之后,自定义新建目录,我以 root/index为例:

#在 index 目录执行初始化
ipfs init

成功返回如下:

generating ED25519 keypair...done
peer identity: 12D3KooWSkGBmkxddpwGRYzJGyyizJZxDSWyZSmrRWAEEpkJJkVm
initializing IPFS node at /root/.ipfs
to get started, enter:

        ipfs cat /ipfs/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc/readme

可以看到,上面的提示,然后我进行上面的提示进行 start 测试:

 ipfs cat /ipfs/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc/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

接下来我们开始启动 ipfs daemon ,启动后上传的文件才会分发到公网,但是我们需要在另外一个窗口,所以这里我们使用了 screen

#创建后台进程
screen -S ipfs

#进入目录
cd /home/index

#开启  ipfs daemon
ipfs daemon

#依次按下键盘 Ctrl A D 即可返回,到此 ipfs daemon 已经在后台进程运行了
 -------------------------------------------------------
| Warning:                                              |
|   This is alpha software. Use at your own discretion! |
|   Much is missing or lacking polish. There are bugs.  |
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/127.0.0.1/udp/4001/quic
Swarm listening on /ip4/173.82.227.154/tcp/4001
Swarm listening on /ip4/173.82.227.154/udp/4001/quic
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /ip6/::1/udp/4001/quic
Swarm listening on /p2p-circuit
Swarm announcing /ip4/127.0.0.1/tcp/4001
Swarm announcing /ip4/127.0.0.1/udp/4001/quic
Swarm announcing /ip4/173.82.227.154/tcp/4001
Swarm announcing /ip4/173.82.227.154/udp/4001/quic
Swarm announcing /ip6/::1/tcp/4001
Swarm announcing /ip6/::1/udp/4001/quic
API server listening on /ip4/127.0.0.1/tcp/5001
WebUI: http://127.0.0.1:5001/webui
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
Daemon is ready

然后我们进入 index 目录,在新建一个 web 文件夹然后添加一个 a.txt 文件,然后随便输入内容:

mkdir web
cd web/
touch a.txt
echo 'This is a Test for papa use ipfs' > a.txt 
cat a.txt 
#测试是否写入
This is a Test for papa use ipfs


#开始上传,上传目录记得命令加上 -r

ipfs add -r web/

#以下为输出内容,表示上传成功
added QmaxLxAAdZjjjPudHqCsyVggzoP5JN1HQQxULhczAirYLd web/a.txt
added QmXj3J2PV3yuLo6TZ6dAESnK3xzgVswXn1mT4UUqW7mZPt web
 33 B / 33 B [=================================================================================================================================================] 100.00%

在上传时文件夹,文件夹也会生成一个对应的hash,可以通过hash后接文件名来进行访问, 如上上传生成的 QmXj3J2PV3yuLo6TZ6dAESnK3xzgVswXn1mT4UUqW7mZPt:

#执行命令测试
ipfs cat QmXj3J2PV3yuLo6TZ6dAESnK3xzgVswXn1mT4UUqW7mZPt/a.txt

#输出如下
This is a Test for papa use ipfs

这时我们可以访问它了:https://ipfs.io/ipfs/QmXj3J2PV3yuLo6TZ6dAESnK3xzgVswXn1mT4UUqW7mZPt/a.txt

但是 web 下的内容我们会经常更新或者添加变动,这时上传后的目录 hash 值会产生变化,所以使用IPNS解决文件更新问题:

#我们将以上生成的目录 hash 值 进行 publish

ipfs name publish /ipfs/QmXj3J2PV3yuLo6TZ6dAESnK3xzgVswXn1mT4UUqW7mZPt

输出如下:

Published to k51qzi5uqu5dmga899sxlassh7y1otxog8lfkdl3s4av2gkcth3pax0673i3jc: /ipfs/QmXj3J2PV3yuLo6TZ6dAESnK3xzgVswXn1mT4UUqW7mZPt

以上的:k51qzi5uqu5dmga899sxlassh7y1otxog8lfkdl3s4av2gkcth3pax0673i3jc 就是针对 web 的永久地址了

接下来我们在 web 下新建 index.html 并上传,然后执行 ipfs name publish QmXYrGJDDe59YND3N1CMeihMCNbsEoHwCAHf9EkKdVyxyC ,也就是更新后生成的目录 hash 值

[root@en-novel index]# ipfs add -r web/
added QmaxLxAAdZjjjPudHqCsyVggzoP5JN1HQQxULhczAirYLd web/a.txt
added Qmbe3d46341Boy1SAQTRULjK2YhNMQWoPcZp6XA9wRT59M web/index.html
added QmXYrGJDDe59YND3N1CMeihMCNbsEoHwCAHf9EkKdVyxyC web
 69 B / 69 B [=================================================================================================================================================] 100.00%


[root@en-novel index]# ipfs name publish QmXYrGJDDe59YND3N1CMeihMCNbsEoHwCAHf9EkKdVyxyC

Published to k51qzi5uqu5dmga899sxlassh7y1otxog8lfkdl3s4av2gkcth3pax0673i3jc: /ipfs/QmXYrGJDDe59YND3N1CMeihMCNbsEoHwCAHf9EkKdVyxyC

我们可以看到,发布的值是一样的。

以后每次更新后,然后我们进行 publish ,就可以永久使用访问了:https://ipfs.io/ipns/k51qzi5uqu5dmga899sxlassh7y1otxog8lfkdl3s4av2gkcth3pax0673i3jc

posted on 2021-04-07 18:44  下雨天唱情歌  阅读(9)  评论(0编辑  收藏  举报  来源

导航