-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure.go
More file actions
82 lines (74 loc) · 1.97 KB
/
azure.go
File metadata and controls
82 lines (74 loc) · 1.97 KB
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
package azure
import (
"context"
"fmt"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/siddontang/go/log"
"net/url"
"sync"
)
type Azure struct {
Name string `json:"name"`
Key string `json:"key"`
Credential azblob.Credential
ServiceURL azblob.ServiceURL
IsInit bool `json:"isInit"`
Containers map[string]*Container
mu sync.Mutex
}
type Operator interface {
GetContainerURL(containerName string) *Container
DeleteContainerURL(containerName string) bool
}
func GetAzure(name string, key string) *Azure {
cre, err := azblob.NewSharedKeyCredential(name, key)
if err != nil {
log.Infof("")
return nil
}
azure := &Azure{
Name: name,
Key: key,
Credential: cre,
}
u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", azure.Name))
azure.ServiceURL = azblob.NewServiceURL(*u, azblob.NewPipeline(azure.Credential, azblob.PipelineOptions{}))
azure.Containers = make(map[string]*Container, 0)
azure.IsInit = true
return azure
}
// GetContainerURL 创建一个 container。会创建一个新的。
func (a *Azure) GetContainerURL(containerName string) *Container {
a.mu.Lock()
defer a.mu.Unlock()
if c, ok := a.Containers[containerName]; ok {
return c
} else {
containerURL := a.ServiceURL.NewContainerURL(containerName)
c = &Container{
Name: containerName,
ContainerURL: containerURL,
UploadFiles: []UploadList{},
Blobs: make(map[string]*Blob, 0),
}
// todo: 此处对于 container 的查询,需要放掉
ctx := context.Background()
resp, err := containerURL.GetAccessPolicy(ctx, azblob.LeaseAccessConditions{})
if err != nil {
panic(err)
}
resp.BlobPublicAccess()
a.Containers[containerName] = c
return c
}
}
// DeleteContainerURL 删除一个 url
func (a *Azure) DeleteContainerURL(containerName string) bool {
a.mu.Lock()
defer a.mu.Unlock()
if _, ok := a.Containers[containerName]; ok {
delete(a.Containers, containerName)
return true
}
return true
}