| 1 | package blobs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/Azure/go-autorest/autorest" |
| 11 | "github.com/Azure/go-autorest/autorest/azure" |
| 12 | "github.com/Azure/go-autorest/autorest/validation" |
| 13 | "github.com/tombuildsstuff/giovanni/storage/internal/endpoints" |
| 14 | ) |
| 15 | |
| 16 | type GetBlockListInput struct { |
| 17 | BlockListType BlockListType |
| 18 | LeaseID *string |
| 19 | } |
| 20 | |
| 21 | type GetBlockListResult struct { |
| 22 | autorest.Response |
| 23 | |
| 24 | // The size of the blob in bytes |
| 25 | ContentLength *int64 |
| 26 | |
| 27 | // The Content Type of the blob |
| 28 | ContentType string |
| 29 | |
| 30 | // The ETag associated with this blob |
| 31 | ETag string |
| 32 | |
| 33 | // A list of blocks which have been committed |
| 34 | CommittedBlocks CommittedBlocks `xml:"CommittedBlocks,omitempty"` |
| 35 | |
| 36 | // A list of blocks which have not yet been committed |
| 37 | UncommittedBlocks UncommittedBlocks `xml:"UncommittedBlocks,omitempty"` |
| 38 | } |
| 39 | |
| 40 | // GetBlockList retrieves the list of blocks that have been uploaded as part of a block blob. |
| 41 | func (client Client) GetBlockList(ctx context.Context, accountName, containerName, blobName string, input GetBlockListInput) (result GetBlockListResult, err error) { |
| 42 | if accountName == "" { |
| 43 | return result, validation.NewError("blobs.Client", "GetBlockList", "`accountName` cannot be an empty string.") |
| 44 | } |
| 45 | if containerName == "" { |
| 46 | return result, validation.NewError("blobs.Client", "GetBlockList", "`containerName` cannot be an empty string.") |
| 47 | } |
| 48 | if strings.ToLower(containerName) != containerName { |
| 49 | return result, validation.NewError("blobs.Client", "GetBlockList", "`containerName` must be a lower-cased string.") |
| 50 | } |
| 51 | if blobName == "" { |
| 52 | return result, validation.NewError("blobs.Client", "GetBlockList", "`blobName` cannot be an empty string.") |
| 53 | } |
| 54 | |
| 55 | req, err := client.GetBlockListPreparer(ctx, accountName, containerName, blobName, input) |
| 56 | if err != nil { |
| 57 | err = autorest.NewErrorWithError(err, "blobs.Client", "GetBlockList", nil, "Failure preparing request") |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | resp, err := client.GetBlockListSender(req) |
| 62 | if err != nil { |
| 63 | result.Response = autorest.Response{Response: resp} |
| 64 | err = autorest.NewErrorWithError(err, "blobs.Client", "GetBlockList", resp, "Failure sending request") |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | result, err = client.GetBlockListResponder(resp) |
| 69 | if err != nil { |
| 70 | err = autorest.NewErrorWithError(err, "blobs.Client", "GetBlockList", resp, "Failure responding to request") |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | // GetBlockListPreparer prepares the GetBlockList request. |
| 78 | func (client Client) GetBlockListPreparer(ctx context.Context, accountName, containerName, blobName string, input GetBlockListInput) (*http.Request, error) { |
| 79 | pathParameters := map[string]interface{}{ |
| 80 | "containerName": autorest.Encode("path", containerName), |
| 81 | "blobName": autorest.Encode("path", blobName), |
| 82 | } |
| 83 | |
| 84 | queryParameters := map[string]interface{}{ |
| 85 | "blocklisttype": autorest.Encode("query", string(input.BlockListType)), |
| 86 | "comp": autorest.Encode("query", "blocklist"), |
| 87 | } |
| 88 | |
| 89 | headers := map[string]interface{}{ |
| 90 | "x-ms-version": APIVersion, |
| 91 | } |
| 92 | |
| 93 | if input.LeaseID != nil { |
| 94 | headers["x-ms-lease-id"] = *input.LeaseID |
| 95 | } |
| 96 | |
| 97 | preparer := autorest.CreatePreparer( |
| 98 | autorest.AsGet(), |
| 99 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 100 | autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 101 | autorest.WithHeaders(headers), |
| 102 | autorest.WithQueryParameters(queryParameters)) |
| 103 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 104 | } |
| 105 | |
| 106 | // GetBlockListSender sends the GetBlockList request. The method will close the |
| 107 | // http.Response Body if it receives an error. |
| 108 | func (client Client) GetBlockListSender(req *http.Request) (*http.Response, error) { |
| 109 | return autorest.SendWithSender(client, req, |
| 110 | azure.DoRetryWithRegistration(client.Client)) |
| 111 | } |
| 112 | |
| 113 | // GetBlockListResponder handles the response to the GetBlockList request. The method always |
| 114 | // closes the http.Response Body. |
| 115 | func (client Client) GetBlockListResponder(resp *http.Response) (result GetBlockListResult, err error) { |
| 116 | if resp != nil && resp.Header != nil { |
| 117 | result.ContentType = resp.Header.Get("Content-Type") |
| 118 | result.ETag = resp.Header.Get("ETag") |
| 119 | |
| 120 | if v := resp.Header.Get("x-ms-blob-content-length"); v != "" { |
| 121 | i, innerErr := strconv.Atoi(v) |
| 122 | if innerErr != nil { |
| 123 | err = fmt.Errorf("Error parsing %q as an integer: %s", v, innerErr) |
| 124 | return |
| 125 | } |
| 126 | |
| 127 | i64 := int64(i) |
| 128 | result.ContentLength = &i64 |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | err = autorest.Respond( |
| 133 | resp, |
| 134 | client.ByInspecting(), |
| 135 | azure.WithErrorUnlessStatusCode(http.StatusOK), |
| 136 | autorest.ByUnmarshallingXML(&result), |
| 137 | autorest.ByClosing()) |
| 138 | result.Response = autorest.Response{Response: resp} |
| 139 | return |
| 140 | } |