Golang MinIO Integration

golang minio

In this tutorial we are gonna explore how to integrate MinIO as Object Storage with Golang.

So Let’s Start !

Requirements:

  • Golang installed on your system. (golang 1.23.4 preferred)
  • Docker and Docker compose installed on your system.

Step 1: Project init

Create a folder named “golang-minio-integration” wherever you want on your system. we call this project root from now on.

Step 2: Docker compose file

Inside your project root, create a file name “docker-compose.yml” with this content:

This is a docker compose file and has two containers, one for minio self hosted and one for your golang project.

Step 3: Dockerfile

Inside your project root create another file named “Dockerfile” without any extensions with this content:

Step 4: app directory

Inside project root, create a folder named “app”.

Step 5: go mod init

Open your terminal inside “app” directory and run this command:

This will initialize a golang project in the “app” directory inside project root.

Step 6: MinIO dependency

Install the “minio” client library with this command:

Step 7: minioclient package

Create a folder named “minioclient” inside the app directory.

Step 8: minio_client.go implementation

Create a file named “minio_client.go” inside the “minioclient” folder and put these content into it:

As you can see we are creating some functions for creating a bucket, uploading file and downloading file from “minio” object storage.

Step 9: main.go implementation

Now go back to “app” directory and create a file named “main.go” which is the entry point of your golang application, and put these contents in it:

As you can see first we are creating a minio client and then we are creating two handlers for upload and download, which these handlers use the “minioclient” package we defined earlier.

Step 10: Make it up and running !

Now everything is fine, let’s run the project by this command:

Step 11: Testing

Let’s test the project, to do this open a folder in your computer wherever you have a picture for example. I have a test.jpg file inside my computer, so I’ll go into the directory that contains this image and open terminal there, and then I run this command:

And I get this result:

So as you can see the upload is working fine.

Now let’s test download, to test download, try to remove your test.jpg file from the directory.

And then run this command:

And I get this:

I can see that the download has been completed and the file exist in my computer in the directory I ran these commands.

Summary:

So this is it !

By the way the code exists in my repo:

https://github.com/mjmichael73/golang-minio-integration

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *