-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
219 lines (192 loc) · 6.21 KB
/
main.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"context"
"errors"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/sas"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/service"
)
var (
subscriptionID string
resourceGroupName = "go-azure-sdk"
storageAccountName = "golangazure"
containerName = "golang-container-" + "test" // RandStringBytes(4)
)
var (
storageClientFactory *armstorage.ClientFactory
blobContainersClient *armstorage.BlobContainersClient
)
func genSaSToken(scred *azblob.SharedKeyCredential) {
sasQueryParams, err := sas.BlobSignatureValues{
Protocol: sas.ProtocolHTTPS,
StartTime: time.Now().UTC(),
ExpiryTime: time.Now().UTC().Add(15 * time.Minute),
Permissions: to.Ptr(sas.ContainerPermissions{Read: true, List: true}).String(),
ContainerName: containerName,
}.SignWithSharedKey(scred)
if err != nil {
log.Fatal(err)
return
}
sasURL := fmt.Sprintf("https://%s.blob.core.windows.net/%s?%s", storageAccountName, containerName, sasQueryParams.Encode())
fmt.Printf("SAS URL list files %v&restype=container&comp=list\n", sasURL)
}
func printSasToken(accountKey string, scred *azblob.SharedKeyCredential) {
blob_client, err := azblob.NewClientWithSharedKeyCredential(fmt.Sprintf("https://%s.blob.core.windows.net/%s", storageAccountName, containerName), scred, nil)
if err != nil {
log.Fatal(err)
return
}
cli_o := &service.GetSASURLOptions{ StartTime: to.Ptr( time.Now().Add(2 * time.Second) )}
sas_url, err := blob_client.ServiceClient().GetSASURL(
sas.AccountResourceTypes{ Container: true },
sas.AccountPermissions{
Create: true, Delete: true, List: true, Add: true,
},
time.Now().Add(24 * time.Hour),
cli_o,
)
if err != nil {
log.Fatal(err)
return
}
fmt.Printf("SAS URL list files %v&restype=container&comp=list\n", sas_url)
}
func main() {
fmt.Println("Starting azure golang main.")
accountKey := os.Getenv("AZURE_ACCOUNT_KEY")
if len(accountKey) == 0 {
log.Fatal("AZURE_ACCOUNT_KEY is not set.")
}
ctx := context.Background()
scred, err := azblob.NewSharedKeyCredential(storageAccountName, accountKey)
if err != nil {
log.Fatal(err)
}
blob_client, err := azblob.NewClientWithSharedKeyCredential(fmt.Sprintf("https://%s.blob.core.windows.net", storageAccountName), scred, nil)
if err != nil {
log.Fatal(err)
return
}
blob_resp, err := blob_client.CreateContainer(ctx, containerName, nil)
var blobErr *azcore.ResponseError
if errors.As(err, &blobErr) {
if blobErr.ErrorCode == "ContainerAlreadyExists" {
log.Println("Blob container already exists")
genSaSToken(scred)
return
} else {
log.Fatal(err)
return
}
} else {
log.Println("Created blob container vers " + *blob_resp.Version)
genSaSToken(scred)
return
}
// skipping the rest of main ( with armstorage and azidentity )
subscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID")
if len(subscriptionID) == 0 {
log.Fatal("AZURE_SUBSCRIPTION_ID is not set.")
}
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
// handle error
log.Fatal(err)
}
storageClientFactory, err = armstorage.NewClientFactory(subscriptionID, cred, nil)
if err != nil {
log.Fatal(err)
}
blobContainersClient = storageClientFactory.NewBlobContainersClient()
blobContainer, err := getBlobContainer(ctx)
var respErr *azcore.ResponseError
if errors.As(err, &respErr) {
// Handle Error
if respErr.StatusCode == http.StatusNotFound {
fmt.Printf("Blob container could not be found, return code: %v\n", respErr.ErrorCode)
fmt.Println("Creating it now...")
blobContainer_new, err_new := createBlobContainers(ctx)
if err_new != nil {
log.Fatal(err_new)
return
}
log.Println("Created blob container:", *blobContainer_new.ID)
blobContainer_again, err_again := getBlobContainer(ctx)
if err_again != nil {
log.Fatal(err_again)
return
}
log.Println("Double check, blob container ID:", *blobContainer_again.ID)
currentTime := time.Now()
tomorrow := currentTime.Add(24 * time.Hour)
client := storageClientFactory.NewAccountsClient()
sasToken, err := client.ListAccountSAS(ctx, resourceGroupName, storageAccountName, armstorage.AccountSasParameters{
KeyToSign: to.Ptr("key1"),
SharedAccessExpiryTime: to.Ptr(tomorrow.Round(time.Microsecond).UTC()),
Permissions: to.Ptr(armstorage.PermissionsR),
Protocols: to.Ptr(armstorage.HTTPProtocolHTTPSHTTP),
ResourceTypes: to.Ptr(armstorage.SignedResourceTypesS),
Services: to.Ptr(armstorage.ServicesB),
SharedAccessStartTime: to.Ptr(currentTime.Round(time.Microsecond).UTC()),
}, nil)
if err != nil {
log.Fatal(err)
return
}
token := *sasToken.AccountSasToken
fmt.Printf("SAS Token %v\n", token)
return
} else {
fmt.Printf("Container Get status code %d error code: %v", respErr.StatusCode, respErr.ErrorCode)
log.Fatal(respErr)
}
} else {
id := *blobContainer.ID
fmt.Printf("Blob container already exists, id: %v", id)
}
}
const letterBytes = "abcdefghijklmnopqrstuvwxyz"
func RandStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
func createBlobContainers(ctx context.Context) (*armstorage.BlobContainer, error) {
blobContainerResp, err := blobContainersClient.Create(
ctx,
resourceGroupName,
storageAccountName,
containerName,
armstorage.BlobContainer{
ContainerProperties: &armstorage.ContainerProperties{
PublicAccess: to.Ptr(armstorage.PublicAccessNone),
},
},
nil,
)
if err != nil {
return nil, err
}
return &blobContainerResp.BlobContainer, nil
}
func getBlobContainer(ctx context.Context) (blobContainer *armstorage.BlobContainer, err error) {
blobContainerResp, err := blobContainersClient.Get(ctx, resourceGroupName, storageAccountName, containerName, nil)
if err != nil {
return
}
blobContainer = &blobContainerResp.BlobContainer
return
}