aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Azure/azure-sdk-for-go/storage/blob.go
blob: 317620363d092d6e7716f01ce9384dc2679e9007 (plain) (blame)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
package storage

import (
    "bytes"
    "encoding/xml"
    "errors"
    "fmt"
    "io"
    "net/http"
    "net/url"
    "strconv"
    "strings"
    "time"
)

// BlobStorageClient contains operations for Microsoft Azure Blob Storage
// Service.
type BlobStorageClient struct {
    client Client
}

// A Container is an entry in ContainerListResponse.
type Container struct {
    Name       string              `xml:"Name"`
    Properties ContainerProperties `xml:"Properties"`
    // TODO (ahmetalpbalkan) Metadata
}

// ContainerProperties contains various properties of a container returned from
// various endpoints like ListContainers.
type ContainerProperties struct {
    LastModified  string `xml:"Last-Modified"`
    Etag          string `xml:"Etag"`
    LeaseStatus   string `xml:"LeaseStatus"`
    LeaseState    string `xml:"LeaseState"`
    LeaseDuration string `xml:"LeaseDuration"`
    // TODO (ahmetalpbalkan) remaining fields
}

// ContainerListResponse contains the response fields from
// ListContainers call.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179352.aspx
type ContainerListResponse struct {
    XMLName    xml.Name    `xml:"EnumerationResults"`
    Xmlns      string      `xml:"xmlns,attr"`
    Prefix     string      `xml:"Prefix"`
    Marker     string      `xml:"Marker"`
    NextMarker string      `xml:"NextMarker"`
    MaxResults int64       `xml:"MaxResults"`
    Containers []Container `xml:"Containers>Container"`
}

// A Blob is an entry in BlobListResponse.
type Blob struct {
    Name       string         `xml:"Name"`
    Properties BlobProperties `xml:"Properties"`
    Metadata   BlobMetadata   `xml:"Metadata"`
}

// BlobMetadata is a set of custom name/value pairs.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179404.aspx
type BlobMetadata map[string]string

type blobMetadataEntries struct {
    Entries []blobMetadataEntry `xml:",any"`
}
type blobMetadataEntry struct {
    XMLName xml.Name
    Value   string `xml:",chardata"`
}

// UnmarshalXML converts the xml:Metadata into Metadata map
func (bm *BlobMetadata) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    var entries blobMetadataEntries
    if err := d.DecodeElement(&entries, &start); err != nil {
        return err
    }
    for _, entry := range entries.Entries {
        if *bm == nil {
            *bm = make(BlobMetadata)
        }
        (*bm)[strings.ToLower(entry.XMLName.Local)] = entry.Value
    }
    return nil
}

// MarshalXML implements the xml.Marshaler interface. It encodes
// metadata name/value pairs as they would appear in an Azure
// ListBlobs response.
func (bm BlobMetadata) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
    entries := make([]blobMetadataEntry, 0, len(bm))
    for k, v := range bm {
        entries = append(entries, blobMetadataEntry{
            XMLName: xml.Name{Local: http.CanonicalHeaderKey(k)},
            Value:   v,
        })
    }
    return enc.EncodeElement(blobMetadataEntries{
        Entries: entries,
    }, start)
}

// BlobProperties contains various properties of a blob
// returned in various endpoints like ListBlobs or GetBlobProperties.
type BlobProperties struct {
    LastModified          string   `xml:"Last-Modified"`
    Etag                  string   `xml:"Etag"`
    ContentMD5            string   `xml:"Content-MD5"`
    ContentLength         int64    `xml:"Content-Length"`
    ContentType           string   `xml:"Content-Type"`
    ContentEncoding       string   `xml:"Content-Encoding"`
    CacheControl          string   `xml:"Cache-Control"`
    ContentLanguage       string   `xml:"Cache-Language"`
    BlobType              BlobType `xml:"x-ms-blob-blob-type"`
    SequenceNumber        int64    `xml:"x-ms-blob-sequence-number"`
    CopyID                string   `xml:"CopyId"`
    CopyStatus            string   `xml:"CopyStatus"`
    CopySource            string   `xml:"CopySource"`
    CopyProgress          string   `xml:"CopyProgress"`
    CopyCompletionTime    string   `xml:"CopyCompletionTime"`
    CopyStatusDescription string   `xml:"CopyStatusDescription"`
    LeaseStatus           string   `xml:"LeaseStatus"`
}

// BlobHeaders contains various properties of a blob and is an entry
// in SetBlobProperties
type BlobHeaders struct {
    ContentMD5      string `header:"x-ms-blob-content-md5"`
    ContentLanguage string `header:"x-ms-blob-content-language"`
    ContentEncoding string `header:"x-ms-blob-content-encoding"`
    ContentType     string `header:"x-ms-blob-content-type"`
    CacheControl    string `header:"x-ms-blob-cache-control"`
}

// BlobListResponse contains the response fields from ListBlobs call.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd135734.aspx
type BlobListResponse struct {
    XMLName    xml.Name `xml:"EnumerationResults"`
    Xmlns      string   `xml:"xmlns,attr"`
    Prefix     string   `xml:"Prefix"`
    Marker     string   `xml:"Marker"`
    NextMarker string   `xml:"NextMarker"`
    MaxResults int64    `xml:"MaxResults"`
    Blobs      []Blob   `xml:"Blobs>Blob"`

    // BlobPrefix is used to traverse blobs as if it were a file system.
    // It is returned if ListBlobsParameters.Delimiter is specified.
    // The list here can be thought of as "folders" that may contain
    // other folders or blobs.
    BlobPrefixes []string `xml:"Blobs>BlobPrefix>Name"`

    // Delimiter is used to traverse blobs as if it were a file system.
    // It is returned if ListBlobsParameters.Delimiter is specified.
    Delimiter string `xml:"Delimiter"`
}

// ListContainersParameters defines the set of customizable parameters to make a
// List Containers call.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179352.aspx
type ListContainersParameters struct {
    Prefix     string
    Marker     string
    Include    string
    MaxResults uint
    Timeout    uint
}

func (p ListContainersParameters) getParameters() url.Values {
    out := url.Values{}

    if p.Prefix != "" {
        out.Set("prefix", p.Prefix)
    }
    if p.Marker != "" {
        out.Set("marker", p.Marker)
    }
    if p.Include != "" {
        out.Set("include", p.Include)
    }
    if p.MaxResults != 0 {
        out.Set("maxresults", fmt.Sprintf("%v", p.MaxResults))
    }
    if p.Timeout != 0 {
        out.Set("timeout", fmt.Sprintf("%v", p.Timeout))
    }

    return out
}

// ListBlobsParameters defines the set of customizable
// parameters to make a List Blobs call.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd135734.aspx
type ListBlobsParameters struct {
    Prefix     string
    Delimiter  string
    Marker     string
    Include    string
    MaxResults uint
    Timeout    uint
}

func (p ListBlobsParameters) getParameters() url.Values {
    out := url.Values{}

    if p.Prefix != "" {
        out.Set("prefix", p.Prefix)
    }
    if p.Delimiter != "" {
        out.Set("delimiter", p.Delimiter)
    }
    if p.Marker != "" {
        out.Set("marker", p.Marker)
    }
    if p.Include != "" {
        out.Set("include", p.Include)
    }
    if p.MaxResults != 0 {
        out.Set("maxresults", fmt.Sprintf("%v", p.MaxResults))
    }
    if p.Timeout != 0 {
        out.Set("timeout", fmt.Sprintf("%v", p.Timeout))
    }

    return out
}

// BlobType defines the type of the Azure Blob.
type BlobType string

// Types of page blobs
const (
    BlobTypeBlock  BlobType = "BlockBlob"
    BlobTypePage   BlobType = "PageBlob"
    BlobTypeAppend BlobType = "AppendBlob"
)

// PageWriteType defines the type updates that are going to be
// done on the page blob.
type PageWriteType string

// Types of operations on page blobs
const (
    PageWriteTypeUpdate PageWriteType = "update"
    PageWriteTypeClear  PageWriteType = "clear"
)

const (
    blobCopyStatusPending = "pending"
    blobCopyStatusSuccess = "success"
    blobCopyStatusAborted = "aborted"
    blobCopyStatusFailed  = "failed"
)

// lease constants.
const (
    leaseHeaderPrefix = "x-ms-lease-"
    leaseID           = "x-ms-lease-id"
    leaseAction       = "x-ms-lease-action"
    leaseBreakPeriod  = "x-ms-lease-break-period"
    leaseDuration     = "x-ms-lease-duration"
    leaseProposedID   = "x-ms-proposed-lease-id"
    leaseTime         = "x-ms-lease-time"

    acquireLease = "acquire"
    renewLease   = "renew"
    changeLease  = "change"
    releaseLease = "release"
    breakLease   = "break"
)

// BlockListType is used to filter out types of blocks in a Get Blocks List call
// for a block blob.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179400.aspx for all
// block types.
type BlockListType string

// Filters for listing blocks in block blobs
const (
    BlockListTypeAll         BlockListType = "all"
    BlockListTypeCommitted   BlockListType = "committed"
    BlockListTypeUncommitted BlockListType = "uncommitted"
)

// ContainerAccessType defines the access level to the container from a public
// request.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179468.aspx and "x-ms-
// blob-public-access" header.
type ContainerAccessType string

// Access options for containers
const (
    ContainerAccessTypePrivate   ContainerAccessType = ""
    ContainerAccessTypeBlob      ContainerAccessType = "blob"
    ContainerAccessTypeContainer ContainerAccessType = "container"
)

// Maximum sizes (per REST API) for various concepts
const (
    MaxBlobBlockSize = 4 * 1024 * 1024
    MaxBlobPageSize  = 4 * 1024 * 1024
)

// BlockStatus defines states a block for a block blob can
// be in.
type BlockStatus string

// List of statuses that can be used to refer to a block in a block list
const (
    BlockStatusUncommitted BlockStatus = "Uncommitted"
    BlockStatusCommitted   BlockStatus = "Committed"
    BlockStatusLatest      BlockStatus = "Latest"
)

// Block is used to create Block entities for Put Block List
// call.
type Block struct {
    ID     string
    Status BlockStatus
}

// BlockListResponse contains the response fields from Get Block List call.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179400.aspx
type BlockListResponse struct {
    XMLName           xml.Name        `xml:"BlockList"`
    CommittedBlocks   []BlockResponse `xml:"CommittedBlocks>Block"`
    UncommittedBlocks []BlockResponse `xml:"UncommittedBlocks>Block"`
}

// BlockResponse contains the block information returned
// in the GetBlockListCall.
type BlockResponse struct {
    Name string `xml:"Name"`
    Size int64  `xml:"Size"`
}

// GetPageRangesResponse contains the reponse fields from
// Get Page Ranges call.
//
// See https://msdn.microsoft.com/en-us/library/azure/ee691973.aspx
type GetPageRangesResponse struct {
    XMLName  xml.Name    `xml:"PageList"`
    PageList []PageRange `xml:"PageRange"`
}

// PageRange contains information about a page of a page blob from
// Get Pages Range call.
//
// See https://msdn.microsoft.com/en-us/library/azure/ee691973.aspx
type PageRange struct {
    Start int64 `xml:"Start"`
    End   int64 `xml:"End"`
}

var (
    errBlobCopyAborted    = errors.New("storage: blob copy is aborted")
    errBlobCopyIDMismatch = errors.New("storage: blob copy id is a mismatch")
)

// ListContainers returns the list of containers in a storage account along with
// pagination token and other response details.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179352.aspx
func (b BlobStorageClient) ListContainers(params ListContainersParameters) (ContainerListResponse, error) {
    q := mergeParams(params.getParameters(), url.Values{"comp": {"list"}})
    uri := b.client.getEndpoint(blobServiceName, "", q)
    headers := b.client.getStandardHeaders()

    var out ContainerListResponse
    resp, err := b.client.exec("GET", uri, headers, nil)
    if err != nil {
        return out, err
    }
    defer resp.body.Close()

    err = xmlUnmarshal(resp.body, &out)
    return out, err
}

// CreateContainer creates a blob container within the storage account
// with given name and access level. Returns error if container already exists.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179468.aspx
func (b BlobStorageClient) CreateContainer(name string, access ContainerAccessType) error {
    resp, err := b.createContainer(name, access)
    if err != nil {
        return err
    }
    defer resp.body.Close()
    return checkRespCode(resp.statusCode, []int{http.StatusCreated})
}

// CreateContainerIfNotExists creates a blob container if it does not exist. Returns
// true if container is newly created or false if container already exists.
func (b BlobStorageClient) CreateContainerIfNotExists(name string, access ContainerAccessType) (bool, error) {
    resp, err := b.createContainer(name, access)
    if resp != nil {
        defer resp.body.Close()
        if resp.statusCode == http.StatusCreated || resp.statusCode == http.StatusConflict {
            return resp.statusCode == http.StatusCreated, nil
        }
    }
    return false, err
}

func (b BlobStorageClient) createContainer(name string, access ContainerAccessType) (*storageResponse, error) {
    verb := "PUT"
    uri := b.client.getEndpoint(blobServiceName, pathForContainer(name), url.Values{"restype": {"container"}})

    headers := b.client.getStandardHeaders()
    if access != "" {
        headers["x-ms-blob-public-access"] = string(access)
    }
    return b.client.exec(verb, uri, headers, nil)
}

// ContainerExists returns true if a container with given name exists
// on the storage account, otherwise returns false.
func (b BlobStorageClient) ContainerExists(name string) (bool, error) {
    verb := "HEAD"
    uri := b.client.getEndpoint(blobServiceName, pathForContainer(name), url.Values{"restype": {"container"}})
    headers := b.client.getStandardHeaders()

    resp, err := b.client.exec(verb, uri, headers, nil)
    if resp != nil {
        defer resp.body.Close()
        if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound {
            return resp.statusCode == http.StatusOK, nil
        }
    }
    return false, err
}

// DeleteContainer deletes the container with given name on the storage
// account. If the container does not exist returns error.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179408.aspx
func (b BlobStorageClient) DeleteContainer(name string) error {
    resp, err := b.deleteContainer(name)
    if err != nil {
        return err
    }
    defer resp.body.Close()
    return checkRespCode(resp.statusCode, []int{http.StatusAccepted})
}

// DeleteContainerIfExists deletes the container with given name on the storage
// account if it exists. Returns true if container is deleted with this call, or
// false if the container did not exist at the time of the Delete Container
// operation.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179408.aspx
func (b BlobStorageClient) DeleteContainerIfExists(name string) (bool, error) {
    resp, err := b.deleteContainer(name)
    if resp != nil {
        defer resp.body.Close()
        if resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound {
            return resp.statusCode == http.StatusAccepted, nil
        }
    }
    return false, err
}

func (b BlobStorageClient) deleteContainer(name string) (*storageResponse, error) {
    verb := "DELETE"
    uri := b.client.getEndpoint(blobServiceName, pathForContainer(name), url.Values{"restype": {"container"}})

    headers := b.client.getStandardHeaders()
    return b.client.exec(verb, uri, headers, nil)
}

// ListBlobs returns an object that contains list of blobs in the container,
// pagination token and other information in the response of List Blobs call.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd135734.aspx
func (b BlobStorageClient) ListBlobs(container string, params ListBlobsParameters) (BlobListResponse, error) {
    q := mergeParams(params.getParameters(), url.Values{
        "restype": {"container"},
        "comp":    {"list"}})
    uri := b.client.getEndpoint(blobServiceName, pathForContainer(container), q)
    headers := b.client.getStandardHeaders()

    var out BlobListResponse
    resp, err := b.client.exec("GET", uri, headers, nil)
    if err != nil {
        return out, err
    }
    defer resp.body.Close()

    err = xmlUnmarshal(resp.body, &out)
    return out, err
}

// BlobExists returns true if a blob with given name exists on the specified
// container of the storage account.
func (b BlobStorageClient) BlobExists(container, name string) (bool, error) {
    verb := "HEAD"
    uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{})
    headers := b.client.getStandardHeaders()
    resp, err := b.client.exec(verb, uri, headers, nil)
    if resp != nil {
        defer resp.body.Close()
        if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound {
            return resp.statusCode == http.StatusOK, nil
        }
    }
    return false, err
}

// GetBlobURL gets the canonical URL to the blob with the specified name in the
// specified container. This method does not create a publicly accessible URL if
// the blob or container is private and this method does not check if the blob
// exists.
func (b BlobStorageClient) GetBlobURL(container, name string) string {
    if container == "" {
        container = "$root"
    }
    return b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{})
}

// GetBlob returns a stream to read the blob. Caller must call Close() the
// reader to close on the underlying connection.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179440.aspx
func (b BlobStorageClient) GetBlob(container, name string) (io.ReadCloser, error) {
    resp, err := b.getBlobRange(container, name, "", nil)
    if err != nil {
        return nil, err
    }

    if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil {
        return nil, err
    }
    return resp.body, nil
}

// GetBlobRange reads the specified range of a blob to a stream. The bytesRange
// string must be in a format like "0-", "10-100" as defined in HTTP 1.1 spec.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179440.aspx
func (b BlobStorageClient) GetBlobRange(container, name, bytesRange string, extraHeaders map[string]string) (io.ReadCloser, error) {
    resp, err := b.getBlobRange(container, name, bytesRange, extraHeaders)
    if err != nil {
        return nil, err
    }

    if err := checkRespCode(resp.statusCode, []int{http.StatusPartialContent}); err != nil {
        return nil, err
    }
    return resp.body, nil
}

func (b BlobStorageClient) getBlobRange(container, name, bytesRange string, extraHeaders map[string]string) (*storageResponse, error) {
    verb := "GET"
    uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{})

    headers := b.client.getStandardHeaders()
    if bytesRange != "" {
        headers["Range"] = fmt.Sprintf("bytes=%s", bytesRange)
    }

    for k, v := range extraHeaders {
        headers[k] = v
    }

    resp, err := b.client.exec(verb, uri, headers, nil)
    if err != nil {
        return nil, err
    }
    return resp, err
}

// leasePut is common PUT code for the various aquire/release/break etc functions.
func (b BlobStorageClient) leaseCommonPut(container string, name string, headers map[string]string, expectedStatus int) (http.Header, error) {
    params := url.Values{"comp": {"lease"}}
    uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params)

    resp, err := b.client.exec("PUT", uri, headers, nil)
    if err != nil {
        return nil, err
    }
    defer resp.body.Close()

    if err := checkRespCode(resp.statusCode, []int{expectedStatus}); err != nil {
        return nil, err
    }

    return resp.headers, nil
}

// AcquireLease creates a lease for a blob as per https://msdn.microsoft.com/en-us/library/azure/ee691972.aspx
// returns leaseID acquired
func (b BlobStorageClient) AcquireLease(container string, name string, leaseTimeInSeconds int, proposedLeaseID string) (returnedLeaseID string, err error) {
    headers := b.client.getStandardHeaders()
    headers[leaseAction] = acquireLease
    headers[leaseProposedID] = proposedLeaseID
    headers[leaseDuration] = strconv.Itoa(leaseTimeInSeconds)

    respHeaders, err := b.leaseCommonPut(container, name, headers, http.StatusCreated)
    if err != nil {
        return "", err
    }

    returnedLeaseID = respHeaders.Get(http.CanonicalHeaderKey(leaseID))

    if returnedLeaseID != "" {
        return returnedLeaseID, nil
    }

    // what should we return in case of HTTP 201 but no lease ID?
    // or it just cant happen? (brave words)
    return "", errors.New("LeaseID not returned")
}

// BreakLease breaks the lease for a blob as per https://msdn.microsoft.com/en-us/library/azure/ee691972.aspx
// Returns the timeout remaining in the lease in seconds
func (b BlobStorageClient) BreakLease(container string, name string) (breakTimeout int, err error) {
    headers := b.client.getStandardHeaders()
    headers[leaseAction] = breakLease
    return b.breakLeaseCommon(container, name, headers)
}

// BreakLeaseWithBreakPeriod breaks the lease for a blob as per https://msdn.microsoft.com/en-us/library/azure/ee691972.aspx
// breakPeriodInSeconds is used to determine how long until new lease can be created.
// Returns the timeout remaining in the lease in seconds
func (b BlobStorageClient) BreakLeaseWithBreakPeriod(container string, name string, breakPeriodInSeconds int) (breakTimeout int, err error) {
    headers := b.client.getStandardHeaders()
    headers[leaseAction] = breakLease
    headers[leaseBreakPeriod] = strconv.Itoa(breakPeriodInSeconds)
    return b.breakLeaseCommon(container, name, headers)
}

// breakLeaseCommon is common code for both version of BreakLease (with and without break period)
func (b BlobStorageClient) breakLeaseCommon(container string, name string, headers map[string]string) (breakTimeout int, err error) {

    respHeaders, err := b.leaseCommonPut(container, name, headers, http.StatusAccepted)
    if err != nil {
        return 0, err
    }

    breakTimeoutStr := respHeaders.Get(http.CanonicalHeaderKey(leaseTime))
    if breakTimeoutStr != "" {
        breakTimeout, err = strconv.Atoi(breakTimeoutStr)
        if err != nil {
            return 0, err
        }
    }

    return breakTimeout, nil
}

// ChangeLease changes a lease ID for a blob as per https://msdn.microsoft.com/en-us/library/azure/ee691972.aspx
// Returns the new LeaseID acquired
func (b BlobStorageClient) ChangeLease(container string, name string, currentLeaseID string, proposedLeaseID string) (newLeaseID string, err error) {
    headers := b.client.getStandardHeaders()
    headers[leaseAction] = changeLease
    headers[leaseID] = currentLeaseID
    headers[leaseProposedID] = proposedLeaseID

    respHeaders, err := b.leaseCommonPut(container, name, headers, http.StatusOK)
    if err != nil {
        return "", err
    }

    newLeaseID = respHeaders.Get(http.CanonicalHeaderKey(leaseID))
    if newLeaseID != "" {
        return newLeaseID, nil
    }

    return "", errors.New("LeaseID not returned")
}

// ReleaseLease releases the lease for a blob as per https://msdn.microsoft.com/en-us/library/azure/ee691972.aspx
func (b BlobStorageClient) ReleaseLease(container string, name string, currentLeaseID string) error {
    headers := b.client.getStandardHeaders()
    headers[leaseAction] = releaseLease
    headers[leaseID] = currentLeaseID

    _, err := b.leaseCommonPut(container, name, headers, http.StatusOK)
    if err != nil {
        return err
    }

    return nil
}

// RenewLease renews the lease for a blob as per https://msdn.microsoft.com/en-us/library/azure/ee691972.aspx
func (b BlobStorageClient) RenewLease(container string, name string, currentLeaseID string) error {
    headers := b.client.getStandardHeaders()
    headers[leaseAction] = renewLease
    headers[leaseID] = currentLeaseID

    _, err := b.leaseCommonPut(container, name, headers, http.StatusOK)
    if err != nil {
        return err
    }

    return nil
}

// GetBlobProperties provides various information about the specified
// blob. See https://msdn.microsoft.com/en-us/library/azure/dd179394.aspx
func (b BlobStorageClient) GetBlobProperties(container, name string) (*BlobProperties, error) {
    verb := "HEAD"
    uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{})

    headers := b.client.getStandardHeaders()
    resp, err := b.client.exec(verb, uri, headers, nil)
    if err != nil {
        return nil, err
    }
    defer resp.body.Close()

    if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil {
        return nil, err
    }

    var contentLength int64
    contentLengthStr := resp.headers.Get("Content-Length")
    if contentLengthStr != "" {
        contentLength, err = strconv.ParseInt(contentLengthStr, 0, 64)
        if err != nil {
            return nil, err
        }
    }

    var sequenceNum int64
    sequenceNumStr := resp.headers.Get("x-ms-blob-sequence-number")
    if sequenceNumStr != "" {
        sequenceNum, err = strconv.ParseInt(sequenceNumStr, 0, 64)
        if err != nil {
            return nil, err
        }
    }

    return &BlobProperties{
        LastModified:          resp.headers.Get("Last-Modified"),
        Etag:                  resp.headers.Get("Etag"),
        ContentMD5:            resp.headers.Get("Content-MD5"),
        ContentLength:         contentLength,
        ContentEncoding:       resp.headers.Get("Content-Encoding"),
        ContentType:           resp.headers.Get("Content-Type"),
        CacheControl:          resp.headers.Get("Cache-Control"),
        ContentLanguage:       resp.headers.Get("Content-Language"),
        SequenceNumber:        sequenceNum,
        CopyCompletionTime:    resp.headers.Get("x-ms-copy-completion-time"),
        CopyStatusDescription: resp.headers.Get("x-ms-copy-status-description"),
        CopyID:                resp.headers.Get("x-ms-copy-id"),
        CopyProgress:          resp.headers.Get("x-ms-copy-progress"),
        CopySource:            resp.headers.Get("x-ms-copy-source"),
        CopyStatus:            resp.headers.Get("x-ms-copy-status"),
        BlobType:              BlobType(resp.headers.Get("x-ms-blob-type")),
        LeaseStatus:           resp.headers.Get("x-ms-lease-status"),
    }, nil
}

// SetBlobProperties replaces the BlobHeaders for the specified blob.
//
// Some keys may be converted to Camel-Case before sending. All keys
// are returned in lower case by GetBlobProperties. HTTP header names
// are case-insensitive so case munging should not matter to other
// applications either.
//
// See https://msdn.microsoft.com/en-us/library/azure/ee691966.aspx
func (b BlobStorageClient) SetBlobProperties(container, name string, blobHeaders BlobHeaders) error {
    params := url.Values{"comp": {"properties"}}
    uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params)
    headers := b.client.getStandardHeaders()

    extraHeaders := headersFromStruct(blobHeaders)

    for k, v := range extraHeaders {
        headers[k] = v
    }

    resp, err := b.client.exec("PUT", uri, headers, nil)
    if err != nil {
        return err
    }
    defer resp.body.Close()

    return checkRespCode(resp.statusCode, []int{http.StatusOK})
}

// SetBlobMetadata replaces the metadata for the specified blob.
//
// Some keys may be converted to Camel-Case before sending. All keys
// are returned in lower case by GetBlobMetadata. HTTP header names
// are case-insensitive so case munging should not matter to other
// applications either.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx
func (b BlobStorageClient) SetBlobMetadata(container, name string, metadata map[string]string, extraHeaders map[string]string) error {
    params := url.Values{"comp": {"metadata"}}
    uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params)
    headers := b.client.getStandardHeaders()
    for k, v := range metadata {
        headers[userDefinedMetadataHeaderPrefix+k] = v
    }

    for k, v := range extraHeaders {
        headers[k] = v
    }

    resp, err := b.client.exec("PUT", uri, headers, nil)
    if err != nil {
        return err
    }
    defer resp.body.Close()

    return checkRespCode(resp.statusCode, []int{http.StatusOK})
}

// GetBlobMetadata returns all user-defined metadata for the specified blob.
//
// All metadata keys will be returned in lower case. (HTTP header
// names are case-insensitive.)
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx
func (b BlobStorageClient) GetBlobMetadata(container, name string) (map[string]string, error) {
    params := url.Values{"comp": {"metadata"}}
    uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params)
    headers := b.client.getStandardHeaders()

    resp, err := b.client.exec("GET", uri, headers, nil)
    if err != nil {
        return nil, err
    }
    defer resp.body.Close()

    if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil {
        return nil, err
    }

    metadata := make(map[string]string)
    for k, v := range resp.headers {
        // Can't trust CanonicalHeaderKey() to munge case
        // reliably. "_" is allowed in identifiers:
        // https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx
        // https://msdn.microsoft.com/library/aa664670(VS.71).aspx
        // http://tools.ietf.org/html/rfc7230#section-3.2
        // ...but "_" is considered invalid by
        // CanonicalMIMEHeaderKey in
        // https://golang.org/src/net/textproto/reader.go?s=14615:14659#L542
        // so k can be "X-Ms-Meta-Foo" or "x-ms-meta-foo_bar".
        k = strings.ToLower(k)
        if len(v) == 0 || !strings.HasPrefix(k, strings.ToLower(userDefinedMetadataHeaderPrefix)) {
            continue
        }
        // metadata["foo"] = content of the last X-Ms-Meta-Foo header
        k = k[len(userDefinedMetadataHeaderPrefix):]
        metadata[k] = v[len(v)-1]
    }
    return metadata, nil
}

// CreateBlockBlob initializes an empty block blob with no blocks.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx
func (b BlobStorageClient) CreateBlockBlob(container, name string) error {
    return b.CreateBlockBlobFromReader(container, name, 0, nil, nil)
}

// CreateBlockBlobFromReader initializes a block blob using data from
// reader. Size must be the number of bytes read from reader. To
// create an empty blob, use size==0 and reader==nil.
//
// The API rejects requests with size > 64 MiB (but this limit is not
// checked by the SDK). To write a larger blob, use CreateBlockBlob,
// PutBlock, and PutBlockList.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx
func (b BlobStorageClient) CreateBlockBlobFromReader(container, name string, size uint64, blob io.Reader, extraHeaders map[string]string) error {
    path := fmt.Sprintf("%s/%s", container, name)
    uri := b.client.getEndpoint(blobServiceName, path, url.Values{})
    headers := b.client.getStandardHeaders()
    headers["x-ms-blob-type"] = string(BlobTypeBlock)
    headers["Content-Length"] = fmt.Sprintf("%d", size)

    for k, v := range extraHeaders {
        headers[k] = v
    }

    resp, err := b.client.exec("PUT", uri, headers, blob)
    if err != nil {
        return err
    }
    defer resp.body.Close()
    return checkRespCode(resp.statusCode, []int{http.StatusCreated})
}

// PutBlock saves the given data chunk to the specified block blob with
// given ID.
//
// The API rejects chunks larger than 4 MiB (but this limit is not
// checked by the SDK).
//
// See https://msdn.microsoft.com/en-us/library/azure/dd135726.aspx
func (b BlobStorageClient) PutBlock(container, name, blockID string, chunk []byte) error {
    return b.PutBlockWithLength(container, name, blockID, uint64(len(chunk)), bytes.NewReader(chunk), nil)
}

// PutBlockWithLength saves the given data stream of exactly specified size to
// the block blob with given ID. It is an alternative to PutBlocks where data
// comes as stream but the length is known in advance.
//
// The API rejects requests with size > 4 MiB (but this limit is not
// checked by the SDK).
//
// See https://msdn.microsoft.com/en-us/library/azure/dd135726.aspx
func (b BlobStorageClient) PutBlockWithLength(container, name, blockID string, size uint64, blob io.Reader, extraHeaders map[string]string) error {
    uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{"comp": {"block"}, "blockid": {blockID}})
    headers := b.client.getStandardHeaders()
    headers["x-ms-blob-type"] = string(BlobTypeBlock)
    headers["Content-Length"] = fmt.Sprintf("%v", size)

    for k, v := range extraHeaders {
        headers[k] = v
    }

    resp, err := b.client.exec("PUT", uri, headers, blob)
    if err != nil {
        return err
    }

    defer resp.body.Close()
    return checkRespCode(resp.statusCode, []int{http.StatusCreated})
}

// PutBlockList saves list of blocks to the specified block blob.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179467.aspx
func (b BlobStorageClient) PutBlockList(container, name string, blocks []Block) error {
    blockListXML := prepareBlockListRequest(blocks)

    uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{"comp": {"blocklist"}})
    headers := b.client.getStandardHeaders()
    headers["Content-Length"] = fmt.Sprintf("%v", len(blockListXML))

    resp, err := b.client.exec("PUT", uri, headers, strings.NewReader(blockListXML))
    if err != nil {
        return err
    }
    defer resp.body.Close()
    return checkRespCode(resp.statusCode, []int{http.StatusCreated})
}

// GetBlockList retrieves list of blocks in the specified block blob.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179400.aspx
func (b BlobStorageClient) GetBlockList(container, name string, blockType BlockListType) (BlockListResponse, error) {
    params := url.Values{"comp": {"blocklist"}, "blocklisttype": {string(blockType)}}
    uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), params)
    headers := b.client.getStandardHeaders()

    var out BlockListResponse
    resp, err := b.client.exec("GET", uri, headers, nil)
    if err != nil {
        return out, err
    }
    defer resp.body.Close()

    err = xmlUnmarshal(resp.body, &out)
    return out, err
}

// PutPageBlob initializes an empty page blob with specified name and maximum
// size in bytes (size must be aligned to a 512-byte boundary). A page blob must
// be created using this method before writing pages.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx
func (b BlobStorageClient) PutPageBlob(container, name string, size int64, extraHeaders map[string]string) error {
    path := fmt.Sprintf("%s/%s", container, name)
    uri := b.client.getEndpoint(blobServiceName, path, url.Values{})
    headers := b.client.getStandardHeaders()
    headers["x-ms-blob-type"] = string(BlobTypePage)
    headers["x-ms-blob-content-length"] = fmt.Sprintf("%v", size)

    for k, v := range extraHeaders {
        headers[k] = v
    }

    resp, err := b.client.exec("PUT", uri, headers, nil)
    if err != nil {
        return err
    }
    defer resp.body.Close()

    return checkRespCode(resp.statusCode, []int{http.StatusCreated})
}

// PutPage writes a range of pages to a page blob or clears the given range.
// In case of 'clear' writes, given chunk is discarded. Ranges must be aligned
// with 512-byte boundaries and chunk must be of size multiplies by 512.
//
// See https://msdn.microsoft.com/en-us/library/ee691975.aspx
func (b BlobStorageClient) PutPage(container, name string, startByte, endByte int64, writeType PageWriteType, chunk []byte, extraHeaders map[string]string) error {
    path := fmt.Sprintf("%s/%s", container, name)
    uri := b.client.getEndpoint(blobServiceName, path, url.Values{"comp": {"page"}})
    headers := b.client.getStandardHeaders()
    headers["x-ms-blob-type"] = string(BlobTypePage)
    headers["x-ms-page-write"] = string(writeType)
    headers["x-ms-range"] = fmt.Sprintf("bytes=%v-%v", startByte, endByte)
    for k, v := range extraHeaders {
        headers[k] = v
    }
    var contentLength int64
    var data io.Reader
    if writeType == PageWriteTypeClear {
        contentLength = 0
        data = bytes.NewReader([]byte{})
    } else {
        contentLength = int64(len(chunk))
        data = bytes.NewReader(chunk)
    }
    headers["Content-Length"] = fmt.Sprintf("%v", contentLength)

    resp, err := b.client.exec("PUT", uri, headers, data)
    if err != nil {
        return err
    }
    defer resp.body.Close()

    return checkRespCode(resp.statusCode, []int{http.StatusCreated})
}

// GetPageRanges returns the list of valid page ranges for a page blob.
//
// See https://msdn.microsoft.com/en-us/library/azure/ee691973.aspx
func (b BlobStorageClient) GetPageRanges(container, name string) (GetPageRangesResponse, error) {
    path := fmt.Sprintf("%s/%s", container, name)
    uri := b.client.getEndpoint(blobServiceName, path, url.Values{"comp": {"pagelist"}})
    headers := b.client.getStandardHeaders()

    var out GetPageRangesResponse
    resp, err := b.client.exec("GET", uri, headers, nil)
    if err != nil {
        return out, err
    }
    defer resp.body.Close()

    if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil {
        return out, err
    }
    err = xmlUnmarshal(resp.body, &out)
    return out, err
}

// PutAppendBlob initializes an empty append blob with specified name. An
// append blob must be created using this method before appending blocks.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx
func (b BlobStorageClient) PutAppendBlob(container, name string, extraHeaders map[string]string) error {
    path := fmt.Sprintf("%s/%s", container, name)
    uri := b.client.getEndpoint(blobServiceName, path, url.Values{})
    headers := b.client.getStandardHeaders()
    headers["x-ms-blob-type"] = string(BlobTypeAppend)

    for k, v := range extraHeaders {
        headers[k] = v
    }

    resp, err := b.client.exec("PUT", uri, headers, nil)
    if err != nil {
        return err
    }
    defer resp.body.Close()

    return checkRespCode(resp.statusCode, []int{http.StatusCreated})
}

// AppendBlock appends a block to an append blob.
//
// See https://msdn.microsoft.com/en-us/library/azure/mt427365.aspx
func (b BlobStorageClient) AppendBlock(container, name string, chunk []byte, extraHeaders map[string]string) error {
    path := fmt.Sprintf("%s/%s", container, name)
    uri := b.client.getEndpoint(blobServiceName, path, url.Values{"comp": {"appendblock"}})
    headers := b.client.getStandardHeaders()
    headers["x-ms-blob-type"] = string(BlobTypeAppend)
    headers["Content-Length"] = fmt.Sprintf("%v", len(chunk))

    for k, v := range extraHeaders {
        headers[k] = v
    }

    resp, err := b.client.exec("PUT", uri, headers, bytes.NewReader(chunk))
    if err != nil {
        return err
    }
    defer resp.body.Close()

    return checkRespCode(resp.statusCode, []int{http.StatusCreated})
}

// CopyBlob starts a blob copy operation and waits for the operation to
// complete. sourceBlob parameter must be a canonical URL to the blob (can be
// obtained using GetBlobURL method.) There is no SLA on blob copy and therefore
// this helper method works faster on smaller files.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd894037.aspx
func (b BlobStorageClient) CopyBlob(container, name, sourceBlob string) error {
    copyID, err := b.startBlobCopy(container, name, sourceBlob)
    if err != nil {
        return err
    }

    return b.waitForBlobCopy(container, name, copyID)
}

