From a0081fab996e9c1602143d9d819871d7b717a2ef Mon Sep 17 00:00:00 2001 From: Alex Csengery Date: Tue, 29 Aug 2023 21:00:49 -0400 Subject: [PATCH] refactor upload into 's3helper' package --- main.go | 62 ++------------------------------------ s3helper/s3helper.go | 72 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 59 deletions(-) create mode 100644 s3helper/s3helper.go diff --git a/main.go b/main.go index 4100417..a40ba3f 100644 --- a/main.go +++ b/main.go @@ -1,75 +1,19 @@ package main import ( + "backup-tool/s3helper" "context" - "github.com/joho/godotenv" "log" "os" - "github.com/minio/minio-go/v7" - "github.com/minio/minio-go/v7/pkg/credentials" -) - -const ( - ENDPOINT = "s3.us-west-004.backblazeb2.com" - BUCKETNAME = "ducimon-db-backups" + "github.com/joho/godotenv" ) func main() { - ctx := context.Background() accessKeyID, secretAccessKey := getCredentials() - client := createClient(accessKeyID, secretAccessKey) - verifyBucket(client, ctx) - absolutePath, basename := validateUploadFile() + s3helper.UploadFile(context.Background(), accessKeyID, secretAccessKey) - _, err := client.FPutObject(ctx, BUCKETNAME, "test", absolutePath, minio.PutObjectOptions{}) - if err != nil { - log.Fatalln(err) - } - - log.Printf("upload of %s complete\n", basename) -} - -func validateUploadFile() (string, string) { - if len(os.Args) < 2 { - log.Fatalln("upload file not specified") - } - - file, err := os.Stat(os.Args[1]) - if err != nil { - log.Fatalln(err) - } - - if file.IsDir() { - log.Fatalln("upload of directories is not supported") - } - - return os.Args[1], file.Name() -} - -func verifyBucket(client *minio.Client, ctx context.Context) { - exists, err := client.BucketExists(ctx, BUCKETNAME) - if err != nil { - log.Fatalln(err) - } - - if !exists { - log.Fatalf("bucket %s does not exist\n", BUCKETNAME) - } -} - -func createClient(accessKeyID string, secretAccessKey string) *minio.Client { - // Initialize minio client object. - minioClient, err := minio.New(ENDPOINT, &minio.Options{ - Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), - Secure: true, - }) - if err != nil { - log.Fatalln(err) - } - - return minioClient } func getCredentials() (string, string) { diff --git a/s3helper/s3helper.go b/s3helper/s3helper.go new file mode 100644 index 0000000..25e2770 --- /dev/null +++ b/s3helper/s3helper.go @@ -0,0 +1,72 @@ +package s3helper + +import ( + "context" + "log" + "os" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" +) + +const ( + ENDPOINT = "s3.us-west-004.backblazeb2.com" + BUCKETNAME = "ducimon-db-backups" +) + +func UploadFile(ctx context.Context, accessKeyID, secretAccessKey) error { + + client := createClient(accessKeyID, secretAccessKey) + verifyBucket(client, ctx) + absolutePath, basename := validateUploadFile() + _, err := client.FPutObject(ctx, BUCKETNAME, "test", absolutePath, minio.PutObjectOptions{}) + if err != nil { + log.Fatalln(err) + } + + log.Printf("upload of %s complete\n", basename) + + return nil +} + +func createClient(accessKeyID string, secretAccessKey string) *minio.Client { + // Initialize minio client object. + minioClient, err := minio.New(ENDPOINT, &minio.Options{ + Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), + Secure: true, + }) + if err != nil { + log.Fatalln(err) + } + + return minioClient +} + +func verifyBucket(client *minio.Client, ctx context.Context) { + exists, err := client.BucketExists(ctx, BUCKETNAME) + if err != nil { + log.Fatalln(err) + } + + if !exists { + log.Fatalf("bucket %s does not exist\n", BUCKETNAME) + } +} + +func validateUploadFile() (string, string) { + if len(os.Args) < 2 { + log.Fatalln("upload file not specified") + } + + file, err := os.Stat(os.Args[1]) + + if err != nil { + log.Fatalln(err) + } + + if file.IsDir() { + log.Fatalln("upload of directories is not supported") + } + + return os.Args[1], file.Name() +}