MinIO: Connecting Go to MinIO

This documentation is part of the Getting started guide. You can view the complete guide here: How to begin using MinIO for S3-compatible object storage.

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use MinIO Object Storage solution that provides a host of benefits, including:

  • Unlimited transfers.
  • Simple, predictive, and transparent pricing.
  • Customizable domain name secured with HTTPS (for example, https://object-storage.your-company.com).
  • Effortless updates with just a click.
  • Optimal performance and robust security powered by a private and dedicated VM.
  • Available in 🇪🇺 Europe and 🇺🇸 USA.

Save time and simplify your life: it only takes 5 minutes to try Stackhero's MinIO Object Storage hosting solution!

There are two options for connecting Go applications to MinIO.

First, install the AWS SDK packages for Go:

go get github.com/aws/aws-sdk-go-v2
go get github.com/aws/aws-sdk-go-v2/config

Below is an example of how to connect to MinIO from Go using the AWS SDK:

package main

import (
  "fmt"
  "os"
  "strings"

  "github.com/aws/aws-sdk-go/aws"
  "github.com/aws/aws-sdk-go/aws/credentials"
  "github.com/aws/aws-sdk-go/aws/session"
  "github.com/aws/aws-sdk-go/service/s3"
  "github.com/aws/aws-sdk-go/service/s3/s3manager"
)

func main() {
  bucket := aws.String("newbucket")
  key := aws.String("testobject")

  // Configure to use the MinIO server
  s3Config := &aws.Config{
    Credentials:      credentials.NewStaticCredentials("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY", ""),
    Endpoint:         aws.String("https://<XXXXXX>.stackhero-network.com"),
    Region:           aws.String("us-east-1"),
    DisableSSL:       aws.Bool(false),
    S3ForcePathStyle: aws.Bool(true),
  }
  newSession := session.New(s3Config)

  s3Client := s3.New(newSession)

  cparams := &s3.CreateBucketInput{
    Bucket: bucket,
  }

  // Create a new bucket
  _, err := s3Client.CreateBucket(cparams)
  if err != nil {
    fmt.Println(err.Error())
    return
  }

  // Upload a new object with the string "Hello from MinIO!"
  _, err = s3Client.PutObject(&s3.PutObjectInput{
    Body:   strings.NewReader("Hello from MinIO!"),
    Bucket: bucket,
    Key:    key,
  })
  if err != nil {
    fmt.Printf("Failed to upload data to %s/%s, %s\n", *bucket, *key, err.Error())
    return
  }
  fmt.Printf("Successfully created bucket %s and uploaded data with key %s\n", *bucket, *key)

  // Retrieve our object and store it locally
  file, err := os.Create("testobject_local")
  if err != nil {
    fmt.Println("Failed to create file", err)
    return
  }
  defer file.Close()

  downloader := s3manager.NewDownloader(newSession)
  numBytes, err := downloader.Download(file,
    &s3.GetObjectInput{
      Bucket: bucket,
      Key:    key,
    })
  if err != nil {
    fmt.Println("Failed to download file", err)
    return
  }
  fmt.Println("Downloaded file", file.Name(), numBytes, "bytes")
}

For additional reading, visit the MinIO Go documentation.

To use the MinIO SDK for Go, install the following package:

GO111MODULE=on go get github.com/minio/minio-go/v7

Below is an example showing how to connect to MinIO using the MinIO SDK for Go:

package main

import (
  "log"

  "github.com/minio/minio-go/v7"
  "github.com/minio/minio-go/v7/pkg/credentials"
)

func main() {
  endpoint := "<XXXXXX>.stackhero-network.com"
  accessKeyID := "YOUR_ACCESS_KEY"
  secretAccessKey := "YOUR_SECRET_KEY"
  useSSL := true

  // Initialize the MinIO client
  minioClient, err := minio.New(endpoint, &minio.Options{
    Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
    Secure: useSSL,
  })
  if err != nil {
    log.Fatalln(err)
  }

  log.Printf("%#v\n", minioClient) // minioClient is now set up
}

For more information, please see the MinIO Go SDK documentation.