pseudoyu

pseudoyu

Blockchain | Programming | Photography | Boyi
github
twitter
telegram
mastodon
bilibili
jike

IPFS Local Node Setup (Command Line)

Introduction#

The previous article "IPFS Distributed File Storage Principles" provided a detailed introduction to the design concepts, functions, working principles, and IPNS of the IPFS system. So, how do we set up a local IPFS node?

This article sets up an IPFS node (command line version) on macOS 11.2.3 system, and performs practical operations such as file upload, download, network synchronization, pinning, garbage collection (GC), and IPNS to deepen the understanding of IPFS working principles.

Code Practice#

Installation#

wget https://dist.ipfs.io/go-ipfs/v0.8.0/go-ipfs_v0.8.0_darwin-amd64.tar.gz
tar -xvzf go-ipfs_v0.8.0_darwin-amd64.tar.gz
cd go-ipfs
./install.sh
ipfs --version

Start#

# Initialize the node
ipfs init

# Upload a file
ipfs add ipfs_init_readme.png

# Upload a file and only output the hash value
ipfs add -q ipfs_init_readme.png

# Upload a directory
ipfs add -r [Dir]

# View a file
ipfs cat /ipfs/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc/readme
ipfs cat /ipfs/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc/quick-start

# View your own uploaded file
ipfs cat QmaP3QS6ZfBoEaUJZ3ZfRKoBm3GGuhQSnUWtkVCNc8ZLTj

# View an image and output it to a file
ipfs cat QmfViXYw7GA296brLwid255ivDp1kmTiXJw1kmZVsg7DFH > ipfsTest.png

# Download a file
ipfs get QmfViXYw7GA296brLwid255ivDp1kmTiXJw1kmZVsg7DFH -o ipfsTest.png

# Compress and download a file
ipfs get QmfViXYw7GA296brLwid255ivDp1kmTiXJw1kmZVsg7DFH -Cao ipfsTest.png

ipfs_init_readme

Start/Join Services#

# View current node information
ipfs id

# View IPFS configuration information
ipfs config show

# Start the node server
ipfs daemon

The API service runs on port 5001 by default and can be accessed at http://localhost:5001/webui.

ipfs_webui

The gateway service runs on port 8080 by default. When accessing files in a browser, you need to use the IPFS gateway service. The browser first accesses the gateway, which then retrieves the file from the IPFS network. Use http://localhost:8080/ipfs/[File Hash] to access files uploaded to IPFS.

File Operations#

# List files
ipfs files ls

# Create a directory
ipfs files mkdir

# Delete a file
ipfs files rm

# Copy a file
ipfs files cp [File Hash] /[Dest Dir]

# Move a file
ipfs files mv [File Hash] /[Dest Dir]

# Status
ipfs files stat

# Read
ipfs files read

Using IPNS to Solve File Update Issues#

# Publish content using IPNS for automatic updates
ipfs name publish [File Hash]

# Query the hash pointed to by the node ID
ipfs name resolve

# If there are multiple sites that need to be updated, you can generate a new key pair and publish using the new key
ipfs key gen --type=rsa --size=2048 mykey
ipfs name publish --key=mykey [File Hash]

Pinning#

When we request a file from the IPFS network, IPFS first synchronizes the content locally to provide the service. The cache mechanism is used to handle files to prevent storage space from continuously growing. If a file is not used for a period of time, it will be "garbage collected". Pinning ensures that the file is not "garbage collected" locally.

# Pin a file
ipfs pin add [File Hash]

# Check if a hash is pinned
ipfs pin ls [File Hash]

# Remove the pin status
ipfs pin rm -r [File Hash]

# GC operation
ipfs repo gc

Conclusion#

This article mainly deploys an IPFS file system locally and tries out basic operations. It is based on macOS 11.2.3 and go-ipfs_v0.8.0_darwin-amd64 version. Different system operations may vary due to different versions or dependency issues. If there are any errors or omissions, please feel free to communicate and correct.

References#

  1. IPFS Official Website
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.