| 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 | "github.com/tombuildsstuff/giovanni/storage/internal/metadata" |
| 13 | ) |
| 14 | |
| 15 | type BlockList struct { |
| 16 | CommittedBlockIDs []BlockID `xml:"Committed,omitempty"` |
| 17 | UncommittedBlockIDs []BlockID `xml:"Uncommitted,omitempty"` |
| 18 | LatestBlockIDs []BlockID `xml:"Latest,omitempty"` |
| 19 | } |
| 20 | |
| 21 | type BlockID struct { |
| 22 | Value string `xml:",chardata"` |
| 23 | } |
| 24 | |
| 25 | type PutBlockListInput struct { |
| 26 | BlockList BlockList |
| 27 | CacheControl *string |
| 28 | ContentDisposition *string |
| 29 | ContentEncoding *string |
| 30 | ContentLanguage *string |
| 31 | ContentMD5 *string |
| 32 | ContentType *string |
| 33 | MetaData map[string]string |
| 34 | LeaseID *string |
| 35 | } |
| 36 | |
| 37 | type PutBlockListResult struct { |
| 38 | autorest.Response |
| 39 | |
| 40 | ContentMD5 string |
| 41 | ETag string |
| 42 | LastModified string |
| 43 | } |
| 44 | |
| 45 | // PutBlockList writes a blob by specifying the list of block IDs that make up the blob. |
| 46 | // In order to be written as part of a blob, a block must have been successfully written |
| 47 | // to the server in a prior Put Block operation. |
| 48 | func (client Client) PutBlockList(ctx context.Context, accountName, containerName, blobName string, input PutBlockListInput) (result PutBlockListResult, err error) { |
| 49 | if accountName == "" { |
| 50 | return result, validation.NewError("blobs.Client", "PutBlockList", "`accountName` cannot be an empty string.") |
| 51 | } |
| 52 | if containerName == "" { |
| 53 | return result, validation.NewError("blobs.Client", "PutBlockList", "`containerName` cannot be an empty string.") |
| 54 | } |
| 55 | if strings.ToLower(containerName) != containerName { |
| 56 | return result, validation.NewError("blobs.Client", "PutBlockList", "`containerName` must be a lower-cased string.") |
| 57 | } |
| 58 | if blobName == "" { |
| 59 | return result, validation.NewError("blobs.Client", "PutBlockList", "`blobName` cannot be an empty string.") |
| 60 | } |
| 61 | |
| 62 | req, err := client.PutBlockListPreparer(ctx, accountName, containerName, blobName, input) |
| 63 | if err != nil { |
| 64 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutBlockList", nil, "Failure preparing request") |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | resp, err := client.PutBlockListSender(req) |
| 69 | if err != nil { |
| 70 | result.Response = autorest.Response{Response: resp} |
| 71 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutBlockList", resp, "Failure sending request") |
| 72 | return |
| 73 | } |
| 74 | |
| 75 | result, err = client.PutBlockListResponder(resp) |
| 76 | if err != nil { |
| 77 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutBlockList", resp, "Failure responding to request") |
| 78 | return |
| 79 | } |
| 80 | |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | // PutBlockListPreparer prepares the PutBlockList request. |
| 85 | func (client Client) PutBlockListPreparer(ctx context.Context, accountName, containerName, blobName string, input PutBlockListInput) (*http.Request, error) { |
| 86 | pathParameters := map[string]interface{}{ |
| 87 | "containerName": autorest.Encode("path", containerName), |
| 88 | "blobName": autorest.Encode("path", blobName), |
| 89 | } |
| 90 | |
| 91 | queryParameters := map[string]interface{}{ |
| 92 | "comp": autorest.Encode("query", "blocklist"), |
| 93 | } |
| 94 | |
| 95 | headers := map[string]interface{}{ |
| 96 | "x-ms-version": APIVersion, |
| 97 | } |
| 98 | |
| 99 | if input.CacheControl != nil { |
| 100 | headers["x-ms-blob-cache-control"] = *input.CacheControl |
| 101 | } |
| 102 | if input.ContentDisposition != nil { |
| 103 | headers["x-ms-blob-content-disposition"] = *input.ContentDisposition |
| 104 | } |
| 105 | if input.ContentEncoding != nil { |
| 106 | headers["x-ms-blob-content-encoding"] = *input.ContentEncoding |
| 107 | } |
| 108 | if input.ContentLanguage != nil { |
| 109 | headers["x-ms-blob-content-language"] = *input.ContentLanguage |
| 110 | } |
| 111 | if input.ContentMD5 != nil { |
| 112 | headers["x-ms-blob-content-md5"] = *input.ContentMD5 |
| 113 | } |
| 114 | if input.ContentType != nil { |
| 115 | headers["x-ms-blob-content-type"] = *input.ContentType |
| 116 | } |
| 117 | if input.LeaseID != nil { |
| 118 | headers["x-ms-lease-id"] = *input.LeaseID |
| 119 | } |
| 120 | |
| 121 | headers = metadata.SetIntoHeaders(headers, input.MetaData) |
| 122 | |
| 123 | preparer := autorest.CreatePreparer( |
| 124 | autorest.AsPut(), |
| 125 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 126 | autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 127 | autorest.WithQueryParameters(queryParameters), |
| 128 | autorest.WithHeaders(headers), |
| 129 | autorest.WithXML(input.BlockList)) |
| 130 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 131 | } |
| 132 | |
| 133 | // PutBlockListSender sends the PutBlockList request. The method will close the |
| 134 | // http.Response Body if it receives an error. |
| 135 | func (client Client) PutBlockListSender(req *http.Request) (*http.Response, error) { |
| 136 | return autorest.SendWithSender(client, req, |
| 137 | azure.DoRetryWithRegistration(client.Client)) |
| 138 | } |
| 139 | |
| 140 | // PutBlockListResponder handles the response to the PutBlockList request. The method always |
| 141 | // closes the http.Response Body. |
| 142 | func (client Client) PutBlockListResponder(resp *http.Response) (result PutBlockListResult, err error) { |
| 143 | if resp != nil && resp.Header != nil { |
| 144 | result.ContentMD5 = resp.Header.Get("Content-MD5") |
| 145 | result.ETag = resp.Header.Get("ETag") |
| 146 | result.LastModified = resp.Header.Get("Last-Modified") |
| 147 | } |
| 148 | |
| 149 | err = autorest.Respond( |
| 150 | resp, |
| 151 | client.ByInspecting(), |
| 152 | azure.WithErrorUnlessStatusCode(http.StatusCreated), |
| 153 | autorest.ByClosing()) |
| 154 | result.Response = autorest.Response{Response: resp} |
| 155 | |
| 156 | return |
| 157 | } |