0%

StudyRecord-IPFS使用入门

安装:

下载链接

Linux 平台下,使用以下命令进行安装:

1
2
3
tar xvfz kubo_v0.17.0_linux-amd64.tar.gz
cd kubo
./install.sh

初始化:

安装完成之后,要使用IPFS第一步是要对IPFS进行初始化

1
2
3
4
5
6
7
[root@localhost kubo]# ipfs init
generating ED25519 keypair...done
peer identity: 12D3KooWCmmHYHQSZ2woV7euLdFenHDj1g6PtmkQpzTJYGtZM9mo
initializing IPFS node at /root/.ipfs
to get started, enter:

ipfs cat /ipfs/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc/readme

配置修改:

修改配置文件:

1
2
3
4
5
6
7
8
vim ~/.ipfs/config

# 修改为内网IP
"Addresses": {
"API": "/ip4/192.168.2.23/tcp/5001",
"Announce": [],
"AppendAnnounce": [],
"Gateway": "/ip4/192.168.2.23/tcp/8080",

允许跨域:

1
2
[root@localhost ~]# ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["http://192.168.2.23:5001", "http://localhost:3000", "http://127.0.0.1:5001", "https://webui.ipfs.io"]'
[root@localhost ~]# ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "POST"]'

上传文件:

1
> echo "HelloWorld" >> upchain.pro.txt

ipfs 使用add 命令来添加内容到节点中,

1
2
3
[root@localhost ~]# ipfs add upchain.pro.txt
added QmfJs3ZYScPctKNfbYDYbkHX6akxgshtn6cPSEj9prqdQz upchain.pro.txt
11 B / 11 B [==============================================================================================================================] 100.00%

当它文件添加到节点时,会为文件生成唯一的hash:QmfJs3ZYScPctKNfbYDYbkHX6akxgshtn6cPSEj9prqdQz, 可以使用ipfs cat查看文件的内容:

1
2
[root@localhost ~]# ipfs cat QmfJs3ZYScPctKNfbYDYbkHX6akxgshtn6cPSEj9prqdQz
HelloWorld

开启服务:

此时文件仅仅是上传在本地的IPFS节点中,如果需要把文件同步到网络,就需要开启 daemon 服务, 使用命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@localhost ~]# ipfs daemon
Initializing daemon...
Kubo version: 0.17.0
Repo version: 12
System version: amd64/linux
Golang version: go1.19.1
2022/11/25 01:12:55 failed to sufficiently increase receive buffer size (was: 208 kiB, wanted: 2048 kiB, got: 416 kiB). See https://github.com/lucas-clemente/quic-go/wiki/UDP-Receive-Buffer-Size for details.
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/192.168.122.1/tcp/4001
Swarm listening on /ip4/192.168.122.1/udp/4001/quic
Swarm listening on /ip4/192.168.2.23/tcp/4001
Swarm listening on /ip4/192.168.2.23/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/192.168.2.23/tcp/4001
Swarm announcing /ip4/192.168.2.23/udp/4001/quic
Swarm announcing /ip6/::1/tcp/4001
Swarm announcing /ip6/::1/udp/4001/quic
API server listening on /ip4/192.168.2.23/tcp/5001
WebUI: http://192.168.2.23:5001/webui
Gateway (readonly) server listening on /ip4/192.168.2.23/tcp/8080
Daemon is ready

Refs: