Google Cloud Storage (GCS) + GoLang - File Upload, Download, List, and Delete Example


To set up and use Google Cloud Storage in GoLang for uploading, listing, downloading, and deleting files, follow these steps:


1. Set Up Google Cloud Project

  1. Create a Google Cloud Project:

  2. Enable Google Cloud Storage API:

    • Navigate to APIs & Services > Library in the console.
    • Search for "Cloud Storage" and enable it for your project.
  3. Create a Storage Bucket:

    • Navigate to Storage > Browser in the console.
    • Click Create Bucket, give it a unique name, choose a location, and configure other settings.
  4. Set Up a Service Account:

    • Go to IAM & Admin > Service Accounts.
    • Create a new service account with the Storage Admin role.
    • Generate a key (JSON) and download it to your local machine.

2. Install the GoLang Google Cloud Storage Library

Install the official library using go get:

go get cloud.google.com/go/storage

3. Configure Authentication

Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to the service account key:

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your-service-account-key.json"

4. Write the Complete Code

Below is a complete GoLang program for uploading, listing, downloading, and deleting files in Google Cloud Storage.

package main

import (
	"context"
	"fmt"
	"io"
	"os"

	"cloud.google.com/go/storage"
)

const (
	projectID  = "your-project-id"  // Replace with your Google Cloud Project ID
	bucketName = "your-bucket-name" // Replace with your Storage Bucket name
)

func main() {
	// Test each function
	uploadErr := uploadFile("example.txt", "example.txt")
	if uploadErr != nil {
		fmt.Printf("Error uploading file: %v\n", uploadErr)
	}

	files, listErr := listFiles()
	if listErr != nil {
		fmt.Printf("Error listing files: %v\n", listErr)
	} else {
		fmt.Println("Files in bucket:", files)
	}

	downloadErr := downloadFile("example.txt", "downloaded_example.txt")
	if downloadErr != nil {
		fmt.Printf("Error downloading file: %v\n", downloadErr)
	}

	deleteErr := deleteFile("example.txt")
	if deleteErr != nil {
		fmt.Printf("Error deleting file: %v\n", deleteErr)
	}
}

// UploadFile uploads a file to Google Cloud Storage
func uploadFile(objectName, filePath string) error {
	ctx := context.Background()
	client, err := storage.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("failed to create storage client: %v", err)
	}
	defer client.Close()

	bucket := client.Bucket(bucketName)
	object := bucket.Object(objectName)

	file, err := os.Open(filePath)
	if err != nil {
		return fmt.Errorf("failed to open file %s: %v", filePath, err)
	}
	defer file.Close()

	writer := object.NewWriter(ctx)
	if _, err := io.Copy(writer, file); err != nil {
		return fmt.Errorf("failed to write file to bucket: %v", err)
	}

	if err := writer.Close(); err != nil {
		return fmt.Errorf("failed to close writer: %v", err)
	}

	fmt.Printf("Uploaded %s to %s as %s\n", filePath, bucketName, objectName)
	return nil
}

// ListFiles lists all files in the bucket
func listFiles() ([]string, error) {
	ctx := context.Background()
	client, err := storage.NewClient(ctx)
	if err != nil {
		return nil, fmt.Errorf("failed to create storage client: %v", err)
	}
	defer client.Close()

	var fileNames []string
	it := client.Bucket(bucketName).Objects(ctx, nil)
	for {
		attr, err := it.Next()
		if err == storage.Done {
			break
		}
		if err != nil {
			return nil, fmt.Errorf("error during iteration: %v", err)
		}
		fileNames = append(fileNames, attr.Name)
	}

	return fileNames, nil
}

// DownloadFile downloads a file from Google Cloud Storage
func downloadFile(objectName, destPath string) error {
	ctx := context.Background()
	client, err := storage.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("failed to create storage client: %v", err)
	}
	defer client.Close()

	bucket := client.Bucket(bucketName)
	object := bucket.Object(objectName)

	reader, err := object.NewReader(ctx)
	if err != nil {
		return fmt.Errorf("failed to create object reader: %v", err)
	}
	defer reader.Close()

	file, err := os.Create(destPath)
	if err != nil {
		return fmt.Errorf("failed to create file: %v", err)
	}
	defer file.Close()

	if _, err := io.Copy(file, reader); err != nil {
		return fmt.Errorf("failed to copy object data to file: %v", err)
	}

	fmt.Printf("Downloaded %s to %s\n", objectName, destPath)
	return nil
}

// DeleteFile deletes a file from Google Cloud Storage
func deleteFile(objectName string) error {
	ctx := context.Background()
	client, err := storage.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("failed to create storage client: %v", err)
	}
	defer client.Close()

	object := client.Bucket(bucketName).Object(objectName)

	if err := object.Delete(ctx); err != nil {
		return fmt.Errorf("failed to delete object: %v", err)
	}

	fmt.Printf("Deleted %s from bucket %s\n", objectName, bucketName)
	return nil
}

5. Run the Program

Save the code to a file (e.g., main.go), and execute it:

go run main.go

6. Key Notes

  • Replace placeholders:
    • your-project-id with your GCP project ID.
    • your-bucket-name with the name of your Cloud Storage bucket.
  • Ensure you have the correct permissions for your service account:
    • Storage Admin for full control or Storage Object Admin for specific file operations.
  • Validate the file paths (example.txt, downloaded_example.txt) exist in the correct directory.

This setup will give you a functional GoLang program to handle file operations in Google Cloud Storage.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete