| 1 | package blobs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/Azure/go-autorest/autorest" |
| 9 | "github.com/Azure/go-autorest/autorest/azure" |
| 10 | "github.com/Azure/go-autorest/autorest/validation" |
| 11 | "github.com/tombuildsstuff/giovanni/storage/internal/endpoints" |
| 12 | ) |
| 13 | |
| 14 | type PutBlockInput struct { |
| 15 | BlockID string |
| 16 | Content []byte |
| 17 | ContentMD5 *string |
| 18 | LeaseID *string |
| 19 | } |
| 20 | |
| 21 | type PutBlockResult struct { |
| 22 | autorest.Response |
| 23 | |
| 24 | ContentMD5 string |
| 25 | } |
| 26 | |
| 27 | // PutBlock creates a new block to be committed as part of a blob. |
| 28 | func (client Client) PutBlock(ctx context.Context, accountName, containerName, blobName string, input PutBlockInput) (result PutBlockResult, err error) { |
| 29 | if accountName == "" { |
| 30 | return result, validation.NewError("blobs.Client", "PutBlock", "`accountName` cannot be an empty string.") |
| 31 | } |
| 32 | if containerName == "" { |
| 33 | return result, validation.NewError("blobs.Client", "PutBlock", "`containerName` cannot be an empty string.") |
| 34 | } |
| 35 | if strings.ToLower(containerName) != containerName { |
| 36 | return result, validation.NewError("blobs.Client", "PutBlock", "`containerName` must be a lower-cased string.") |
| 37 | } |
| 38 | if blobName == "" { |
| 39 | return result, validation.NewError("blobs.Client", "PutBlock", "`blobName` cannot be an empty string.") |
| 40 | } |
| 41 | if input.BlockID == "" { |
| 42 | return result, validation.NewError("blobs.Client", "PutBlock", "`input.BlockID` cannot be an empty string.") |
| 43 | } |
| 44 | if len(input.Content) == 0 { |
| 45 | return result, validation.NewError("blobs.Client", "PutBlock", "`input.Content` cannot be empty.") |
| 46 | } |
| 47 | |
| 48 | req, err := client.PutBlockPreparer(ctx, accountName, containerName, blobName, input) |
| 49 | if err != nil { |
| 50 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutBlock", nil, "Failure preparing request") |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | resp, err := client.PutBlockSender(req) |
| 55 | if err != nil { |
| 56 | result.Response = autorest.Response{Response: resp} |
| 57 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutBlock", resp, "Failure sending request") |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | result, err = client.PutBlockResponder(resp) |
| 62 | if err != nil { |
| 63 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutBlock", resp, "Failure responding to request") |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | return |
| 68 | } |
| 69 | |
| 70 | // PutBlockPreparer prepares the PutBlock request. |
| 71 | func (client Client) PutBlockPreparer(ctx context.Context, accountName, containerName, blobName string, input PutBlockInput) (*http.Request, error) { |
| 72 | pathParameters := map[string]interface{}{ |
| 73 | "containerName": autorest.Encode("path", containerName), |
| 74 | "blobName": autorest.Encode("path", blobName), |
| 75 | } |
| 76 | |
| 77 | queryParameters := map[string]interface{}{ |
| 78 | "comp": autorest.Encode("query", "block"), |
| 79 | "blockid": autorest.Encode("query", input.BlockID), |
| 80 | } |
| 81 | |
| 82 | headers := map[string]interface{}{ |
| 83 | "x-ms-version": APIVersion, |
| 84 | "Content-Length": int(len(input.Content)), |
| 85 | } |
| 86 | |
| 87 | if input.ContentMD5 != nil { |
| 88 | headers["x-ms-blob-content-md5"] = *input.ContentMD5 |
| 89 | } |
| 90 | if input.LeaseID != nil { |
| 91 | headers["x-ms-lease-id"] = *input.LeaseID |
| 92 | } |
| 93 | |
| 94 | preparer := autorest.CreatePreparer( |
| 95 | autorest.AsPut(), |
| 96 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 97 | autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 98 | autorest.WithQueryParameters(queryParameters), |
| 99 | autorest.WithHeaders(headers), |
| 100 | autorest.WithBytes(&input.Content)) |
| 101 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 102 | } |
| 103 | |
| 104 | // PutBlockSender sends the PutBlock request. The method will close the |
| 105 | // http.Response Body if it receives an error. |
| 106 | func (client Client) PutBlockSender(req *http.Request) (*http.Response, error) { |
| 107 | return autorest.SendWithSender(client, req, |
| 108 | azure.DoRetryWithRegistration(client.Client)) |
| 109 | } |
| 110 | |
| 111 | // PutBlockResponder handles the response to the PutBlock request. The method always |
| 112 | // closes the http.Response Body. |
| 113 | func (client Client) PutBlockResponder(resp *http.Response) (result PutBlockResult, err error) { |
| 114 | if resp != nil && resp.Header != nil { |
| 115 | result.ContentMD5 = resp.Header.Get("Content-MD5") |
| 116 | } |
| 117 | |
| 118 | err = autorest.Respond( |
| 119 | resp, |
| 120 | client.ByInspecting(), |
| 121 | azure.WithErrorUnlessStatusCode(http.StatusCreated), |
| 122 | autorest.ByClosing()) |
| 123 | result.Response = autorest.Response{Response: resp} |
| 124 | |
| 125 | return |
| 126 | } |