How to Upload Files in Rust

How to Upload Files in Rust

Written by: Steve Simkins

Republished from here: https://bit.ly/42WzA7I

There are few languages that get a rouse out of developers more than Rust. When you strip away the memes and the lore behind this language, then you’ll find a robust ecosystem of developer apps, frameworks, even blockchains being built on Rust. In this tutorial, we’ll dip our toes into the deep waters of Rust and walk you through each step, so even if you have never used it before you’ll be safe and sound. Let’s dive in!

Setup

First thing first, you’ll want to make sure your develop environment is ready to go. With that, you’ll just need two things:

  • A code editor like VSCode, Cursor, Zed, etc.

  • Rust installed on your machine

Installing Rust is pretty easy, just use the following cURL script to start the install:

If you’re on Windows, be sure to read the installation instructions here. Once you have Rust installed, try running these commands to make sure everything is working as expected.

Now let’s start up a new project using , a package manager for Rust.

This should create a new folder with our project files; let’s move into that directory and test to make sure everything is working as expected.

This should print a to your terminal, so we’re all set! We’ll edit our files soon, but the next thing we need is our Pinata API key and Gateway URL. If you haven’t already, go ahead and sign up for a free account here. Once you’re signed up, visit the API Keys page and create a new key in the top right. Give it a name that you can remember like then give it a scope of .

Once you create the API key, it will give you a that you will want to save somewhere safe. Last thing we need to do is grab our Dedicated Gateway domain. Just visit the Gateways tab and copy the domain as you see it, and let’s store that with your API key for later.

Implementing Uploads

Now it’s time to actually start uploading some files! The cool thing about Rust is that it compiles to efficient binaries which can be run anywhere on your computer. With our little project we’ll make the smallest CLI that we can use wherever we want to upload a file to our Pinata account! To start, let’s open our project in our text editor and update the file.

There’s a lot going on here, so let’s break it down piece by piece to see what’s going on.

At the top of the file, we have some library imports. Some of them are standard libraries to read arguments from the terminal, read files, and there’s also some third party libraries like , , and to handle API requests, serializing JSON, and handling async applications. Below our imports, we have some structs that will help us parse the upload response we get back and grab the . Now let’s look at our upload function.

This function is going to take in both a and a which will authorize our request. Once we have a path, we’ll use our standard libraries to check the file, open it, and read the file into a buffer. When we prep our API call headers with our to authorize the request, then we start to build the request with . That includes creating form data with and attaching the file buffer as well as the network we want to upload to . Then we simple make the API request with with our form, then parse the response. This function will be used in our main application below.

In this main function, we’ll parse command line arguments for file path, grab the from the system environment as well as the , then run the upload function. If successful, our app will print the resulting CID as well as a gateway link we can use to access it! Now, to test this out, you will need to export the environment variables to your shell like so.

Then try running your command followed by the path to the file you want to upload. If you need something to upload try this image of Pinnie.

If it works, then you should get this printed in the terminal!

How about that - you built a CLI in Rust! Now if we wanted to make this a binary we can run anywhere on our machine you can do the following:

  • Run to build an optimized binary

  • Move the produced binary to a bin folder that’s part of your path, eg.

  • Securely export your and to your system environment using a combination of a password manager like 1Password and your or file, or go deeper in Rust to store your JWT in a secure dot file and have the program pull it from there instead

Now you can run anywhere on your computer to upload a file at blazing Rust speeds 🦀

While it’s unlikely you might ever use Rust again, it’s good practice to try other languages to see how they work. Each one has it’s own specific purpose, and if you’re using Pinata you can rest assured that our APIs will be a breeze to implement no matter what language you choose. Give it a shot today with a free account and experience the best uploading experience for blockchain storage.

Happy Pinning!

To view or add a comment, sign in

Others also viewed

Explore topics