| 1 | package blobs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/Azure/go-autorest/autorest" |
| 10 | "github.com/Azure/go-autorest/autorest/azure" |
| 11 | "github.com/Azure/go-autorest/autorest/validation" |
| 12 | "github.com/tombuildsstuff/giovanni/storage/internal/endpoints" |
| 13 | ) |
| 14 | |
| 15 | type GetInput struct { |
| 16 | LeaseID *string |
| 17 | StartByte *int64 |
| 18 | EndByte *int64 |
| 19 | } |
| 20 | |
| 21 | type GetResult struct { |
| 22 | autorest.Response |
| 23 | |
| 24 | Contents []byte |
| 25 | } |
| 26 | |
| 27 | // Get reads or downloads a blob from the system, including its metadata and properties. |
| 28 | func (client Client) Get(ctx context.Context, accountName, containerName, blobName string, input GetInput) (result GetResult, err error) { |
| 29 | if accountName == "" { |
| 30 | return result, validation.NewError("blobs.Client", "Get", "`accountName` cannot be an empty string.") |
| 31 | } |
| 32 | if containerName == "" { |
| 33 | return result, validation.NewError("blobs.Client", "Get", "`containerName` cannot be an empty string.") |
| 34 | } |
| 35 | if strings.ToLower(containerName) != containerName { |
| 36 | return result, validation.NewError("blobs.Client", "Get", "`containerName` must be a lower-cased string.") |
| 37 | } |
| 38 | if blobName == "" { |
| 39 | return result, validation.NewError("blobs.Client", "Get", "`blobName` cannot be an empty string.") |
| 40 | } |
| 41 | if input.LeaseID != nil && *input.LeaseID == "" { |
| 42 | return result, validation.NewError("blobs.Client", "Get", "`input.LeaseID` should either be specified or nil, not an empty string.") |
| 43 | } |
| 44 | if (input.StartByte != nil && input.EndByte == nil) || input.StartByte == nil && input.EndByte != nil { |
| 45 | return result, validation.NewError("blobs.Client", "Get", "`input.StartByte` and `input.EndByte` must both be specified, or both be nil.") |
| 46 | } |
| 47 | |
| 48 | req, err := client.GetPreparer(ctx, accountName, containerName, blobName, input) |
| 49 | if err != nil { |
| 50 | err = autorest.NewErrorWithError(err, "blobs.Client", "Get", nil, "Failure preparing request") |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | resp, err := client.GetSender(req) |
| 55 | if err != nil { |
| 56 | result.Response = autorest.Response{Response: resp} |
| 57 | err = autorest.NewErrorWithError(err, "blobs.Client", "Get", resp, "Failure sending request") |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | result, err = client.GetResponder(resp) |
| 62 | if err != nil { |
| 63 | err = autorest.NewErrorWithError(err, "blobs.Client", "Get", resp, "Failure responding to request") |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | return |
| 68 | } |
| 69 | |
| 70 | // GetPreparer prepares the Get request. |
| 71 | func (client Client) GetPreparer(ctx context.Context, accountName, containerName, blobName string, input GetInput) (*http.Request, error) { |
| 72 | pathParameters := map[string]interface{}{ |
| 73 | "containerName": autorest.Encode("path", containerName), |
| 74 | "blobName": autorest.Encode("path", blobName), |
| 75 | } |
| 76 | |
| 77 | headers := map[string]interface{}{ |
| 78 | "x-ms-version": APIVersion, |
| 79 | } |
| 80 | |
| 81 | if input.StartByte != nil && input.EndByte != nil { |
| 82 | headers["x-ms-range"] = fmt.Sprintf("bytes=%d-%d", *input.StartByte, *input.EndByte) |
| 83 | } |
| 84 | |
| 85 | preparer := autorest.CreatePreparer( |
| 86 | autorest.AsGet(), |
| 87 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 88 | autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 89 | autorest.WithHeaders(headers)) |
| 90 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 91 | } |
| 92 | |
| 93 | // GetSender sends the Get request. The method will close the |
| 94 | // http.Response Body if it receives an error. |
| 95 | func (client Client) GetSender(req *http.Request) (*http.Response, error) { |
| 96 | return autorest.SendWithSender(client, req, |
| 97 | azure.DoRetryWithRegistration(client.Client)) |
| 98 | } |
| 99 | |
| 100 | // GetResponder handles the response to the Get request. The method always |
| 101 | // closes the http.Response Body. |
| 102 | func (client Client) GetResponder(resp *http.Response) (result GetResult, err error) { |
| 103 | if resp != nil && int(resp.ContentLength) > 0 { |
| 104 | result.Contents = make([]byte, resp.ContentLength) |
| 105 | } |
| 106 | |
| 107 | err = autorest.Respond( |
| 108 | resp, |
| 109 | client.ByInspecting(), |
| 110 | azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusPartialContent), |
| 111 | autorest.ByUnmarshallingBytes(&result.Contents), |
| 112 | autorest.ByClosing()) |
| 113 | result.Response = autorest.Response{Response: resp} |
| 114 | |
| 115 | return |
| 116 | } |