15 "github.com/curoverse/azure-sdk-for-go/storage"
19 azureStorageAccountName string
20 azureStorageAccountKeyFile string
21 azureStorageReplication int
24 func readKeyFromFile(file string) (string, error) {
25 buf, err := ioutil.ReadFile(file)
27 return "", errors.New("reading key from " + file + ": " + err.Error())
29 accountKey := strings.TrimSpace(string(buf))
31 return "", errors.New("empty account key in " + file)
33 return accountKey, nil
36 type azureVolumeAdder struct {
40 func (s *azureVolumeAdder) Set(containerName string) error {
41 if containerName == "" {
42 return errors.New("no container name given")
44 if azureStorageAccountName == "" || azureStorageAccountKeyFile == "" {
45 return errors.New("-azure-storage-account-name and -azure-storage-account-key-file arguments must given before -azure-storage-container-volume")
47 accountKey, err := readKeyFromFile(azureStorageAccountKeyFile)
51 azClient, err := storage.NewBasicClient(azureStorageAccountName, accountKey)
53 return errors.New("creating Azure storage client: " + err.Error())
56 log.Print("Notice: -serialize is not supported by azure-blob-container volumes.")
58 v := NewAzureBlobVolume(azClient, containerName, flagReadonly, azureStorageReplication)
59 if err := v.Check(); err != nil {
62 *s.volumeSet = append(*s.volumeSet, v)
67 flag.Var(&azureVolumeAdder{&volumes},
68 "azure-storage-container-volume",
69 "Use the given container as a storage volume. Can be given multiple times.")
71 &azureStorageAccountName,
72 "azure-storage-account-name",
74 "Azure storage account name used for subsequent --azure-storage-container-volume arguments.")
76 &azureStorageAccountKeyFile,
77 "azure-storage-account-key-file",
79 "File containing the account key used for subsequent --azure-storage-container-volume arguments.")
81 &azureStorageReplication,
82 "azure-storage-replication",
84 "Replication level to report to clients when data is stored in an Azure container.")
87 // An AzureBlobVolume stores and retrieves blocks in an Azure Blob
89 type AzureBlobVolume struct {
90 azClient storage.Client
91 bsClient storage.BlobStorageClient
97 func NewAzureBlobVolume(client storage.Client, containerName string, readonly bool, replication int) *AzureBlobVolume {
98 return &AzureBlobVolume{
100 bsClient: client.GetBlobService(),
101 containerName: containerName,
103 replication: replication,
107 // Check returns nil if the volume is usable.
108 func (v *AzureBlobVolume) Check() error {
109 ok, err := v.bsClient.ContainerExists(v.containerName)
114 return errors.New("container does not exist")
119 func (v *AzureBlobVolume) Get(loc string) ([]byte, error) {
120 rdr, err := v.bsClient.GetBlob(v.containerName, loc)
122 if strings.Contains(err.Error(), "404 Not Found") {
123 // "storage: service returned without a response body (404 Not Found)"
124 return nil, os.ErrNotExist
128 switch err := err.(type) {
131 log.Printf("ERROR IN Get(): %T %#v", err, err)
135 buf := bufs.Get(BlockSize)
136 n, err := io.ReadFull(rdr, buf)
138 case io.EOF, io.ErrUnexpectedEOF:
146 func (v *AzureBlobVolume) Compare(loc string, expect []byte) error {
147 rdr, err := v.bsClient.GetBlob(v.containerName, loc)
152 return compareReaderWithBuf(rdr, expect, loc[:32])
155 func (v *AzureBlobVolume) Put(loc string, block []byte) error {
157 return MethodDisabledError
159 return v.bsClient.CreateBlockBlobFromReader(v.containerName, loc, uint64(len(block)), bytes.NewReader(block))
162 func (v *AzureBlobVolume) Touch(loc string) error {
164 return MethodDisabledError
166 return v.bsClient.SetBlobMetadata(v.containerName, loc, map[string]string{
167 "touch": fmt.Sprintf("%d", time.Now()),
171 func (v *AzureBlobVolume) Mtime(loc string) (time.Time, error) {
172 props, err := v.bsClient.GetBlobProperties(v.containerName, loc)
174 return time.Time{}, err
176 return time.Parse(time.RFC1123, props.LastModified)
179 func (v *AzureBlobVolume) IndexTo(prefix string, writer io.Writer) error {
180 params := storage.ListBlobsParameters{
184 resp, err := v.bsClient.ListBlobs(v.containerName, params)
188 for _, b := range resp.Blobs {
189 t, err := time.Parse(time.RFC1123, b.Properties.LastModified)
193 fmt.Fprintf(writer, "%s+%d %d\n", b.Name, b.Properties.ContentLength, t.Unix())
195 if resp.NextMarker == "" {
198 params.Marker = resp.NextMarker
202 func (v *AzureBlobVolume) Delete(loc string) error {
204 return MethodDisabledError
206 // Ideally we would use If-Unmodified-Since, but that
207 // particular condition seems to be ignored by Azure. Instead,
208 // we get the Etag before checking Mtime, and use If-Match to
209 // ensure we don't delete data if Put() or Touch() happens
210 // between our calls to Mtime() and DeleteBlob().
211 props, err := v.bsClient.GetBlobProperties(v.containerName, loc)
215 if t, err := v.Mtime(loc); err != nil {
217 } else if time.Since(t) < blobSignatureTTL {
220 return v.bsClient.DeleteBlob(v.containerName, loc, map[string]string{
221 "If-Match": props.Etag,
225 func (v *AzureBlobVolume) Status() *VolumeStatus {
226 return &VolumeStatus{
228 BytesFree: BlockSize * 1000,
233 func (v *AzureBlobVolume) String() string {
234 return fmt.Sprintf("azure-storage-container:%+q", v.containerName)
237 func (v *AzureBlobVolume) Writable() bool {
241 func (v *AzureBlobVolume) Replication() int {