foreword
This article mainly introduces how to use IPFS and general operations
1. IPFS development foundation
This part mainly introduces the environment construction and common operations of IPFS.
1. Install IPFS
1.1 Install through the installation package
IPFS has multiple language implementations, the mainstream ones are Go language and JavaScript implementations. This article mainly introduces the Go language version.
Before installing IPFS, you need to install a Go language environment greater than version 1.7.
1. Download the precompiled version of go-ipfs from the IPFS official website (https://dist.ipfs.io/#go-ipfs).
For Mac OS X users, please download darwin Binary amd64.
·Linux system users please download linux Binary amd64.
·Windows system users please download windows Binary amd64.
Domestic users can also obtain the latest installation package through the official open source repository on GitHub: https://github.com/ipfs/go-ipfs/releases.
Take centos as an example:
perform the following operations in sequence:
tar xvfz go-ipfs_v0.4.16_darwin-amd64.tar.gz
cd go-ipfs
./install.sh
Execute the ipfs -help command, the following interface will appear, indicating that the installation is complete
1.2 Install via ipfs-update
ipfs-update is a command-line tool used to update the IPFS version.
We can obtain this command in the following two ways:
1) Download directly from https://dist.ipfs.io/#ipfs-update, unzip the installation package, and run install.sh script to install.
2) If the Go language version is higher than 1.8, it can also be installed directly in the following way:
>go get -u github.com/ipfs/ipfs-update
Through the ipfs-update versions command, you can list all downloadable IPFS versions
>ipfs-update versions
v0.3.2
v0.3.4
v0.3.5
v0.3.6
v0.3.7
v0.3.8
v0.3.9
Update and download the latest go-ipfs version through the ipfs-update install latest command
$ipfs-update install latest
fetching go-ipfs version v0.4.7-rc1
binary downloaded, verifying...
success!
stashing old binary
installing new binary to /home/hector/go/bin/ipfs
checking if repo migration is needed...
Installation complete!
2 IPFS warehouse configuration initialization
2.1 Initialization
1. Use the ipfs init command to initialize the IPFS repository.
$ ipfs init
initializing ipfs node at /Users/daijiale/.go-ipfs
generating 2048-bit RSA keypair...done
peer identity: Qmcpo2iLBikrdf1d6QU6vXuNb6P7hwrbNPW9kLAH8eG67z
to get started, enter:
ipfs cat/ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme
2. Open the readme of IPFS through the instruction of viewing files through IPFS
ipfs cat /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme
After the configuration is successful, the following interface will appear:
2.2 Access configuration files
IPFS local warehouse files are stored in ~/.ipfs path by default
>ls ~/.ipfs blocks datastore keystore config datastore_spec version
The functions of several warehouse archive files are introduced below:
Blocks: CID file block link directory stored in the local warehouse.
·keystore: key pair file storage directory.
·datastore: LevelDB data file directory.
·datastore_spec: LevelDB data file cache directory.
config: Warehouse configuration file. ·version: version information file.
The configuration file is in json format, and we can view and edit the content through the ipfs config show or vim config command.
3. Interact with the IPFS file system
The following will introduce several common ways for local files to interact with the IPFS file system.
1. Add files to IPFS
//切换到本地桌面目录下
$ cd ~/Desktop
//新建ipfs-test文件目录
$ mkdir ipfs-test
//切换到ipfs-test文件目录
$ cd ipfs-test
//新建文件的同时写入一串字符串:"version 1 of my text"
$ echo "version 1 of my text" > mytextfile.txt
//查看mytextfile.txt文件内容
$ cat mytextfile.txt version 1 of my text
//将mytextfile.txt文件添加到IPFS文件系统中
$ ipfs add mytextfile.txt
added QmZtmD2qt6fJot32nabSP3CUjicnypEBz7bHVDhPQt9aAy mytextfile.txt
2. We can read the content of files in the IPFS network through the ipfs cat CID command.
//在IPFS网络中查看验证刚才上传的mytextfile.txt文件内容
$ ipfs cat QmZtmD2qt6fJot32nabSP3CUjicnypEBz7bHVDhPQt9aAy
version 1 of my text
3. Content uniqueness verification
We can directly add the file content to IPFS to test whether the file hash (CID) is in one-to-one correspondence with the file content itself.
We can verify in three ways.
1) Add content to the IPFS file system
$ echo "version 1 of my text" | ipfs add added QmZtmD2qt6fJot32nabSP3CUjicnypEBz7bHVDhPQt9aAy
QmZtmD2qt6fJot32nabSP3CUjicnypEB
2). Add the text of the same content to the IPFS file system
$ ipfs add mytextfile.txt added
QmZtmD2qt6fJot32nabSP3CUjicnypEBz7bHVDhPQt9aAy mytextfile.txt
3). Take out the content in mytextfile.txt and add it to the IPFS file system
$ cat mytextfile.txt | ipfs add added QmZtmD2qt6fJot32nabSP3CUjicnypEBz7bHVDhPQt9aAy
QmZtmD2qt6fJot32nabSP3CUjicnypEB
Through the comparison and verification of the three methods, we can see that as long as the content remains unchanged, the obtained CID will also remain unchanged.
4. Write content file name and directory information in IPFS
Use the ipfs add -w CID command to add mytextfile.txt to IPFS again
$ ipfs add -w mytextfile.txt
added QmZtmD2qt6fJot32nabSP3CUjicnypEBz7bHVDhPQt9aAy mytextfile.txt
added QmPvaEQFVvuiaYzkSVUp23iHTQeEUpDaJnP8U7C3PqE57w
It can be seen that two hash values will be obtained. The first one is the same as before, and the second one stands for IPFS
Wrapped, which includes information such as directory and file name related to the content.
5. Display IPFS Wrapped information
We can get all the information contained in IPFS Wrapped through ipfs ls -v
$ ipfs ls -v QmPvaEQFVvuiaYzkSVUp23iHTQeEUpDaJnP8U7C3PqE57w
Hash Size Name
QmZtmD2qt6fJot32nabSP3CUjicnypEBz7bHVDhPQt9aAy 29 mytextfile.txt
6. Obtain the file content through the content hash of the parent directory
We can get the file content through the following parent directory content hash path format
$ ipfs cat QmPvaEQFVvuiaYzkSVUp23iHTQeEUpDaJnP8U7C3PqE57w/mytextfile.txt
version 1 of my text
4. Join the IPFS network environment
1) Through the ipfs daemon command, we can connect the local IPFS file system to the IPFS network
> ipfs daemon
Initializing daemon...
API server listening on /ip4/127.0.0.1/tcp/5001
Gateway server listening on /ip4/127.0.0.1/tcp/8080
2) If the access is successful, you can
see the IPFS node address information of the peer in the p2p network when running ipfs swarm peers
> ipfs swarm peers
/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx
/ip4/134.121.64.93/tcp/1035/ipfs/QmWHyrPWQnsz1wxHR219ooJDYTvxJPyZuDUPSDpdsAovN5
/ip4/178.62.8.190/tcp/4002/ipfs/QmdXzZ25cyzSF99csCQmmPZ1NTbWTe8qtKFaZKpZQPdTFB
5. Interacting with the HTTP Web
To interact with the HTTP Web, you need to start the daemon process to connect to the network service.
ipfs daemon
1. Obtain IPFS data from the local HTTP gateway
We can enter the following command in the browser
http://localhost:8080/ipfs/Qmd286K6pohQcTKYqnS1YhWrCiS4gz7Xi34sdwMe9USZ7u
2. Get IPFS data from public HTTP gateway
The public gateway refers to the public HTTP network address that can be used to access any content in the IPFS network. The official gateway address of the domain name of ipfs.io is provided.
//ipfs.io由官方提供
https://ipfs.io/ipfs/Qmd286K6pohQcTKYqnS1YhWrCiS4gz7Xi34sdwMe9USZ7u
//ipfs.infura.io 由CONSENSYS团队提供
https://ipfs.infura.io/ipfs/Qmd286K6pohQcTKYqnS1YhWrCiS4gz7Xi34sdwMe9USZ7u
3.IPFS Web Console
IPFS provides a local web console service. Enter http://localhost:5001/webui in the browser to directly access the Web console. As shown in the picture:
6. API use
After IPFS is built, in addition to command line operations, it can also be called by API.
1. IPFS command line usage
The IPFS command line consists of various options such as -config (configuration file path), -debug (Debug mode operation), -help (help document) and a series of (subcommands). The command format is as follows:
ipfs [--config= <config> | -c] [--debug=<debug> | -D] [--help=<help>] [-h=<h>] [--local=<local> | -L
1). Options
The IPFS command line options are as shown in the figure below:
2). Subcommands
The basic commands for operating IPFS are as follows:
the commands for operating IPFS data structures are as follows:
the advanced commands for operating IPFS are as follows:
commands related to operating IPFS network communication:
control The commands of IPFS-related auxiliary tools are as follows:
IPFS has a warehouse in the local file system, and its default location is ~/.ipfs. You can change the warehouse location by setting the environment variable IPFS_PATH
export IPFS_PATH=/path/to/ipfsrepo
【The pit encountered】
After executing the ipfs daemon on the server side, closing the terminal will cause the daemon process to stop accordingly, so we need to create
a service based on the Linux server for the ipfs service 1.
cd /lib/systemd/system/
vim ipfs.service
2. Paste the following code so that IPFS can automatically restart the service after encountering a failure
[Unit]
Description=IPFS
[Service]
ExecStart=/usr/local/bin/ipfs daemon
Restart=always
User=root
Group=root
[Install]
WantedBy=multi-user.target
3. Execute the command after saving and exiting
systemctl start ipfs
The following errors will occur if IPFS is not set across domains
1. Execute the following commands on the console
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT","GET", "POST", "OPTIONS"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials '["true"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Headers '["Authorization"]'
ipfs config --json API.HTTPHeaders.Access-Control-Expose-Headers '["Location"]'
2. Finally, restart IPFS from the command line
systemctl start ipfs
2. Advanced development of IPFS
Summarize
Tip: Here is a summary of the article:
For example: the above is what I will talk about today. This article only briefly introduces the use of pandas, and pandas provides a large number of functions and methods that allow us to process data quickly and easily.