This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
blob_client.go
116 lines (95 loc) · 3.17 KB
/
blob_client.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
package blobclient
import (
"context"
"fmt"
"io/ioutil" // nolint:staticcheck
"strings"
"time"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/giantswarm/microerror"
)
const (
blobSASValidityTime = 4320
maxRetriesRequests = 3
)
func ContainerExists(ctx context.Context, containerURL *azblob.ContainerURL) (bool, error) {
_, err := containerURL.GetProperties(ctx, azblob.LeaseAccessConditions{})
if IsContainerNotFound(err) {
return false, nil
} else if err != nil {
return false, microerror.Mask(err)
}
return true, nil
}
func PutBlockBlob(ctx context.Context, blobName string, payload string, containerURL *azblob.ContainerURL) (azblob.BlockBlobURL, error) {
blob := containerURL.NewBlockBlobURL(blobName)
_, err := blob.Upload(
ctx,
strings.NewReader(payload),
azblob.BlobHTTPHeaders{
ContentType: "text/plain",
},
azblob.Metadata{},
azblob.BlobAccessConditions{},
azblob.DefaultAccessTier,
nil,
azblob.ClientProvidedKeyOptions{},
azblob.ImmutabilityPolicyOptions{},
)
if err != nil {
return azblob.BlockBlobURL{}, microerror.Mask(err)
}
return blob, nil
}
func GetBlockBlob(ctx context.Context, blobName string, containerURL *azblob.ContainerURL) ([]byte, error) {
blobURL := containerURL.NewBlockBlobURL(blobName)
response, err := blobURL.Download(ctx, 0, azblob.CountToEnd, azblob.BlobAccessConditions{}, false, azblob.ClientProvidedKeyOptions{})
if err != nil {
return nil, microerror.Mask(err)
}
retryReaderOptions := azblob.RetryReaderOptions{
MaxRetryRequests: maxRetriesRequests,
}
defer response.Body(retryReaderOptions).Close() // nolint:errcheck
blobData, err := ioutil.ReadAll(response.Body(retryReaderOptions))
if err != nil {
return nil, microerror.Mask(err)
}
return blobData, nil
}
func GetBlobURL(blobName, containerName, storageAccountName, primaryKey string, containerURL *azblob.ContainerURL) (string, error) {
sharedKeyCredential, err := azblob.NewSharedKeyCredential(storageAccountName, primaryKey)
if err != nil {
return "", microerror.Mask(err)
}
// Set the desired SAS signature values and sign them
//with the shared key credentials to get the SAS query parameters.
sasQueryParams, err := azblob.BlobSASSignatureValues{
BlobName: blobName,
ContainerName: containerName,
ExpiryTime: time.Now().UTC().Add(blobSASValidityTime * time.Hour),
// give readonly access
Permissions: azblob.BlobSASPermissions{Add: false, Read: true, Write: false}.String(),
Protocol: azblob.SASProtocolHTTPS,
}.NewSASQueryParameters(sharedKeyCredential)
if err != nil {
return "", microerror.Mask(err)
}
blobURL := fmt.Sprintf("https://%s.blob.core.windows.net/%s/%s?%s", storageAccountName, containerName, blobName, sasQueryParams.Encode())
return blobURL, nil
}
func ListBlobs(ctx context.Context, containerURL *azblob.ContainerURL) (*azblob.ListBlobsFlatSegmentResponse, error) {
var listBlobs *azblob.ListBlobsFlatSegmentResponse
listBlobs, err := containerURL.ListBlobsFlatSegment(
ctx,
azblob.Marker{},
azblob.ListBlobsSegmentOptions{
Details: azblob.BlobListingDetails{
Snapshots: false,
},
})
if err != nil {
return nil, microerror.Mask(err)
}
return listBlobs, nil
}