-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure.go
74 lines (67 loc) · 2.13 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
package utils
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/ku9nov/backup/configs"
"github.com/sirupsen/logrus"
)
type AzureStorageClient struct {
Client *azblob.Client
BlobServiceURL string
}
type FileMetadata struct {
Name string
Size int64
LastModified time.Time
}
func (c *AzureStorageClient) ListObjects(cfgValues configs.Config, isExtraClient bool) (interface{}, error) {
bucketName := getBucketName(cfgValues, isExtraClient)
pager := c.Client.NewListBlobsFlatPager(bucketName, nil)
var files []FileMetadata
for pager.More() {
resp, err := pager.NextPage(context.TODO())
if err != nil {
return nil, err
}
for _, v := range resp.Segment.BlobItems {
files = append(files, FileMetadata{
Name: *v.Name,
Size: *v.Properties.ContentLength,
LastModified: *v.Properties.LastModified,
})
}
}
return files, nil
}
func (c *AzureStorageClient) UploadFileToS3(filename string, cfgValues configs.Config, dailyPrefix string, isExtraClient bool) error {
bucketName := getBucketName(cfgValues, isExtraClient)
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("failed to open file %s: %v", filename, err)
}
defer file.Close()
_, err = c.Client.UploadFile(context.TODO(), bucketName, dailyPrefix+filepath.Base(filename), file,
&azblob.UploadFileOptions{
BlockSize: int64(1024),
Concurrency: uint16(3),
Progress: func(bytesTransferred int64) {},
})
if err != nil {
return fmt.Errorf("failed to upload file %s to Azure Blob Storage: %v", filename, err)
}
logrus.Infof("%s was successfully uploaded to Azure Blob Storage.\n", filename)
return nil
}
func (c *AzureStorageClient) RemoveFileFromS3(filename string, cfgValues configs.Config, isExtraClient bool) error {
bucketName := getBucketName(cfgValues, isExtraClient)
_, err := c.Client.DeleteBlob(context.TODO(), bucketName, filename, nil)
if err != nil {
return fmt.Errorf("failed to delete blob from Azure storage: %v", err)
}
logrus.Infof("%s was successfully removed from Azure Blob Storage.\n", filename)
return nil
}