ipfs可以像传统的文件系统一样,将服务上的目录挂载在本地文件系统,方便用户浏览目录内容。网上对于ipfs mount指令的使用没有找到一个完整的教程,因此,在这里写一篇文章分享给大家。
我使用的是操作系统是centos7.9,ipfs使用的是0.13.0
创建文件并上传到ipfs
[root@ipfs study]# mkdir ipfs-test & cd ipfs-test
[root@ipfs ipfs-test]# echo "This first test file" > ipfs-1.txt
[root@ipfs ipfs-test]# echo "This second test file" > ipfs-2.txt
[root@ipfs ipfs-test]# ipfs add -r .
added QmXLe1PCnMhVBDwbuVr4tMhH62J5gjHMEiuw94cx2FM3pK ipfs-test/ipfs-1.txt
added QmZWra4FQSqhkMgPPGfimntoLN7LXan2chc4Voh6WFsiHe ipfs-test/ipfs-2.txt
added QmVrtr5DoFYBBc22MLe9rXWHJF9TtgcSSa9uNiZxCHGmuU ipfs-test
启动ipfs daemon服务
[root@ipfs ipfs-test]# ipfs daemon --mount
Initializing daemon...
go-ipfs version: 0.13.0
Repo version: 12
System version: amd64/linux
Golang version: go1.18.3
......
2022-07-04T11:12:16.596+0800 ERROR fuse/ipns ipns/ipns_unix.go:100 looking up /ipns/k51qzi5uqu5dlca4efnsxpum66zvctp8ptzr58zhre721prvr0hcmk96dqx0n9: could not resolve name
2022-07-04T11:12:16.599+0800 ERROR node node/mount_unix.go:92 error mounting: mountpoint does not exist: /ipfs
2022-07-04T11:12:16.599+0800 ERROR node node/mount_unix.go:96 error mounting: could not resolve name
按照上面的操作,会出现ERROR错误,ipfs服务不能起来,这里需要将上一步产生的目录发布到IPNS。在发布之前还要创建ipfs, ipns的映射目录。再执行ipfs daemon --mount,出现Daemon is ready则可认为成功。
[root@ipfs ipfs-test]# mkdir /ipfs /ipns
[root@ipfs ipfs-test]# ipfs name publish --allow-offline QmVrtr5DoFYBBc22MLe9rXWHJF9TtgcSSa9uNiZxCHGmuU
[root@ipfs ipfs-test]# ipfs daemon --mount
[root@conflux ipfs-test]# ipfs daemon --mount
Initializing daemon...
go-ipfs version: 0.13.0
Repo version: 12
System version: amd64/linux
Golang version: go1.18.3
......
IPFS mounted at: /ipfs
IPNS mounted at: /ipns
Gateway (readonly) server listening on /ip4/0.0.0.0/tcp/8080
Daemon is ready
挂载
另起来一个终端,执行ipfs mount命令,如果执行成功,会在/ipfs目录下出现QmVrtr5DoFYBBc22MLe9rXWHJF9TtgcSSa9uNiZxCHGmuU的目录,下面有第一步创建的两个文件,即挂载成功。
[root@ipfs ipfs-test]# ipfs mount
IPFS mounted at: /ipfs
IPNS mounted at: /ipns
[root@ipfs ipfs-test]# ls /ipfs/QmVrtr5DoFYBBc22MLe9rXWHJF9TtgcSSa9uNiZxCHGmuU
ipfs-1.txt ipfs-2.txt
注意,直接在/ipfs上查看是没有权限的,会报错,因此要指定挂载的目录
[root@conflux ipfs-test]# ls /ipfs/
ls: 正在读取目录/ipfs/: 不允许的操作