-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure.go
179 lines (146 loc) · 4.61 KB
/
azure.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
package storage
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/sas"
"github.com/itgcloud/gobackup/helper"
"github.com/itgcloud/gobackup/logger"
)
// Azure - Microsoft Azure Blob Storage
//
// type: azure
// # Storage Account
// account: gobackup-test
// # Container name
// container: gobackup
// path: backups
// # Authorization https://learn.microsoft.com/en-us/azure/developer/go/azure-sdk-authentication
// tenant_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
// client_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
// client_secret: xxxxxxxxxxxxxxxxx
// timeout: 300
type Azure struct {
Base
account string
container string
path string
timeout time.Duration
client *azblob.Client
}
func (s *Azure) open() error {
s.viper.SetDefault("timeout", "300")
s.viper.SetDefault("container", "gobackup")
s.viper.SetDefault("path", "/")
timeout := s.viper.GetInt("timeout")
s.timeout = time.Duration(timeout) * time.Second
s.container = s.viper.GetString("container")
s.account = s.viper.GetString("account")
if len(s.account) == 0 {
s.account = s.viper.GetString("bucket")
}
s.path = s.viper.GetString("path")
tenantId := s.viper.GetString("tenant_id")
clientId := s.viper.GetString("client_id")
clientSecret := s.viper.GetString("client_secret")
credential, err := azidentity.NewClientSecretCredential(tenantId, clientId, clientSecret, nil)
if err != nil {
return fmt.Errorf("Invalid credentials with error: %w", err)
}
s.client, err = azblob.NewClient(s.getBucketURL(), credential, nil)
if err != nil {
return err
}
return nil
}
func (s *Azure) close() {
}
func (s Azure) getBucketURL() string {
return fmt.Sprintf("https://%s.blob.core.windows.net", s.account)
}
func (s *Azure) upload(fileKey string) (err error) {
logger := logger.Tag("Azure")
var ctx = context.Background()
var cancel context.CancelFunc
if s.timeout.Seconds() > 0 {
logger.Info(fmt.Sprintf("timeout: %s", s.timeout))
ctx, cancel = context.WithTimeout(ctx, s.timeout)
defer cancel()
}
// Check to create Azure Storage Container, And ignore error
_, err = s.client.CreateContainer(ctx, s.container, nil)
var fileKeys []string
if len(s.fileKeys) != 0 {
// directory
// 2022.12.04.07.09.47/2022.12.04.07.09.47.tar.xz-000
fileKeys = s.fileKeys
} else {
// file
// 2022.12.04.07.09.25.tar.xz
fileKeys = append(fileKeys, fileKey)
}
for _, key := range fileKeys {
sourcePath := filepath.Join(filepath.Dir(s.archivePath), key)
remotePath := filepath.Join(s.path, key)
// Open file
f, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("Azure failed to open file %q, %v", sourcePath, err)
}
defer f.Close()
progress := helper.NewProgressBar(logger, f)
if _, err = s.client.UploadStream(ctx, s.container, remotePath, progress.Reader, nil); err != nil {
return progress.Errorf("Azure upload error: %v", err)
}
progress.Done(remotePath)
}
return nil
}
func (s *Azure) delete(fileKey string) (err error) {
remotePath := filepath.Join(s.path, fileKey)
var ctx = context.Background()
if _, err = s.client.DeleteBlob(ctx, s.container, remotePath, nil); err != nil {
return fmt.Errorf("Azure failed to delete file %q, %v", remotePath, err)
}
return nil
}
// List the objects in the bucket with the prefix = parent
// https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob
func (s *Azure) list(parent string) ([]FileItem, error) {
remotePath := filepath.Join(s.archivePath, parent)
var ctx = context.Background()
var fileItems []FileItem
// Get a result segment starting with the blob indicated by the current Marker.
pager := s.client.NewListBlobsFlatPager(s.container, &azblob.ListBlobsFlatOptions{
Prefix: &remotePath,
})
for pager.More() {
resp, err := pager.NextPage(ctx)
if err != nil {
return nil, err
}
for _, blob := range resp.Segment.BlobItems {
fileItems = append(fileItems, FileItem{
Filename: *blob.Name,
LastModified: *blob.Properties.LastModified,
Size: *blob.Properties.ContentLength,
})
}
}
return fileItems, nil
}
// Get a client download URL
func (s *Azure) download(fileKey string) (string, error) {
containerClient := s.client.ServiceClient().NewContainerClient(s.container)
blobClient := containerClient.NewBlobClient(fileKey)
t := time.Now().Add(time.Hour * 1)
opts := &blob.GetSASURLOptions{
StartTime: &t,
}
return blobClient.GetSASURL(sas.BlobPermissions{Read: true}, time.Now(), opts)
}