15 "github.com/curoverse/azure-sdk-for-go/storage"
19 azureStorageAccountName string
20 azureStorageAccountKeyFile string
21 azureStorageReplication int
22 azureWriteRaceInterval time.Duration = 15 * time.Second
23 azureWriteRacePollTime time.Duration = time.Second
26 func readKeyFromFile(file string) (string, error) {
27 buf, err := ioutil.ReadFile(file)
29 return "", errors.New("reading key from " + file + ": " + err.Error())
31 accountKey := strings.TrimSpace(string(buf))
33 return "", errors.New("empty account key in " + file)
35 return accountKey, nil
38 type azureVolumeAdder struct {
42 func (s *azureVolumeAdder) Set(containerName string) error {
43 if containerName == "" {
44 return errors.New("no container name given")
46 if azureStorageAccountName == "" || azureStorageAccountKeyFile == "" {
47 return errors.New("-azure-storage-account-name and -azure-storage-account-key-file arguments must given before -azure-storage-container-volume")
49 accountKey, err := readKeyFromFile(azureStorageAccountKeyFile)
53 azClient, err := storage.NewBasicClient(azureStorageAccountName, accountKey)
55 return errors.New("creating Azure storage client: " + err.Error())
58 log.Print("Notice: -serialize is not supported by azure-blob-container volumes.")
60 v := NewAzureBlobVolume(azClient, containerName, flagReadonly, azureStorageReplication)
61 if err := v.Check(); err != nil {
64 *s.volumeSet = append(*s.volumeSet, v)
69 flag.Var(&azureVolumeAdder{&volumes},
70 "azure-storage-container-volume",
71 "Use the given container as a storage volume. Can be given multiple times.")
73 &azureStorageAccountName,
74 "azure-storage-account-name",
76 "Azure storage account name used for subsequent --azure-storage-container-volume arguments.")
78 &azureStorageAccountKeyFile,
79 "azure-storage-account-key-file",
81 "File containing the account key used for subsequent --azure-storage-container-volume arguments.")
83 &azureStorageReplication,
84 "azure-storage-replication",
86 "Replication level to report to clients when data is stored in an Azure container.")
89 // An AzureBlobVolume stores and retrieves blocks in an Azure Blob
91 type AzureBlobVolume struct {
92 azClient storage.Client
93 bsClient storage.BlobStorageClient
99 func NewAzureBlobVolume(client storage.Client, containerName string, readonly bool, replication int) *AzureBlobVolume {
100 return &AzureBlobVolume{
102 bsClient: client.GetBlobService(),
103 containerName: containerName,
105 replication: replication,
109 // Check returns nil if the volume is usable.
110 func (v *AzureBlobVolume) Check() error {
111 ok, err := v.bsClient.ContainerExists(v.containerName)
116 return errors.New("container does not exist")
121 func (v *AzureBlobVolume) Get(loc string) ([]byte, error) {
122 var deadline time.Time
123 haveDeadline := false
124 buf, err := v.get(loc)
125 for err == nil && len(buf) == 0 && loc[:32] != "d41d8cd98f00b204e9800998ecf8427e" {
126 // Seeing a brand new empty block probably means we're
127 // in a race with CreateBlob, which under the hood
128 // (apparently) does "CreateEmpty" and "CommitData"
129 // with no additional transaction locking.
131 t, err := v.Mtime(loc)
133 log.Print("Got empty block (possible race) but Mtime failed: ", err)
136 deadline = t.Add(azureWriteRaceInterval)
139 if time.Now().After(deadline) {
143 time.Sleep(azureWriteRacePollTime)
144 buf, err = v.get(loc)
149 func (v *AzureBlobVolume) get(loc string) ([]byte, error) {
150 rdr, err := v.bsClient.GetBlob(v.containerName, loc)
152 if strings.Contains(err.Error(), "404 Not Found") {
153 // "storage: service returned without a response body (404 Not Found)"
154 return nil, os.ErrNotExist
158 switch err := err.(type) {
161 log.Printf("ERROR IN Get(): %T %#v", err, err)
165 buf := bufs.Get(BlockSize)
166 n, err := io.ReadFull(rdr, buf)
168 case io.EOF, io.ErrUnexpectedEOF:
176 func (v *AzureBlobVolume) Compare(loc string, expect []byte) error {
177 rdr, err := v.bsClient.GetBlob(v.containerName, loc)
182 return compareReaderWithBuf(rdr, expect, loc[:32])
185 func (v *AzureBlobVolume) Put(loc string, block []byte) error {
187 return MethodDisabledError
189 return v.bsClient.CreateBlockBlobFromReader(v.containerName, loc, uint64(len(block)), bytes.NewReader(block))
192 func (v *AzureBlobVolume) Touch(loc string) error {
194 return MethodDisabledError
196 return v.bsClient.SetBlobMetadata(v.containerName, loc, map[string]string{
197 "touch": fmt.Sprintf("%d", time.Now()),
201 func (v *AzureBlobVolume) Mtime(loc string) (time.Time, error) {
202 props, err := v.bsClient.GetBlobProperties(v.containerName, loc)
204 return time.Time{}, err
206 return time.Parse(time.RFC1123, props.LastModified)
209 func (v *AzureBlobVolume) IndexTo(prefix string, writer io.Writer) error {
210 params := storage.ListBlobsParameters{
214 resp, err := v.bsClient.ListBlobs(v.containerName, params)
218 for _, b := range resp.Blobs {
219 t, err := time.Parse(time.RFC1123, b.Properties.LastModified)
223 fmt.Fprintf(writer, "%s+%d %d\n", b.Name, b.Properties.ContentLength, t.Unix())
225 if resp.NextMarker == "" {
228 params.Marker = resp.NextMarker
232 func (v *AzureBlobVolume) Delete(loc string) error {
234 return MethodDisabledError
236 // Ideally we would use If-Unmodified-Since, but that
237 // particular condition seems to be ignored by Azure. Instead,
238 // we get the Etag before checking Mtime, and use If-Match to
239 // ensure we don't delete data if Put() or Touch() happens
240 // between our calls to Mtime() and DeleteBlob().
241 props, err := v.bsClient.GetBlobProperties(v.containerName, loc)
245 if t, err := v.Mtime(loc); err != nil {
247 } else if time.Since(t) < blobSignatureTTL {
250 return v.bsClient.DeleteBlob(v.containerName, loc, map[string]string{
251 "If-Match": props.Etag,
255 func (v *AzureBlobVolume) Status() *VolumeStatus {
256 return &VolumeStatus{
258 BytesFree: BlockSize * 1000,
263 func (v *AzureBlobVolume) String() string {
264 return fmt.Sprintf("azure-storage-container:%+q", v.containerName)
267 func (v *AzureBlobVolume) Writable() bool {
271 func (v *AzureBlobVolume) Replication() int {