From d316b8ff4bae15a030383e9e2a6bf2048491d744 Mon Sep 17 00:00:00 2001 From: Alex Csengery Date: Thu, 21 Sep 2023 22:01:03 -0400 Subject: [PATCH] refactor into uploader module This will probably just morph further into a "bucket" module for uploading, listing, deleting, etc. --- main.go | 85 +++++++--------------------------------- uploader/uploader.go | 92 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 71 deletions(-) create mode 100644 uploader/uploader.go diff --git a/main.go b/main.go index 4100417..2d86934 100644 --- a/main.go +++ b/main.go @@ -1,90 +1,33 @@ package main import ( - "context" + "backup-tool/uploader" "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" + ENDPOINT = "s3.us-west-004.backblazeb2.com" ) func main() { - - ctx := context.Background() - accessKeyID, secretAccessKey := getCredentials() - 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) -} - -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) { - // get credentials from env vars err := godotenv.Load() if err != nil { log.Fatalf("error loading .env file: (%s)", err.Error()) } - keyName := os.Getenv("KEY_ID") - if keyName == "" { - log.Fatal("missing or empty KEY_ID") + + if len(os.Args) < 2 { + log.Fatalln("missing filename parameter") } - applicationKey := os.Getenv("APPLICATION_KEY") - if applicationKey == "" { - log.Fatal("missing or empty APPLICATION_KEY") + + bucketName := os.Getenv("BUCKET_NAME") + if bucketName == "" { + log.Fatalln("missing or empty BUCKET_NAME") + } + + err = uploader.UploadFile(bucketName, ENDPOINT, os.Args[1]) + if err != nil { + log.Fatalln(err.Error()) } - return keyName, applicationKey } diff --git a/uploader/uploader.go b/uploader/uploader.go new file mode 100644 index 0000000..1329b1e --- /dev/null +++ b/uploader/uploader.go @@ -0,0 +1,92 @@ +package uploader + +import ( + "context" + "errors" + "fmt" + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" + "log" + "os" +) + +func UploadFile(bucketName, endpoint, fileName string) error { + + ctx := context.Background() + accessKeyID, secretAccessKey, err := getCredentials() + if err != nil { + return (err) + } + client, err := createClient(accessKeyID, secretAccessKey, endpoint) + if err != nil { + return (err) + } + err = verifyBucket(client, ctx, bucketName) + if err != nil { + return err + } + absolutePath, basename, err := validateUploadFile(fileName) + if err != nil { + return err + } + + _, err = client.FPutObject(ctx, bucketName, basename, absolutePath, minio.PutObjectOptions{}) + if err != nil { + return err + } + + log.Printf("upload of %s complete\n", basename) + return nil +} + +func validateUploadFile(fileName string) (string, string, error) { + file, err := os.Stat(fileName) + if err != nil { + return "", "", err + } + + if file.IsDir() { + return "", "", errors.New("upload of directories is not supported") + } + + return fileName, file.Name(), nil +} + +func verifyBucket(client *minio.Client, ctx context.Context, bucketName string) error { + exists, err := client.BucketExists(ctx, bucketName) + if err != nil { + return err + } + + if !exists { + return errors.New(fmt.Sprintf("bucket %s does not exist\n", bucketName)) + } + + return nil +} + +func createClient(accessKeyID, secretAccessKey, endpoint string) (*minio.Client, error) { + // Initialize minio client object. + minioClient, err := minio.New(endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), + Secure: true, + }) + if err != nil { + return nil, err + } + + return minioClient, nil +} + +func getCredentials() (string, string, error) { + // get credentials from env vars + keyName := os.Getenv("KEY_ID") + if keyName == "" { + return "", "", errors.New("missing or empty KEY_ID") + } + applicationKey := os.Getenv("APPLICATION_KEY") + if applicationKey == "" { + return "", "", errors.New("missing or empty APPLICATION_KEY") + } + return keyName, applicationKey, nil +}