Published in


Golang: Image Upload With Google Cloud Storage and Gin (Part 2)

I hope you have successfully completed Part 1: Golang: Image Upload With Google Cloud Storage and Gin. Before jumping into coding, lets first have a brief discussion about Go programming language and Gin framework.

Prerequisites: Go Installation (Follow instructions -> CLICK HERE)

Official website https://golang.org/ defines Go as :

“an open source programming language that makes it easy to build simple, reliable, and efficient software.”

Gin Web Framework a.k.a Gin-Gonic as described by creators:

“a web framework written in Go (Golang). It features a martini-like API with performance that is up to 40 times faster thanks to httprouter.

So lets start creating our API to upload image in Google Cloud Storage.

Make sure you have Go installed in your system. Confirm by typing:

go version

Setting up Development Environment

Create a new directory by typing the following commands in your terminal

mkdir go-cloud && cd go-cloud

If you are using Visual Studio Code as your Code Editor you can type the command below to open up the editor.

code .

Creating New Project

To initialize the project run the following command.

go mod init package-name-you-want
In my case: go mod init go-cloud-storage

The above command generates go.mod file in the current directory. This file contains the module name and the packages we use in the project.

Installing required packages

Since we are using gin-gonic framework and google cloud storage we need to add the packages by typing the following commands.

go get github.com/gin-gonic/gingo get -u cloud.google.com/go/storage

Create main.go file in the root directory.

touch main.go

Creating a server

Copy the code below in your main.go file

Run the command below to start the server.

# run main.go and visit ("localhost:8080/ping") on browsergo run main.go
Response from the server.

We have the server up and running. By default it runs in port :8080.

Create a folder and a file inside it to store handler function in root directory.

mkdir cloudbucket && cd cloudbucket && touch cloudBucket.go

cloudBucket.go file consists of HandleFileUploadToBucket function. Edit your file as snippet below:

Lets add a POST route in our main.go file.

Finally add the downloaded Service Account Key (.json) file from the Part 1 to the root directory where your main.go file is. (I have renamed it to “keys.json”).

Project Structure

It’s time to test our API for image upload. Close and rerun the server after saving all files from the root directory i.e where your main.go file is.

API endpoint for file upload:


Here, I’ve used Postman to test the endpoint. Send POST request to the above mentioned endpoint using form-data attaching file with key as “file”.

Request and Response from the server (SUCCESS)

We can see the image being successfully upload to our bucket.

Reference : Google Cloud Platform (Go samples)

In this tutorial we have learned to configure the Google Cloud Storage and upload file using Go programming language and gin-gonic framework.

If you have not read Part 1: Golang: Image Upload With Google Cloud Storage and Gin

For complete code: UjjwolKayastha/GO-GIN-GCS

Happy Coding. 🙇🏻




Visionary Development Team. We ❤︎ Technology!

Recommended from Medium

V2 Getting Closer!

Vga to hdmi adapter driver windows 10

Design Patterns — Builder Pattern

Walking Through Git:

Cloud Migration and Transformation: The Start of the Cloud Journey

Q&A after Megrez testnet launched one week

WebP Image Optimization and the Faster Modern Web

Accident Lawyer Sandy Utah

accident lawyer sandy utah

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ujjwol Kayastha

Ujjwol Kayastha

More from Medium

Simple data alignment technique to Speed Up Your Struct in Golang

Password Hashing with Golang and PostgreSQL

Automation — Dynamic sitemap generation with Golang API

Automation — Dynamic sitemap generation with Golang API

SimpleApp — Connect To MongoDB (Part II)