Simple Golang API File Uploader Using Google Cloud Storage

Image credits to CHUTTERSNAP (https://unsplash.com/photos/xewrfLD8emE)

What on earth is this writting? This tutorial will demonstrate a simple golang API for uploading file to Google Cloud Storage. GCS (Google Cloud Storage) can be used for uploading your image, files, assets, basically any file with the benefits of

Cloud Storage Benefits (I am not selling it). Credits to the website https://cloud.google.com/storage
  1. Create Google Cloud Storage Bucket
  2. Create golang API to upload the file
  3. Conduct testing to upload a file using the API
  4. Check the uploaded file in the GCP

All steps will be detailed below.

You can access this link for a quick one (https://cloud.google.com/storage). Go to the console menu, then create a bucket. Choose a bucket name, pick anything you want as long as it’s unique. When you choose region and default storage, choose the pre-chosen one. Any details on each configuration can be read more on the documentation. For this example I created a bucket named “navy-1210” with south asia region and standard storage class.

After creating the bucket, you need to generate service account in which required for authenticating your application for the GCP. Go to this link https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-go for details, and choose “Create Service Account Key” like the image below.

Pick New service account with role of cloud storage → storage object creator. Then click “Create”. After It’s done, you will automatically download a JSON file that you need to keep it for yourself to authenticate later.

We will create a golang simple API in which receive one file from FormFile to be uploaded to cloud storage. The flow consists of:

  1. Init application credentials needed by GCP to authenticate your app using “GOOGLE_APPLICATION_CREDENTIALS” environment variables to point out the JSON file directory location.
  2. Create client uploader instance with pre-defined bucket name, project id (your GCP project ID), and upload path (we will use “test-files/”)
  3. Create a POST API (path: “/upload”) that receives “file_input” Form File
  4. Trigger upload file to GCP with concatenated upload path as the prefix
  5. Return success after all process run without error

The full code can be seen below:

I’m using insomnia to test the API, choose a file to be uploaded, on this test I tried to upload an image of honey badger, and it showed that upload was successful.

Open your project GCP console, and you can see the test-files folder is created automatically with your file inside of it.

Ignore the “img/” folder, It’s mine and nothing to do with this project

If we want people (publicly) able to access the image / file, we can set it to public via editing the file permission, and add “all user” access.

After we set the image into public

Copy-ing the URL and you can see the image by opening this image on new tab:

https://storage.googleapis.com/navy-1210/test-files/honeybadger.jpg

Then, you can create any creative things you have in mind :)

Fellow Software Engineer