Azure Blob Storage with Golang: Upload, List, Download, and Delete Files

If you’re building backend systems or cloud-native apps with Golang, you’ll eventually need to work with file storage. That’s where Azure Blob Storage comes in — a reliable and scalable place to store images, documents, videos, backups, or any kind of unstructured data.

In this article, we’ll walk through how to:

  • Set up Azure Blob Storage
  • Generate connection strings
  • Use Golang to upload, list, download, and delete blobs (files)

Let’s get started!


☁️ What is Azure Blob Storage?

Azure Blob Storage is Microsoft’s object storage solution for the cloud. It lets you store large amounts of unstructured data — like photos, documents, or logs — and access them over HTTP/HTTPS.

It’s commonly used in:

  • Web apps to store user uploads
  • Backup systems for logs and media
  • Serverless functions to process files

You interact with Blob Storage through containers (like folders), and each container holds blobs (your files).


🛠️ Setting Up Azure Blob Storage

Before we jump into code, let’s set up a blob container in Azure.

✅ Step 1: Create a Storage Account

  1. Go to the Azure Portal
  2. Search for Storage Accounts
  3. Click Create
  4. Choose your subscription, resource group, and name
  5. Leave most options as default and hit Review + Create

✅ Step 2: Create a Container

  1. Open your storage account
  2. Go to the Containers tab
  3. Click + Container
  4. Give it a name like demo-container
  5. Set Public Access Level to Private

✅ Step 3: Get the Connection String

  1. In your storage account, go to Access Keys
  2. Copy the Connection String (you’ll need this in your Go code)

👨‍💻 Writing Go Code for Azure Blob Storage

We’ll use the Azure SDK for Go to interact with blob storage, and joho/godotenv to load environment variables from .env files

You’ll learn how to:

  • Upload a file
  • List all blobs
  • Download a blob
  • Delete a blob

Initialise the Go project from the terminal and import the Azure SDK for GO and joho/godotenv library

✅ main.go – Orchestrates the Entire Flow
This is the entry point of the application — the script you run using go run main.go.

What it does:

Loads environment variables from a .env file using godotenv.

Sets values like container name, blob name, file path, and download location.

Calls all the major operations in sequence:

UploadBlob() – Uploads a file to Azure Blob Storage

ListBlob() – Lists all blobs in the specified container

DownloadBlob() – Downloads the uploaded blob to a local path

DeleteBlob() – Deletes the blob from Azure

This file is great for testing or demoing the full end-to-end blob flow.

📤 upload.go – Upload a File to Azure Blob
This file contains the UploadBlob function that handles uploading any local file to a blob container.

How it works:

Reads the Azure Storage connection string from the environment.

Opens the specified local file using os.Open().

Uses the Azure SDK’s BlockBlobClient.UploadStream() to upload the file to the specified blob name.

Useful for: Image uploads, backups, logs, user files — any case where your app needs to push data to the cloud.

📃 list.go – List All Blobs in a Container
This file defines the ListBlob function which fetches all blob names in a container.

What’s going on inside:

Initializes a container client using your connection string.

Uses Azure’s NewListBlobsFlatPager to paginate through all files.

Collects blob names in a slice and returns them.

Useful for: Dashboards, UIs, cleanup jobs, or APIs that need to show stored files.


📥 download.go – Download a Blob to Local File System
This file contains the DownloadBlob function which pulls a blob from Azure Blob Storage and saves it to your local machine.

Key operations:

Creates a blob client for the given file.

Downloads the blob using DownloadStream.

Writes the data to a local file via os.Create and io.Copy.

Useful for: Archiving, processing uploaded content, or syncing blobs to your app.


🗑️ delete.go – Delete a Blob from Azure
This file defines the DeleteBlob function which removes a specific blob from Azure.

How it works:

Connects to Azure Blob Storage using the environment variable.

Initializes a blob client for the target blob.

Calls .Delete() to remove it.

Useful for: Cleanup operations, user file management, or versioned blob workflows.

✅ Final Notes

That’s it! You now have a basic Golang implementation to interact with Azure Blob Storage. You can easily integrate these functions into larger applications or serverless functions.

📚 GitHub Repo

All code is available here: https://github.com/kale-swapnil/azure-blob-storage-golang

If you found this article useful, share it with other developers, implement your version and comment below or reach out to me to discuss.

We will be happy to hear your thoughts

Leave a reply

cloudsxp.com
Logo