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

Top comments (0)