-
Notifications
You must be signed in to change notification settings - Fork 58
/
upload.go
988 lines (791 loc) · 23.2 KB
/
upload.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
// pg_back
//
// Copyright 2011-2021 Nicolas Thauvin and contributors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package main
import (
"context"
"errors"
"fmt"
"io"
"net"
"os"
"os/user"
"path/filepath"
"strings"
"time"
"cloud.google.com/go/storage"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/Backblaze/blazer/b2"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/crypto/ssh/knownhosts"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
// A Repo is a remote service where we can upload files
type Repo interface {
// Upload a path to the remote naming it target
Upload(path string, target string) error
// Download target from the remote and store it into path
Download(target string, path string) error
// List remote files starting with a prefix. the prefix can be empty to
// list all files
List(prefix string) ([]Item, error)
// Remove path from the remote
Remove(path string) error
// Close cleans up any open resource
Close() error
}
type Item struct {
key string
modtime time.Time
isDir bool
}
// Replace any backslashes from windows to forward slashed
func forwardSlashes(target string) string {
return strings.ReplaceAll(target, fmt.Sprintf("%c", os.PathSeparator), "/")
}
func NewRepo(kind string, opts options) (Repo, error) {
var (
repo Repo
err error
)
switch kind {
case "s3":
repo, err = NewS3Repo(opts)
if err != nil {
return nil, fmt.Errorf("failed to prepare S3 repo: %w", err)
}
case "b2":
repo, err = NewB2Repo(opts)
if err != nil {
return nil, fmt.Errorf("failed to prepare B2 repo: %w", err)
}
case "sftp":
repo, err = NewSFTPRepo(opts)
if err != nil {
return nil, fmt.Errorf("failed to prepare sftp repo: %w", err)
}
case "gcs":
repo, err = NewGCSRepo(opts)
if err != nil {
return nil, fmt.Errorf("failed to prepare CGS repo: %w", err)
}
case "azure":
repo, err = NewAzRepo(opts)
if err != nil {
return nil, fmt.Errorf("failed to prepare Azure repo: %w", err)
}
}
return repo, nil
}
type b2repo struct {
appKey string
b2Bucket *b2.Bucket
b2Client *b2.Client
bucket string
concurrentConnections int
ctx context.Context
forcePath bool
keyID string
}
type s3repo struct {
region string
bucket string
profile string
keyID string
secret string
endPoint string
forcePath bool
disableSSL bool
session *session.Session
}
func NewB2Repo(opts options) (*b2repo, error) {
r := &b2repo{
appKey: opts.B2AppKey,
bucket: opts.B2Bucket,
concurrentConnections: opts.B2ConcurrentConnections,
ctx: context.Background(),
forcePath: opts.B2ForcePath,
keyID: opts.B2KeyID,
}
l.Verbosef("starting b2 client with %d connections to endpoint to bucket %s \n", r.concurrentConnections, r.bucket)
client, err := b2.NewClient(r.ctx, r.keyID, r.appKey)
if err != nil {
return nil, fmt.Errorf("could not create B2 session: %w", err)
}
r.b2Client = client
bucket, err := r.b2Client.Bucket(r.ctx, r.bucket)
if err != nil {
return nil, fmt.Errorf("could not connect to B2 bucket: %w", err)
}
r.b2Bucket = bucket
return r, nil
}
func (r *b2repo) Upload(path string, target string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
w := r.b2Bucket.Object(target).NewWriter(r.ctx)
defer w.Close()
w.ConcurrentUploads = r.concurrentConnections
l.Infof("uploading %s to B2 bucket %s\n", path, r.bucket)
if _, err := io.Copy(w, f); err != nil {
return err
}
return nil
}
func (r *b2repo) Download(target string, path string) error {
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("download error: %w", err)
}
defer f.Close()
bucket := r.b2Bucket
l.Infof("downloading %s from B2 bucket %s to %s\n", target, r.bucket, path)
rf := bucket.Object(target).NewReader(r.ctx)
rf.ConcurrentDownloads = r.concurrentConnections
defer rf.Close()
if err != nil {
return err
}
if _, err := io.Copy(f, rf); err != nil {
return err
}
return nil
}
func (r *b2repo) Close() error {
return nil
}
func (r *b2repo) List(prefix string) ([]Item, error) {
files := make([]Item, 0)
i := r.b2Bucket.List(r.ctx, b2.ListPrefix(prefix))
for i.Next() {
obj := i.Object()
attributes, err := obj.Attrs(r.ctx)
if err != nil {
return nil, err
}
files = append(files, Item{
key: obj.Name(),
modtime: attributes.LastModified,
},
)
}
return files, i.Err()
}
func (r *b2repo) Remove(path string) error {
ctx, cancel := context.WithCancel(r.ctx)
defer cancel()
return r.b2Bucket.Object(path).Delete(ctx)
}
func NewS3Repo(opts options) (*s3repo, error) {
r := &s3repo{
region: opts.S3Region,
bucket: opts.S3Bucket,
profile: opts.S3Profile,
keyID: opts.S3KeyID,
secret: opts.S3Secret,
endPoint: opts.S3EndPoint,
forcePath: opts.S3ForcePath,
disableSSL: opts.S3DisableTLS,
}
conf := aws.NewConfig()
if r.region != "" {
conf = conf.WithRegion(r.region)
}
if r.keyID != "" {
conf = conf.WithCredentials(credentials.NewStaticCredentials(r.keyID, r.secret, ""))
}
if r.endPoint != "" {
conf = conf.WithEndpoint(r.endPoint)
}
if r.forcePath {
conf = conf.WithS3ForcePathStyle(true)
}
if r.disableSSL {
conf = conf.WithDisableSSL(true)
}
sopts := session.Options{
Config: *conf,
SharedConfigState: session.SharedConfigEnable,
}
if r.profile != "" {
sopts.Profile = r.profile
}
session, err := session.NewSessionWithOptions(sopts)
if err != nil {
return nil, fmt.Errorf("could not create AWS session: %w", err)
}
r.session = session
return r, nil
}
func (r *s3repo) Close() error {
return nil
}
func (r *s3repo) Upload(path string, target string) error {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("upload error: %w", err)
}
defer file.Close()
uploader := s3manager.NewUploader(r.session)
l.Infof("uploading %s to S3 bucket %s\n", path, r.bucket)
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(r.bucket),
Key: aws.String(forwardSlashes(target)),
Body: file,
})
if err != nil {
return fmt.Errorf("unable to upload %q to %q: %w", path, r.bucket, err)
}
return nil
}
func (r *s3repo) Download(target string, path string) error {
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("download error: %w", err)
}
defer file.Close()
downloader := s3manager.NewDownloader(r.session)
l.Infof("downloading %s from S3 bucket %s to %s\n", target, r.bucket, path)
_, err = downloader.Download(file, &s3.GetObjectInput{
Bucket: aws.String(r.bucket),
Key: aws.String(forwardSlashes(target)),
})
if err != nil {
return fmt.Errorf("unable to download %q from %q: %w", target, r.bucket, err)
}
return nil
}
func (r *s3repo) List(prefix string) ([]Item, error) {
svc := s3.New(r.session)
files := make([]Item, 0)
var contToken *string
for {
resp, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(r.bucket),
Prefix: aws.String(forwardSlashes(prefix)),
ContinuationToken: contToken,
})
if err != nil {
return files, fmt.Errorf("could not list items in S3 bucket %s: %w", r.bucket, err)
}
for _, item := range resp.Contents {
file := Item{
key: *item.Key,
modtime: *item.LastModified,
}
files = append(files, file)
}
if !*resp.IsTruncated {
break
}
contToken = resp.NextContinuationToken
}
return files, nil
}
func (r *s3repo) Remove(path string) error {
svc := s3.New(r.session)
_, err := svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(r.bucket),
Key: aws.String(forwardSlashes(path)),
})
if err != nil {
return fmt.Errorf("could not remove %s from S3 bucket %s: %w", path, r.bucket, err)
}
return nil
}
type sftpRepo struct {
host string
port string
user string
password string
identityFile string
baseDir string
disableHostCheck bool
conn *ssh.Client
client *sftp.Client
}
func expandHomeDir(path string) (string, error) {
expanded := filepath.Clean(path)
if strings.HasPrefix(path, "~") {
var (
homeDir string
err error
)
parts := strings.SplitN(path, "/", 2)
username := parts[0][1:]
if username == "" {
homeDir, err = os.UserHomeDir()
if err != nil || homeDir == "" {
u, err := user.Current()
if err != nil {
return expanded, fmt.Errorf("could not expand ~: %w", err)
}
homeDir = u.HomeDir
if homeDir == "" {
return expanded, fmt.Errorf("could not expand ~: empty home directory")
}
}
} else {
u, err := user.Lookup(username)
if err != nil {
return expanded, fmt.Errorf("could not expand ~%s: %w", username, err)
}
homeDir = u.HomeDir
if homeDir == "" {
return expanded, fmt.Errorf("could not expand ~%s: empty home directory", username)
}
}
expanded = filepath.Clean(filepath.Join(homeDir, parts[1]))
}
return expanded, nil
}
func hostKeyCheck(ignore bool) ssh.HostKeyCallback {
if ignore {
return ssh.InsecureIgnoreHostKey()
}
knownHostsFiles := make([]string, 0)
for _, p := range []string{"/etc/ssh/ssh_known_hosts", "~/.ssh/known_hosts"} {
path, err := expandHomeDir(p)
if err != nil {
continue
}
// Check if the file is there to mitigate a complete failure
// when loading all files in knownhosts.New()
_, err = os.Stat(path)
if err != nil {
continue
}
knownHostsFiles = append(knownHostsFiles, path)
}
if len(knownHostsFiles) == 0 {
// No host keys can be loaded for checking, return a callback
// that fails all the time
return func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return fmt.Errorf("ssh: no local keys to check host key")
}
}
knownHostsKeyCb, err := knownhosts.New(knownHostsFiles...)
if err != nil {
return func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return fmt.Errorf("ssh: unable to load local keys to check host key")
}
}
return knownHostsKeyCb
}
func pubKeyAuth(identity string, passphrase string) ([]ssh.Signer, error) {
signers := make([]ssh.Signer, 0)
if identity != "" {
path, err := expandHomeDir(identity)
if err != nil {
return nil, fmt.Errorf("ssh: unable to load private key: %w", err)
}
key, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("ssh: could not read %s: %w", path, err)
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
var passError *ssh.PassphraseMissingError
if errors.As(err, &passError) {
signer, err = ssh.ParsePrivateKeyWithPassphrase(key, []byte(passphrase))
if err != nil {
return nil, fmt.Errorf("ssh: could not decrypt %s: %w", path, err)
}
} else {
return nil, fmt.Errorf("ssh: could not parse %s: %w", path, err)
}
}
signers = append(signers, signer)
}
// ssh-agent(1) provides a UNIX socket at $SSH_AUTH_SOCK. We try to get
// its keys but do not fail if it is not available
socket := os.Getenv("SSH_AUTH_SOCK")
conn, err := net.Dial("unix", socket)
if err != nil {
return signers, nil
}
agentClient := agent.NewClient(conn)
agentSigners, err := agentClient.Signers()
if err != nil {
return signers, nil
}
signers = append(signers, agentSigners...)
return signers, nil
}
func NewSFTPRepo(opts options) (*sftpRepo, error) {
r := &sftpRepo{
host: opts.SFTPHost,
port: opts.SFTPPort,
user: opts.SFTPUsername,
password: opts.SFTPPassword,
baseDir: opts.SFTPDirectory,
identityFile: opts.SFTPIdentityFile,
disableHostCheck: opts.SFTPIgnoreKnownHosts,
}
if r.port == "" {
r.port = "22"
}
if r.user == "" {
u, err := user.Current()
if err != nil {
return nil, fmt.Errorf("could not retrieve current username: %w", err)
}
r.user = u.Username
}
if r.password == "" {
r.password = os.Getenv("PGBK_SSH_PASS")
}
// Prepare authentication methods for SSH. The password is used for
// regular password authentication when a private key is not explicitly
// provided. Otherwise use the password as a passphrase to decrypt the
// private key. In all cases, we try to get private keys from a running
// ssh agent
methods := make([]ssh.AuthMethod, 0)
if r.identityFile == "" && r.password != "" {
methods = append(methods, ssh.Password(r.password))
}
signers, err := pubKeyAuth(r.identityFile, r.password)
if err != nil {
return nil, err
}
methods = append(methods, ssh.PublicKeys(signers...))
config := &ssh.ClientConfig{
User: r.user,
Auth: methods,
HostKeyCallback: hostKeyCheck(r.disableHostCheck),
}
// Connect to the remote server and perform the SSH handshake.
hostport := fmt.Sprintf("%s:%s", r.host, r.port)
conn, err := ssh.Dial("tcp", hostport, config)
if err != nil {
return nil, fmt.Errorf("unable to connect to %s: %w", hostport, err)
}
r.conn = conn
// Open a sftp client over the SSH connection, it is safe to use it
// concurrently, so we keep it in the repo struct
client, err := sftp.NewClient(r.conn)
if err != nil {
return nil, fmt.Errorf("could not open sftp session: %w", err)
}
r.client = client
return r, nil
}
func (r *sftpRepo) Close() error {
r.client.Close()
return r.conn.Close()
}
func (r *sftpRepo) Upload(path string, target string) error {
l.Infof("uploading %s to %s:%s using sftp\n", path, r.host, r.baseDir)
src, err := os.Open(path)
if err != nil {
return fmt.Errorf("sftp: could not open source %s: %w", path, err)
}
defer src.Close()
rpath := filepath.Join(r.baseDir, target)
targetDir := filepath.Dir(rpath)
// sftp requires slash as path separator
if os.PathSeparator != '/' {
rpath = strings.ReplaceAll(rpath, string(os.PathSeparator), "/")
targetDir = strings.ReplaceAll(targetDir, string(os.PathSeparator), "/")
}
l.Verboseln("sftp remote path is:", rpath)
// Target directory must be created first
if targetDir != "." && targetDir != "/" {
if err := r.client.MkdirAll(targetDir); err != nil {
return fmt.Errorf("sftp: could not create parent directory of %s: %w", rpath, err)
}
}
dst, err := r.client.Create(rpath)
if err != nil {
return fmt.Errorf("sftp: could not open destination %s: %w", rpath, err)
}
defer dst.Close()
if _, err := io.Copy(dst, src); err != nil {
return fmt.Errorf("sftp: could not send data with sftp: %s", err)
}
return nil
}
func (r *sftpRepo) Download(target string, path string) error {
l.Infof("downloading %s from %s:%s using sftp\n", target, r.host, r.baseDir)
dst, err := os.Create(path)
if err != nil {
return fmt.Errorf("sftp: could not open or create %s: %w", path, err)
}
defer dst.Close()
rpath := filepath.Join(r.baseDir, target)
// sftp requires slash as path separator
if os.PathSeparator != '/' {
rpath = strings.ReplaceAll(rpath, string(os.PathSeparator), "/")
}
l.Verboseln("sftp remote path is:", rpath)
src, err := r.client.Open(rpath)
if err != nil {
return fmt.Errorf("sftp: could not open %s on %s: %w", rpath, r.host, err)
}
defer src.Close()
if _, err := io.Copy(dst, src); err != nil {
return fmt.Errorf("sftp: could not receive data with sftp: %s", err)
}
return nil
}
func (r *sftpRepo) List(prefix string) (items []Item, rerr error) {
items = make([]Item, 0)
// sftp requires slash as path separator
baseDir := r.baseDir
if os.PathSeparator != '/' {
baseDir = strings.ReplaceAll(baseDir, string(os.PathSeparator), "/")
}
w := r.client.Walk(baseDir)
for w.Step() {
if err := w.Err(); err != nil {
l.Warnln("could not list remote file:", err)
rerr = err
continue
}
// relPath() makes use of functions of the filepath std module
// that take care of putting back the proper os.PathSeparator
// if it find some slashes, so we can compare paths without
// worrying about path separators
path := relPath(baseDir, w.Path())
if !strings.HasPrefix(path, prefix) {
continue
}
finfo := w.Stat()
items = append(items, Item{
key: path,
modtime: finfo.ModTime(),
isDir: finfo.IsDir(),
})
}
return
}
func (r *sftpRepo) Remove(path string) error {
rpath := filepath.Join(r.baseDir, path)
// sftp requires slash as path separator
if os.PathSeparator != '/' {
rpath = strings.ReplaceAll(rpath, string(os.PathSeparator), "/")
}
if err := r.client.Remove(rpath); err != nil {
return err
}
return nil
}
type gcsRepo struct {
bucket string
url string // Endpoint URL
keyFile string
client *storage.Client
}
func NewGCSRepo(opts options) (*gcsRepo, error) {
r := &gcsRepo{
bucket: opts.GCSBucket,
url: opts.GCSEndPoint,
keyFile: opts.GCSCredentialsFile,
}
options := make([]option.ClientOption, 0)
if r.url != "" {
options = append(options, option.WithEndpoint(r.url))
}
if r.keyFile != "" {
options = append(options, option.WithCredentialsFile(r.keyFile))
}
client, err := storage.NewClient(context.Background(), options...)
if err != nil {
return nil, fmt.Errorf("could not create GCS client: %w", err)
}
r.client = client
return r, nil
}
func (r *gcsRepo) Close() error {
return r.client.Close()
}
func (r *gcsRepo) Upload(path string, target string) error {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("upload error: %w", err)
}
defer file.Close()
obj := r.client.Bucket(r.bucket).Object(forwardSlashes(target)).NewWriter(context.Background())
defer obj.Close()
l.Infof("uploading %s to GCS bucket %s\n", path, r.bucket)
if _, err := io.Copy(obj, file); err != nil {
return fmt.Errorf("could not write data to GCS object: %w", err)
}
// The upload is done asynchronously, the error returned by Close()
// says if it was successful
return obj.Close()
}
func (r *gcsRepo) Download(target string, path string) error {
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("download error: %w", err)
}
defer file.Close()
obj, err := r.client.Bucket(r.bucket).Object(forwardSlashes(target)).NewReader(context.Background())
if err != nil {
return fmt.Errorf("download error: %w", err)
}
defer obj.Close()
l.Infof("downloading %s from GCS bucket %s to %s\n", target, r.bucket, path)
if _, err := io.Copy(file, obj); err != nil {
return fmt.Errorf("could not read data from GCS object: %w", err)
}
return obj.Close()
}
func (r *gcsRepo) List(prefix string) (items []Item, rerr error) {
items = make([]Item, 0)
it := r.client.Bucket(r.bucket).Objects(context.Background(), &storage.Query{Prefix: forwardSlashes(prefix)})
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
l.Warnln("could not list remote file:", err)
rerr = err
break
}
items = append(items, Item{
key: attrs.Name,
modtime: attrs.Updated,
})
}
return
}
func (r *gcsRepo) Remove(path string) error {
if err := r.client.Bucket(r.bucket).Object(forwardSlashes(path)).Delete(context.Background()); err != nil {
return fmt.Errorf("could not remove %s from GCS bucket %s: %w", path, r.bucket, err)
}
return nil
}
type azRepo struct {
container string
account string
key string
endpoint string
client *azblob.Client
}
func NewAzRepo(opts options) (*azRepo, error) {
r := &azRepo{
container: opts.AzureContainer,
account: opts.AzureAccount,
key: opts.AzureKey,
endpoint: opts.AzureEndpoint,
}
var (
client *azblob.Client
err error
)
if r.account == "" {
r.account = os.Getenv("AZURE_STORAGE_ACCOUNT")
}
if r.key == "" {
r.key = os.Getenv("AZURE_STORAGE_KEY")
}
if r.account == "" {
client, err = azblob.NewClientWithNoCredential(r.endpoint, nil)
if err != nil {
return nil, fmt.Errorf("could not create anonymous Azure client: %w", err)
}
} else {
credential, err := azblob.NewSharedKeyCredential(r.account, r.key)
if err != nil {
return nil, fmt.Errorf("could not setup Azure credentials: %w", err)
}
url := fmt.Sprintf("https://%s.%s", r.account, r.endpoint)
client, err = azblob.NewClientWithSharedKeyCredential(url, credential, nil)
if err != nil {
return nil, fmt.Errorf("could not create Azure client: %w", err)
}
}
r.client = client
return r, nil
}
func (r *azRepo) Upload(path string, target string) error {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("upload error: %w", err)
}
defer file.Close()
l.Infof("uploading %s to Azure container %s\n", path, r.container)
_, err = r.client.UploadFile(context.Background(), r.container, path, file, nil)
if err != nil {
return fmt.Errorf("could not upload %s to Azure: %w", path, err)
}
return nil
}
func (r *azRepo) Download(target string, path string) error {
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("download error: %w", err)
}
defer file.Close()
l.Infof("downloading %s from Azure container %s\n", target, r.container)
_, err = r.client.DownloadFile(context.Background(), r.container, target, file, nil)
if err != nil {
return fmt.Errorf("could not download %s from Azure: %w", target, err)
}
return nil
}
func (r *azRepo) List(prefix string) ([]Item, error) {
p := forwardSlashes(prefix)
pager := r.client.NewListBlobsFlatPager(r.container, &azblob.ListBlobsFlatOptions{
Prefix: &p,
})
files := make([]Item, 0)
for pager.More() {
resp, err := pager.NextPage(context.Background())
if err != nil {
return nil, fmt.Errorf("could not fully list Azure container %s: %w", r.container, err)
}
for _, v := range resp.Segment.BlobItems {
file := Item{
key: *v.Name,
modtime: *v.Properties.LastModified,
}
files = append(files, file)
}
}
return files, nil
}
func (r *azRepo) Remove(path string) error {
if _, err := r.client.DeleteBlob(context.Background(), r.container, forwardSlashes(path), nil); err != nil {
return fmt.Errorf("could not remove blob from Azure container %s: %w", r.container, err)
}
return nil
}
func (r *azRepo) Close() error {
return nil
}