explicitClick to confirm you are 18+

Tutorial - IPFS: Setting up Permanent Remote Server With API

museJun 27, 2018, 6:51:20 AM
thumb_up9thumb_downmore_vert

In this tutorial I will show you how you can use IPFS to create an API server that is permanently available and ready to upload files at all times. The advantage of this is that you can type a single line with curl to upload a file to a remote server, similar to how you might use the linux commands rsync or scp, although, unfortunately, curl does not have a recursive option. Note, you will be able to use this to easily send large files to your friends or even host a website through IPFS. As usual the entire script is available for download.

1. Script Explanation
2. Run as Vultr startup script
3. Calling the API with Curl

1. Script Explanation

The script is actually very similar to the one in my earlier tutorial Tutorial: IPFS - Linux Install. To see the explanation of that script please go there. Below are the differences that are added to the script of that tutorial to make it publicly accessible.

1. A boolean variable that makes an if condition toggleable.

2. The if clause which includes:
    2a. Commands to make the IPFS API globally accessible
        Please especially note here the line /ip4/0.0.0.0/tcp/15371. Best to change this 15371 to some random number between 1025 and 65535. Changing 0.0.0.0 to your personal IP will make the API only accessible from you IP, which would be a good security improvement, but I'm going to leave it globally accessible for now.
    2b. Commands to switch off the firewall. To be honest, this script is extremely vulnerable, but this shouldn't matter on a VPS that costs you $2.5/month if you are not storing private information.

2. Run as Vultr Startup Script

I explained in an earlier blog how to get started with Vultr and I left with an exciting cliffhanger moment telling you that I would later give you a script you can run at startup, meaning that you never actually need to type any code yourself in order to get a service up and running.

You can run the script which is the subject of this tutorial on your own computer, but for stability it's likely going to be better to run this on a Virtual Private Server(VPS) which you can hire for as little as 2.5USD/month. Go to my earlier blog for instructions on how to get started with a private VPS, but this time when you deploy a server, go to the area "Startup Script" and click "Add New".

After clicking "Add New", you will see something like the screen below. First give your script a descriptive name, then delete the text inside the the field that says "Script" and in its place copy and paste the script at the URL https://ipfs.io/ipfs/QmNYjdEDQkKvyNN3rWSSPx5t5pyEYD3yeMrN2zhJm8FKb8.

As I have explained above, you should change the port number from 15371 to something else for a very basic security improvement, but it's not necessary. After you have pasted the text, click "Add Script".

After adding the script, you will be taken back to the page below. Select the script with the name you just added, then click "Deploy Now".

After a few minutes your server will be ready to use without additional configuration, which means you can upload files or even websites to this server from the command line and share them with other people. All for $2.50/mo !

3. Uploading Files

To upload files type the command below, but instead replace the IP address and Port number with the IP Address of your VPS, and replace the Port with the port 15371 or whatever you changed the port to when you added the script.

curl -F file=@<file-name> <ip-address>:<port>/api/v0/add

If you are unsure what your IP address is, click on the tab "Servers" on the Left. You should get a screen looking something like below. The arrow shows you where your IP can be found.

The below command calling the VPS server(I have edited all the IPs in the images for security) and the successful reply I get in response.

If you get the reply given above, you have successfully added a file to your IPFS Server. And you should be able to load the file by copy pasting the hash and adding it to https://ipfs.io/ipfs/<hash>. Thus, the file I uploaded can be accessed from https://ipfs.io/ipfs/QmSi4AYE2otmofbg5yBTG1d8kmP9255wxd4kmRwQEUxK1u

Conclusion

This tutorial has guided you through the process of creating a public facing IP on a highly available Virtual Private Server on the Vultr platform. This allows you to upload arbitrary files from your PC with the curl command, and immediately makes them accessible to the whole world through the gateway https://ipfs.io/ipfs/<file_hash>.

This can be used to upload any files you like, including videos and files you wish to share with your friends, as well as entire websites.

Thank you for reading through to the end, I do hope that you have found this helpful or at least interesting. Please do let me know if you end up using this for anything.