[WIP] add file upload specification

This commit is contained in:
2023-08-20 20:36:36 -04:00
parent 0f1f983532
commit 44d0001592

56
main.go
View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"log" "log"
"os" "os"
@@ -9,13 +10,58 @@ import (
"github.com/minio/minio-go/v7/pkg/credentials" "github.com/minio/minio-go/v7/pkg/credentials"
) )
const (
ENDPOINT = "s3.us-west-004.backblazeb2.com"
BUCKETNAME = "ducimon-db-backups"
)
func main() { func main() {
ctx := context.Background()
accessKeyID, secretAccessKey := getCredentials() accessKeyID, secretAccessKey := getCredentials()
endpoint := "s3.us-west-002.backblazeb2.com" 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. // Initialize minio client object.
minioClient, err := minio.New(endpoint, &minio.Options{ minioClient, err := minio.New(ENDPOINT, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: true, Secure: true,
}) })
@@ -23,7 +69,7 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
log.Printf("%#v\n", minioClient) // minioClient is now set up return minioClient
} }
func getCredentials() (string, string) { func getCredentials() (string, string) {
@@ -32,9 +78,9 @@ func getCredentials() (string, string) {
if err != nil { if err != nil {
log.Fatalf("error loading .env file: (%s)", err.Error()) log.Fatalf("error loading .env file: (%s)", err.Error())
} }
keyName := os.Getenv("KEY_NAME") keyName := os.Getenv("KEY_ID")
if keyName == "" { if keyName == "" {
log.Fatal("missing or empty KEY_NAME") log.Fatal("missing or empty KEY_ID")
} }
applicationKey := os.Getenv("APPLICATION_KEY") applicationKey := os.Getenv("APPLICATION_KEY")
if applicationKey == "" { if applicationKey == "" {