-
Notifications
You must be signed in to change notification settings - Fork 10
/
storage.go
75 lines (59 loc) · 2.56 KB
/
storage.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package armhelpers
import (
"bytes"
"context"
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2018-02-01/storage"
azStorage "github.com/Azure/azure-sdk-for-go/storage"
"github.com/Azure/go-autorest/autorest/to"
)
// AzureStorageClient implements the StorageClient interface and wraps the Azure storage client.
type AzureStorageClient struct {
client *azStorage.Client
}
// GetStorageClient returns an authenticated client for the specified account.
func (az *AzureClient) GetStorageClient(ctx context.Context, resourceGroup, accountName string) (AKSStorageClient, error) {
keys, err := az.getStorageKeys(ctx, resourceGroup, accountName)
if err != nil {
return nil, err
}
client, err := azStorage.NewBasicClientOnSovereignCloud(accountName, to.String(keys[0].Value), az.environment)
if err != nil {
return nil, err
}
return &AzureStorageClient{
client: &client,
}, nil
}
func (az *AzureClient) getStorageKeys(ctx context.Context, resourceGroup, accountName string) ([]storage.AccountKey, error) {
storageKeysResult, err := az.storageAccountsClient.ListKeys(ctx, resourceGroup, accountName)
if err != nil {
return nil, err
}
return *storageKeysResult.Keys, nil
}
// DeleteBlob deletes the specified blob
// TODO(colemick): why doesn't SDK give a way to just delete a blob by URI?
// it's what it ends up doing internally anyway...
func (as *AzureStorageClient) DeleteBlob(vhdContainer, vhdBlob string, options *azStorage.DeleteBlobOptions) error {
containerRef := getContainerRef(as.client, vhdContainer)
blobRef := containerRef.GetBlobReference(vhdBlob)
return blobRef.Delete(options)
}
// CreateContainer creates the CloudBlobContainer if it does not exist
func (as *AzureStorageClient) CreateContainer(containerName string, options *azStorage.CreateContainerOptions) (bool, error) {
containerRef := getContainerRef(as.client, containerName)
created, err := containerRef.CreateIfNotExists(options)
return created, err
}
// SaveBlockBlob initializes a block blob by taking the byte
func (as *AzureStorageClient) SaveBlockBlob(containerName, blobName string, b []byte, options *azStorage.PutBlobOptions) error {
containerRef := getContainerRef(as.client, containerName)
blobRef := containerRef.GetBlobReference(blobName)
return blobRef.CreateBlockBlobFromReader(bytes.NewReader(b), options)
}
func getContainerRef(client *azStorage.Client, containerName string) *azStorage.Container {
bs := client.GetBlobService()
return bs.GetContainerReference(containerName)
}