如何使用Golang中的不同存储类上传S3中的文件
#aws #go #s3 #s3glacier

介绍

我已经在上一篇文章中写了有关使用various storage classes in S3的博客文章。

所以,在这篇文章中,我将向您展示如何使用不同的存储类上传任何文件。

代码

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")
}

各种存储班占位符

这些是存储类的不同字符串,可以将数据传递到不同类中的S3中。

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.