Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.7 kB
1
Indexable
Never
package main

import (
    "context"
    "fmt"
    "log"
    "os"
    "path/filepath"
    "strings"

    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/s3"
)

const (
    bucketName = "your-s3-bucket-name"   // S3 bucket name
    bucketPath = "folder1/folder2"       // Path inside the bucket (with subfolders)
)

func main() {
    // Load AWS configuration using the profile that has role_arn
    cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigProfile("my-assumed-role-profile"))
    if err != nil {
        log.Fatalf("unable to load AWS SDK config, %v", err)
    }

    // Create S3 client
    s3Client := s3.NewFromConfig(cfg)

    // Find directories starting with "2023"
    baseDir := "./"
    err = filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
        if err != nil {
            return err
        }

        // Check if it's a directory and starts with "2023"
        if info.IsDir() && strings.HasPrefix(info.Name(), "2023") {
            fmt.Printf("Found directory: %s\n", path)
            // Upload directory to S3
            err := uploadDirectoryToS3(s3Client, path)
            if err != nil {
                log.Printf("Failed to upload %s: %v", path, err)
            }
        }
        return nil
    })

    if err != nil {
        log.Fatalf("error walking the path %q: %v\n", baseDir, err)
    }
}

// uploadDirectoryToS3 uploads the contents of the given directory to an S3 bucket
func uploadDirectoryToS3(s3Client *s3.Client, dirPath string) error {
    err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
        if err != nil {
            return err
        }

        // Skip directories themselves, only upload files
        if !info.IsDir() {
            relativePath, err := filepath.Rel(dirPath, path)
            if err != nil {
                return err
            }

            fmt.Printf("Uploading %s to S3\n", path)

            file, err := os.Open(path)
            if err != nil {
                return err
            }
            defer file.Close()

            // Key will include both the bucketPath and the relative path of the file
            objectKey := filepath.Join(bucketPath, filepath.Base(dirPath), relativePath)

            _, err = s3Client.PutObject(context.TODO(), &s3.PutObjectInput{
                Bucket: aws.String(bucketName),
                Key:    aws.String(objectKey),
                Body:   file,
            })
            if err != nil {
                return err
            }
        }

        return nil
    })

    return err
}
Leave a Comment