/
blob.go
89 lines (76 loc) · 2.11 KB
/
blob.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
package azblob
import (
"context"
"fmt"
"io"
"net/url"
"time"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/b4fun/prom-snapshot/pkg/snapshotsidecar/storage"
"github.com/sirupsen/logrus"
)
// StorageOption defines option for creating azure blob based storage.
type StorageOption struct {
Logger logrus.FieldLogger
ServicePrincipalToken func() (*adal.ServicePrincipalToken, error)
ContainerURL string
}
// Create creates an azblob instance.
func (s *StorageOption) Create() (*AZBlob, error) {
logger := s.Logger
if logger == nil {
logger = logrus.New()
}
containerURL, err := url.Parse(s.ContainerURL)
if err != nil {
return nil, fmt.Errorf("parse ContainerURL %s: %w", s.ContainerURL, err)
}
if s.ServicePrincipalToken == nil {
return nil, fmt.Errorf("ServicePrincipalToken is required")
}
spt, err := s.ServicePrincipalToken()
if err != nil {
return nil, fmt.Errorf("read service principal token: %w", err)
}
cred := azblob.NewTokenCredential(
spt.OAuthToken(),
func(credential azblob.TokenCredential) time.Duration {
_ = spt.RefreshWithContext(context.Background())
credential.SetToken(spt.OAuthToken())
return time.Until(spt.Token().Expires())
},
)
pipeline := azblob.NewPipeline(cred, azblob.PipelineOptions{})
azBlobContainerURL := azblob.NewContainerURL(*containerURL, pipeline)
return &AZBlob{
logger: logger,
containerURL: azBlobContainerURL,
}, nil
}
// AZBlob implements azure blob based storage.
type AZBlob struct {
logger logrus.FieldLogger
containerURL azblob.ContainerURL
}
var _ storage.Uploader = (*AZBlob)(nil)
func (ab *AZBlob) UploadStream(
ctx context.Context,
stream io.Reader,
name string,
) (string, error) {
blobURL := ab.containerURL.NewBlockBlobURL(name)
ab.logger.Debugf("blob url: %s", blobURL)
_, err := azblob.UploadStreamToBlockBlob(
ctx,
stream,
blobURL,
azblob.UploadStreamToBlockBlobOptions{},
)
if err != nil {
err = fmt.Errorf("upload %s: %w", name, err)
ab.logger.WithError(err).Error()
return "", err
}
return blobURL.String(), nil
}