/
storageSync.go
273 lines (233 loc) · 7.32 KB
/
storageSync.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main
import (
"encoding/json"
"io/ioutil"
"log"
// "fmt"
"os"
"strings"
"errors"
"time"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/cloud"
"google.golang.org/cloud/storage"
"google.golang.org/cloud/pubsub"
"github.com/secsy/goftp"
)
// Configuration ...
type Configuration struct {
GsProjectID string `json:"gs_project_id"`
GsBucket string `json:"gs_bucket"`
FtpAddr string `json:"ftp_addr"`
FtpUser string `json:"ftp_user"`
FtpPassword string `json:"ftp_passwd"`
FtpRoot string `json:"ftp_root"`
AllowStores []string `json:"allow_stores"`
CredentialsPath string
}
// SyncInfo ...
type SyncInfo struct {
storeID string
orderID string
gcPrefix string
ftpRootPath string
}
// Gcloud ...
type Gcloud struct {
ctx context.Context
storageClient storage.Client
}
// MaxFtpConnect ...
const MaxFtpConnect = 2
var config = Configuration{}
var gcloud = Gcloud{}
var currentFtpConnect = 0
func init() {
configFilePath := os.Getenv("GSS_CONFIG")
if (len(configFilePath) <= 0) {
configFilePath = "config.json"
}
configFile, err := os.Open(configFilePath)
if (err != nil) {
log.Fatalln("Opening file failed. ", err)
}
decoder := json.NewDecoder(configFile)
err = decoder.Decode(&config)
if err != nil {
log.Fatalln("Loading config failed.", err)
}
credentialsPath := os.Getenv("GSS_CREDENTIALS")
if len(credentialsPath) <= 0 {
log.Fatalln(`No set environment variable "GSS_CREDENTIALS" for credentials path.`)
}
config.CredentialsPath = credentialsPath
gcloudAuth()
// fmt.Println("GsBucket: ", config.GsBucket)
// fmt.Println("FtpAddr: ", config.FtpAddr)
// fmt.Println("FtpUser: ", config.FtpUser)
// fmt.Println("FtpPassword: ", config.FtpPassword)
// fmt.Println("FtpRoot: ", config.FtpRoot)
// fmt.Println("AllowStores: ", config.AllowStores)
// fmt.Println("CredentialsPath: ", config.CredentialsPath)
log.Println("GSS initial success.")
}
func main() {
for {
msgs, err := pubsub.Pull(gcloud.ctx, "syncToFtp", 1)
if err != nil {
log.Println(err)
}
if len(msgs) > 0 {
attr := msgs[0].Attributes
storeID := attr["storeID"]
orderID := attr["orderID"]
if err := pubsub.Ack(gcloud.ctx, "syncToFtp", msgs[0].AckID); err != nil {
log.Println("Ack failed.")
}
info, err := initSyncInfo(storeID, orderID)
if err == nil {
// fmt.Println("storeID: ", info.storeID)
// fmt.Println("orderID: ", info.orderID)
// fmt.Println("gcPrefix: ", info.gcPrefix)
// fmt.Println("ftpRootPath: ", info.ftpRootPath)
objects := queryStorageObjects(info)
uploadObjects(info, objects)
}
}
}
}
func uploadObjects(info *SyncInfo, objects *[]string) {
ftpConfig := goftp.Config {
User: config.FtpUser,
Password: config.FtpPassword,
ConnectionsPerHost: 10,
}
ftpClient, err := goftp.DialConfig(ftpConfig, config.FtpAddr)
if err != nil {
log.Fatalln("ftp connect failed. ", err)
}
//create dir
makeDir(info, ftpClient, objects)
doUpload := make(chan bool)
uploadDone := make(chan bool)
go func() {
for {
_, ok := <-uploadDone
if ok {
currentFtpConnect--
} else {
return
}
}
}()
// create upload job
for _, obj := range *objects {
go doUploadObject(info, obj, ftpClient, doUpload, uploadDone)
}
// send doUpload access
for i := 0; i < len(*objects); i++ {
for currentFtpConnect >= MaxFtpConnect {
time.Sleep(time.Millisecond * 500)
}
doUpload <- true
currentFtpConnect++
}
for currentFtpConnect > 0 {
log.Println("currentFtpConnect: ", currentFtpConnect)
time.Sleep(time.Millisecond * 500)
}
}
func doUploadObject(info *SyncInfo, object string, ftpClient *goftp.Client, doUpload chan bool, uploadDone chan bool) {
for {
_,ok := <- doUpload
if ok {
ftpPath := getFtpPath(info, object)
ftpPathStr := strings.Join(*ftpPath, "/")
rc, err := gcloud.storageClient.Bucket(config.GsBucket).Object(object).NewReader(gcloud.ctx)
if err != nil {
log.Println("Get Object Read io failed.", err)
} else {
defer rc.Close()
err := ftpClient.Store(ftpPathStr, rc)
if err != nil {
log.Println("Object store to FTP failed ", err)
} else {
log.Println(ftpPathStr + " stror to FTP Success.")
}
}
}
uploadDone <- true
return
}
}
func initSyncInfo(storeID , orderID string) (*SyncInfo, error) {
for _, allowStore := range config.AllowStores {
if allowStore == storeID {
gcPrefix := storeID + "/" + "order/" + orderID
ftpRootPath := config.FtpRoot + "/" + orderID + "/"
return &SyncInfo{
storeID: storeID,
orderID: orderID,
gcPrefix: gcPrefix,
ftpRootPath: ftpRootPath,
}, nil
}
}
return nil, errors.New("Not Allow Storage ID.")
}
func makeDir(info *SyncInfo, ftpClinet *goftp.Client, objects *[]string) {
for _, obj := range *objects {
path := getFtpPath(info, obj)
var mkPath string
for i, dir := range *path {
mkPath += dir + "/"
if i <= len(*path) - 2 {
ftpClinet.Mkdir(mkPath)
}
}
}
}
func getFtpPath(info *SyncInfo, stroagePath string) *[]string {
ftpPathStr := strings.Replace(stroagePath, info.gcPrefix + "/", "", -1)
ftpPath := strings.Split(info.ftpRootPath + ftpPathStr, "/")
return &ftpPath
}
func queryStorageObjects(info *SyncInfo) *[]string {
var objects []string
query := storage.Query{Prefix: info.gcPrefix}
objList, err := gcloud.storageClient.Bucket(config.GsBucket).List(gcloud.ctx, &query)
if (err != nil) {
log.Println("List targe folder failed. " ,err)
return &objects
}
for _, obj := range objList.Results {
if !strings.HasSuffix(obj.Name, "/") {
objects = append(objects, obj.Name)
}
}
return &objects
}
func gcloudAuth() {
jsonKey, err := ioutil.ReadFile(config.CredentialsPath)
if err != nil {
log.Fatal(err)
}
conf, err := google.JWTConfigFromJSON(
jsonKey,
pubsub.ScopeCloudPlatform,
pubsub.ScopePubSub,
storage.ScopeFullControl,
)
if err != nil {
log.Fatal(err)
}
ctx := cloud.NewContext(config.GsProjectID, conf.Client(oauth2.NoContext))
client, err := storage.NewClient(ctx, cloud.WithTokenSource(conf.TokenSource(ctx)))
if err != nil {
log.Fatal("New Storage client.", err)
}
gcloud.ctx = ctx
gcloud.storageClient = *client
}