-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathobjects.go
More file actions
150 lines (123 loc) · 3.7 KB
/
objects.go
File metadata and controls
150 lines (123 loc) · 3.7 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"os"
"sync"
minio "github.com/minio/minio-go"
)
func md5Hasher(dataChunk []byte) string {
//Initialize variable returnMD5String now in case an error has to be returned
var md5String string
//Open a new hash interface to write to
hash := md5.New()
//Get the 16 bytes hash
hashInBytes := hash.Sum(dataChunk)[:16]
//Convert the bytes to a string
md5String = hex.EncodeToString(hashInBytes)
return md5String
}
func eTagger(path string, partSizeMb int) string {
partSize := partSizeMb * 1024 * 1024
content, _ := ioutil.ReadFile(path)
fileSize := len(content)
contentToHash := content
parts := 0
if fileSize > partSize {
pos := 0
contentToHash = make([]byte, 0)
for fileSize > pos {
endpos := pos + partSize
if endpos >= fileSize {
endpos = fileSize
}
hash := md5.Sum(content[pos:endpos])
contentToHash = append(contentToHash, hash[:]...)
pos += partSize
parts++
}
}
hash := md5.Sum(contentToHash)
etag := fmt.Sprintf("%x", hash)
if parts > 0 {
etag += fmt.Sprintf("-%d", parts)
}
return etag
}
// checksumMatch - returns true if they match, false if they do not match
func checksumMatch(remoteChecksum, localFilePath string) bool {
// check if local file exists. If not return false right away
_, err := os.Stat(localFilePath)
if err != nil {
log.Println("Could not find file", localFilePath, "Skipping checksum and downloading")
return false
}
// doing a normal md5checksum and my multi-part function checks.
// There are instances where a regular md5 is used and where the multi-part
// hash is used where you wouldn't always expect them. Checking both scenarios.
//get md5sum of local file
fileContent, _ := ioutil.ReadFile(localFilePath)
localMd5Checksum := md5Hasher(fileContent)
if err != nil {
log.Println("Error getting local MD5 checksum", err)
return false
}
// compare local md5 with remote md5
if string(localMd5Checksum) == remoteChecksum {
log.Println("Local Sum:", localMd5Checksum, "Remote Sum:", remoteChecksum)
return true
}
// get multi-part ETag checksum of local file
localEtagChecksum := eTagger(localFilePath, 64)
if err != nil {
log.Println("Error getting local Etag checksum", err)
return false
}
// compare local multi-part etag with remote multi-part etag
if string(localEtagChecksum) == remoteChecksum {
log.Println("Local Sum:", localEtagChecksum, "Remote Sum:", remoteChecksum)
return true
}
// return false by default
return false
}
func getFiles(client *minio.Client, filesToDownload []string, bucketName, dest string) {
concurrency := 2
workerPool := make(chan bool, concurrency)
for i := 0; i < concurrency; i++ {
workerPool <- true
}
var wg sync.WaitGroup
wg.Add(len(filesToDownload))
for _, fileName := range filesToDownload {
<-workerPool
go func(wg *sync.WaitGroup, fileName string) {
stat, err := client.StatObject(bucketName, fileName, minio.StatObjectOptions{})
if err != nil {
log.Fatalln(err)
}
// if the checksums match we don't need to download because they are
// the same thing
if checksumMatch(stat.ETag, dest+"/"+fileName) {
log.Printf("%s/%s Has Not Changed. Not downloading.", dest, fileName)
workerPool <- true
wg.Done()
return
}
log.Println("downloading", dest+"/"+fileName)
// Spin this out in to go routines so we can download concurrently.
// might want to buffer this with channels though so we don't saturate things
err = client.FGetObject(bucketName, fileName, dest+"/"+fileName, minio.GetObjectOptions{})
if err != nil {
log.Println("Error downloading file ", err)
return
}
workerPool <- true
wg.Done()
}(&wg, fileName)
}
wg.Wait()
}