DEV Community

Ankit malik
Ankit malik

Posted on

How to Upload file in S3 using Different Storage classes in Golang

Introduction

I have written blog post in the previous post on using various storage classes in S3.

So, here in this post I will show you that how you can upload any file to S3 using different storage classes.

Code

package main

import (
    "encoding/csv"
    "fmt"
    "math/rand"
    "os"

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

func main() {
    // Generate random CSV data
    data := [][]string{
        {"name", "age", "gender"},
        {"Alice", fmt.Sprint(rand.Intn(50)), "female"},
        {"Bob", fmt.Sprint(rand.Intn(50)), "male"},
        {"Charlie", fmt.Sprint(rand.Intn(50)), "male"},
        {"David", fmt.Sprint(rand.Intn(50)), "male"},
    }

    // Create a new CSV file
    file, err := os.Create("random.csv")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    // Write CSV data to file
    writer := csv.NewWriter(file)
    defer writer.Flush()
    for _, row := range data {
        err := writer.Write(row)
        if err != nil {
            panic(err)
        }
    }

    // Upload file to S3 with GLACIER storage class
    bucket := "my-bucket"
    region := "us-east-1"
    key := "random.csv"
    uploader := s3manager.NewUploader(session.Must(session.NewSession(&aws.Config{
        Region: aws.String(region),
    })))
    _, err = uploader.Upload(&s3manager.UploadInput{
        Bucket:       &bucket,
        Key:          &key,
        Body:         file,
        ACL:          aws.String("private"),
        StorageClass: aws.String("GLACIER"),
    })
    if err != nil {
        panic(err)
    }

    // Archive file to S3 Glacier Service
    svc := glacier.New(session.Must(session.NewSession(&aws.Config{
        Region: aws.String(region),
    })))
    vaultName := "my-vault"
    _, err = svc.UploadArchive(&glacier.UploadArchiveInput{
        Body:      file,
        VaultName: aws.String(vaultName),
    })
    if err != nil {
        panic(err)
    }

    fmt.Println("File uploaded to S3 with Standard-IA storage class and archived to S3 Glacier")
}

Enter fullscreen mode Exit fullscreen mode

Various storage classes placeholder

These are different strings for storage class which can be passed to store the data in S3 in different classes.

STANDARD: Standard storage class for frequently accessed data.
STANDARD_IA: Standard-Infrequent Access storage class for infrequently accessed data.
ONEZONE_IA: One Zone-Infrequent Access storage class for infrequently accessed data that can be recreated.
INTELLIGENT_TIERING: Intelligent Tiering storage class for data with unknown or changing access patterns.
GLACIER: Glacier storage class for long-term data archival.
DEEP_ARCHIVE: Deep Archive storage class for long-term data archival with the lowest cost.
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay