-
Notifications
You must be signed in to change notification settings - Fork 22
/
put_block.go
119 lines (99 loc) · 2.55 KB
/
put_block.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
package blobs
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/hashicorp/go-azure-sdk/sdk/client"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
)
type PutBlockInput struct {
BlockID string
Content []byte
ContentMD5 *string
LeaseID *string
EncryptionScope *string
}
type PutBlockResponse struct {
HttpResponse *http.Response
ContentMD5 string
}
// PutBlock creates a new block to be committed as part of a blob.
func (c Client) PutBlock(ctx context.Context, containerName, blobName string, input PutBlockInput) (result PutBlockResponse, err error) {
if containerName == "" {
err = fmt.Errorf("`containerName` cannot be an empty string")
return
}
if strings.ToLower(containerName) != containerName {
err = fmt.Errorf("`containerName` must be a lower-cased string")
return
}
if blobName == "" {
err = fmt.Errorf("`blobName` cannot be an empty string")
return
}
if input.BlockID == "" {
err = fmt.Errorf("`input.BlockID` cannot be an empty string")
return
}
if len(input.Content) == 0 {
err = fmt.Errorf("`input.Content` cannot be empty")
return
}
opts := client.RequestOptions{
ExpectedStatusCodes: []int{
http.StatusCreated,
},
HttpMethod: http.MethodPut,
OptionsObject: putBlockOptions{
input: input,
},
Path: fmt.Sprintf("/%s/%s", containerName, blobName),
}
req, err := c.Client.NewRequest(ctx, opts)
if err != nil {
err = fmt.Errorf("building request: %+v", err)
return
}
req.ContentLength = int64(len(input.Content))
req.Body = io.NopCloser(bytes.NewReader(input.Content))
var resp *client.Response
resp, err = req.Execute(ctx)
if resp != nil && resp.Response != nil {
result.HttpResponse = resp.Response
}
if err != nil {
err = fmt.Errorf("executing request: %+v", err)
return
}
return
}
type putBlockOptions struct {
input PutBlockInput
}
func (p putBlockOptions) ToHeaders() *client.Headers {
headers := &client.Headers{}
headers.Append("Content-Length", strconv.Itoa(len(p.input.Content)))
if p.input.ContentMD5 != nil {
headers.Append("x-ms-blob-content-md5", *p.input.ContentMD5)
}
if p.input.LeaseID != nil {
headers.Append("x-ms-lease-id", *p.input.LeaseID)
}
if p.input.EncryptionScope != nil {
headers.Append("x-ms-encryption-scope", *p.input.EncryptionScope)
}
return headers
}
func (p putBlockOptions) ToOData() *odata.Query {
return nil
}
func (p putBlockOptions) ToQuery() *client.QueryParams {
out := &client.QueryParams{}
out.Append("comp", "block")
out.Append("blockid", p.input.BlockID)
return out
}