func (b BlobStorageClient) startBlobCopy(container, name, sourceBlob string) (string, error) {
    uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{})

    headers := b.client.getStandardHeaders()
    headers["x-ms-copy-source"] = sourceBlob

    resp, err := b.client.exec("PUT", uri, headers, nil)
    if err != nil {
        return "", err
    }
    defer resp.body.Close()

    if err := checkRespCode(resp.statusCode, []int{http.StatusAccepted, http.StatusCreated}); err != nil {
        return "", err
    }

    copyID := resp.headers.Get("x-ms-copy-id")
    if copyID == "" {
        return "", errors.New("Got empty copy id header")
    }
    return copyID, nil
}

func (b BlobStorageClient) waitForBlobCopy(container, name, copyID string) error {
    for {
        props, err := b.GetBlobProperties(container, name)
        if err != nil {
            return err
        }

        if props.CopyID != copyID {
            return errBlobCopyIDMismatch
        }

        switch props.CopyStatus {
        case blobCopyStatusSuccess:
            return nil
        case blobCopyStatusPending:
            continue
        case blobCopyStatusAborted:
            return errBlobCopyAborted
        case blobCopyStatusFailed:
            return fmt.Errorf("storage: blob copy failed. Id=%s Description=%s", props.CopyID, props.CopyStatusDescription)
        default:
            return fmt.Errorf("storage: unhandled blob copy status: '%s'", props.CopyStatus)
        }
    }
}

// DeleteBlob deletes the given blob from the specified container.
// If the blob does not exists at the time of the Delete Blob operation, it
// returns error. See https://msdn.microsoft.com/en-us/library/azure/dd179413.aspx
func (b BlobStorageClient) DeleteBlob(container, name string, extraHeaders map[string]string) error {
    resp, err := b.deleteBlob(container, name, extraHeaders)
    if err != nil {
        return err
    }
    defer resp.body.Close()
    return checkRespCode(resp.statusCode, []int{http.StatusAccepted})
}

// DeleteBlobIfExists deletes the given blob from the specified container If the
// blob is deleted with this call, returns true. Otherwise returns false.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179413.aspx
func (b BlobStorageClient) DeleteBlobIfExists(container, name string, extraHeaders map[string]string) (bool, error) {
    resp, err := b.deleteBlob(container, name, extraHeaders)
    if resp != nil && (resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound) {
        return resp.statusCode == http.StatusAccepted, nil
    }
    defer resp.body.Close()
    return false, err
}

func (b BlobStorageClient) deleteBlob(container, name string, extraHeaders map[string]string) (*storageResponse, error) {
    verb := "DELETE"
    uri := b.client.getEndpoint(blobServiceName, pathForBlob(container, name), url.Values{})
    headers := b.client.getStandardHeaders()
    for k, v := range extraHeaders {
        headers[k] = v
    }

    return b.client.exec(verb, uri, headers, nil)
}

// helper method to construct the path to a container given its name
func pathForContainer(name string) string {
    return fmt.Sprintf("/%s", name)
}

// helper method to construct the path to a blob given its container and blob
// name
func pathForBlob(container, name string) string {
    return fmt.Sprintf("/%s/%s", container, name)
}

// GetBlobSASURI creates an URL to the specified blob which contains the Shared
// Access Signature with specified permissions and expiration time.
//
// See https://msdn.microsoft.com/en-us/library/azure/ee395415.aspx
func (b BlobStorageClient) GetBlobSASURI(container, name string, expiry time.Time, permissions string) (string, error) {
    var (
        signedPermissions = permissions
        blobURL           = b.GetBlobURL(container, name)
    )
    canonicalizedResource, err := b.client.buildCanonicalizedResource(blobURL)

    if err != nil {
        return "", err
    }

    // "The canonicalizedresouce portion of the string is a canonical path to the signed resource.
    // It must include the service name (blob, table, queue or file) for version 2015-02-21 or
    // later, the storage account name, and the resource name, and must be URL-decoded.
    // -- https://msdn.microsoft.com/en-us/library/azure/dn140255.aspx

    // We need to replace + with %2b first to avoid being treated as a space (which is correct for query strings, but not the path component).
    canonicalizedResource = strings.Replace(canonicalizedResource, "+", "%2b", -1)

    canonicalizedResource, err = url.QueryUnescape(canonicalizedResource)
    if err != nil {
        return "", err
    }

    signedExpiry := expiry.UTC().Format(time.RFC3339)
    signedResource := "b"

    stringToSign, err := blobSASStringToSign(b.client.apiVersion, canonicalizedResource, signedExpiry, signedPermissions)
    if err != nil {
        return "", err
    }

    sig := b.client.computeHmac256(stringToSign)
    sasParams := url.Values{
        "sv":  {b.client.apiVersion},
        "se":  {signedExpiry},
        "sr":  {signedResource},
        "sp":  {signedPermissions},
        "sig": {sig},
    }

    sasURL, err := url.Parse(blobURL)
    if err != nil {
        return "", err
    }
    sasURL.RawQuery = sasParams.Encode()
    return sasURL.String(), nil
}

func blobSASStringToSign(signedVersion, canonicalizedResource, signedExpiry, signedPermissions string) (string, error) {
    var signedStart, signedIdentifier, rscc, rscd, rsce, rscl, rsct string

    if signedVersion >= "2015-02-21" {
        canonicalizedResource = "/blob" + canonicalizedResource
    }

    // reference: http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx
    if signedVersion >= "2013-08-15" {
        return fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s", signedPermissions, signedStart, signedExpiry, canonicalizedResource, signedIdentifier, signedVersion, rscc, rscd, rsce, rscl, rsct), nil
    }
    return "", errors.New("storage: not implemented SAS for versions earlier than 2013-08-15")
}