preface

It didn’t take long to get into the awkward situation of having to cite a lot of pictures.

If all the pictures are uploaded to Github, the later code repository will become more and more bloated. In China, where the Internet speed is very anxious, a better way is needed to solve this problem.

The first thought is to use the object storage service and then, in conjunction with the CDN, deploy a graph bed.

Through many aspects of understanding, Qiniu and Tencent cloud are good choices (Ali Cloud is too expensive, I passed directly).

What follows is a wave of painful things 😭.

Seven Cows will give away 10GB of space for free, but its temporary domain name will only last 30 days and refresh every 30 days 🤒.

If want permanent, need oneself to buy a domain name, and undertake public security net puts on record. (Filing is really a hassle…)

Tencent cloud a little better, temporary domain name is not limited, made a configuration, can barely use.

After a few days, I wondered if there was a better way.

Due to my work, I came into contact with IPFS and had some understanding of it before, so I started to try decisively whether I could open a node on my own server through IPFS and upload pictures to ipFS public network, so that I could access them anytime and anywhere, similar to realizing the function of CDN.

The preparatory work

All you need is a server with at least 10GB of hard disk, but the bigger the better, ipFS is a huge memory hog.

The body of the

First we need to log in to the server through SSH to install IPFS.

My server is Ubuntu 18.04.

If you are using another system, go to the official installation guide.

# update packages
sudo apt update

# install golang
sudo apt install golang-go -y

# install git
sudo apt install git -y

# install ipfs-update
go get -u github.com/ipfs/ipfs-update

# add ~/go/bin into .bash_profile
echo "export PATH=$PATH:$HOME/go/bin" >> .bash_profile

# add GOPATH into .bash_profile
echo "export GOPATH=$HOME/go" >> .bash_profile

source .bash_profile
Copy the code

Install ipfs:

$ipfs-update install latest fetching -ipfs version v0.4.20 success! tests all passed. installing new binary to /home/Like/go/bin/ipfs checkingif repo migration is needed...

Installation complete!
Copy the code

Initializing IPFS:

$ ipfs init initializing IPFS node at /home/Like/.ipfs generating 2048-bit RSA keypair... done peer identity: QmS5DQXifPi4cBRDYhVLWbSAYcMEvt4J6RdBs4mCMr7oUP
to get started, enter:

        ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme
Copy the code

QmS5DQXifPi4cBRDYhVLWbSAYcMEvt4J6RdBs4mCMr7oUP this long the node id of character is what you get.

Don’t worry about losing it, you can always use the IPfs ID command line to check.

Now we can start the IPFS process:

$ ipfs daemon Initializing daemon... Go-ipfs version: 0.4.20- Repo version: 7 System version: AMD64 / Linux Golang version: Go1.12.4 Swarm listening on /ip4/10.152.0.2/ TCP /4001 Swarm listening on /ip4/127.0.0.1/ TCP /4001 Swarm listening on /ip6/::1/ TCP /4001 Swarm listening on /p2p-circuit Swarm announcing /ip4/10.152.0.2/ TCP /4001 Swarm announcing Swarm announcing /ip6/::1/ TCP /4001 API Server Listening on /ip4/127.0.0.1/ TCP /5001 WebUI: Gateway (http://127.0.0.1:5001/webuireadonlyServer listening on/IP4/127.0.0.1 / TCP /8080 Daemon is readyCopy the code

We can get the following information:

  1. The authorities provided oneWebUI: http://127.0.0.1:5001/webuiIt allows us to operate on command.
  2. We have a local gatewayGateway (readonly) Server Listening on /ip4/127.0.0.1/ TCP /8080Allows you to access files on the public network through CID.

The next step is simply to enable the Nginx service, which allows us to access these two addresses from the Internet, and achieve the equivalent of object storage.

First we CTRL + C to exit the ipFS process currently running.

To achieve background operation:

$ ipfs daemon --enable-gc > ipfs.log &
[1] 5497
Copy the code

Next we install nginx:

sudo apt install nginx -y
Copy the code

After the installation is complete, modify the configuration through VIm. The configuration is provided below, just copy it directly.

sudo vim /etc/nginx/sites-available/default
Copy the code
server { server_name _; Location / {proxy_pass http://127.0.0.1:5001; } location /ipfs/ {proxy_pass http://127.0.0.1:8080; }}Copy the code

Start nginx service:

sudo service nginx start
Copy the code

To view the IPFS UI, visit [your server IP]/webui in the local browser 😃.

The first load may be slow, please wait patiently.

If the load fails, restart nginx.

sudo service nginx restart
Copy the code

You can see two lines of commands on the UI, copy them, and execute them on the server.

# excute copied command
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '[" http:// [your] server IP ", "http://127.0.0.1:5001", "https://webui.ipfs.io"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]'

# restart ipfs daemon
kill -9 $(ps | grep 'ipfs' | cut -d' ' -f1)
ipfs daemon --enable-gc > ipfs.log &
Copy the code

You can use [your server IP]/ipfs/[CID] to access your uploaded files 🎉.


If you like this article, please follow me on my blog.