-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
107 lines (95 loc) · 2.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/joho/godotenv"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"golang.org/x/exp/slices"
)
func main() {
ctx := context.Background()
// Load environment variables
err := godotenv.Load()
if err != nil {
log.Fatalln(err)
}
minioUrl := os.Getenv("MINIO_ENDPOINT")
backupsDir := os.Getenv("BACKUP_FILES_DIR")
minioKey := os.Getenv("MINIO_ACCESSKEY")
minioSecret := os.Getenv("MINIO_SECRET")
minioSSL := false
fmt.Println("URL: ", minioUrl)
// Set up minio
endpoint := minioUrl
accessKeyID := minioKey
secretAccessKey := minioSecret
useSSL := minioSSL
// Initialize minio client object.
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
log.Fatalln(err)
}
//log.Printf("%#v\n", minioClient)
// list buckets
buckets, err := minioClient.ListBuckets(context.Background())
if err != nil {
log.Fatalln(err)
}
// get files
dirFs, err := ioutil.ReadDir(backupsDir)
if err != nil {
log.Fatalln(err)
}
fmt.Println("\n\n")
// loop through files
for i, f := range dirFs {
// for each file, find a bucket with a corresponding name
fileName := strings.ToLower(strings.Split(f.Name(), ".")[0])
fileBucketIndex := slices.IndexFunc(buckets, func(b minio.BucketInfo) bool {
return fileName == strings.ToLower(strings.Split(b.Name, "-backups")[0])
})
if fileBucketIndex != -1 {
fileBucket := buckets[fileBucketIndex]
fmt.Println(fmt.Sprint(i+1) + ") File: " + fmt.Sprint(f.Name()) + ", Bucket: " + fileBucket.Name)
fopen, err := os.Open(backupsDir + "/" + f.Name())
fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
if err != nil {
log.Fatalln("Could not open file "+f.Name()+". ", err)
}
fileType, err := getFileContentType(fopen)
if err != nil {
log.Fatalln("Could not read file type.", err)
}
info, err := minioClient.FPutObject(ctx,
fileBucket.Name,
f.Name()+" - "+time.Now().Local().String(),
backupsDir+"/"+f.Name(),
minio.PutObjectOptions{ContentType: fileType})
if err != nil {
log.Fatalln(err)
}
log.Printf("Successfully uploaded %s of size %d\n", f.Name(), info.Size)
} else {
fmt.Println("File: " + fmt.Sprint(f.Name()) + ", Bucket not found")
}
}
}
func getFileContentType(ouput *os.File) (string, error) {
buf := make([]byte, 512)
_, err := ouput.Read(buf)
if err != nil {
return "", err
}
contentType := http.DetectContentType(buf)
return contentType, nil
}