| 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 PutBlockFromURLInput struct { |
| 15 | BlockID string |
| 16 | CopySource string |
| 17 | |
| 18 | ContentMD5 *string |
| 19 | LeaseID *string |
| 20 | Range *string |
| 21 | } |
| 22 | |
| 23 | type PutBlockFromURLResult struct { |
| 24 | autorest.Response |
| 25 | ContentMD5 string |
| 26 | } |
| 27 | |
| 28 | // PutBlockFromURL creates a new block to be committed as part of a blob where the contents are read from a URL |
| 29 | func (client Client) PutBlockFromURL(ctx context.Context, accountName, containerName, blobName string, input PutBlockFromURLInput) (result PutBlockFromURLResult, err error) { |
| 30 | if accountName == "" { |
| 31 | return result, validation.NewError("blobs.Client", "PutBlockFromURL", "`accountName` cannot be an empty string.") |
| 32 | } |
| 33 | if containerName == "" { |
| 34 | return result, validation.NewError("blobs.Client", "PutBlockFromURL", "`containerName` cannot be an empty string.") |
| 35 | } |
| 36 | if strings.ToLower(containerName) != containerName { |
| 37 | return result, validation.NewError("blobs.Client", "PutBlockFromURL", "`containerName` must be a lower-cased string.") |
| 38 | } |
| 39 | if blobName == "" { |
| 40 | return result, validation.NewError("blobs.Client", "PutBlockFromURL", "`blobName` cannot be an empty string.") |
| 41 | } |
| 42 | if input.BlockID == "" { |
| 43 | return result, validation.NewError("blobs.Client", "PutBlockFromURL", "`input.BlockID` cannot be an empty string.") |
| 44 | } |
| 45 | if input.CopySource == "" { |
| 46 | return result, validation.NewError("blobs.Client", "PutBlockFromURL", "`input.CopySource` cannot be an empty string.") |
| 47 | } |
| 48 | |
| 49 | req, err := client.PutBlockFromURLPreparer(ctx, accountName, containerName, blobName, input) |
| 50 | if err != nil { |
| 51 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutBlockFromURL", nil, "Failure preparing request") |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | resp, err := client.PutBlockFromURLSender(req) |
| 56 | if err != nil { |
| 57 | result.Response = autorest.Response{Response: resp} |
| 58 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutBlockFromURL", resp, "Failure sending request") |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | result, err = client.PutBlockFromURLResponder(resp) |
| 63 | if err != nil { |
| 64 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutBlockFromURL", resp, "Failure responding to request") |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | // PutBlockFromURLPreparer prepares the PutBlockFromURL request. |
| 72 | func (client Client) PutBlockFromURLPreparer(ctx context.Context, accountName, containerName, blobName string, input PutBlockFromURLInput) (*http.Request, error) { |
| 73 | pathParameters := map[string]interface{}{ |
| 74 | "containerName": autorest.Encode("path", containerName), |
| 75 | "blobName": autorest.Encode("path", blobName), |
| 76 | } |
| 77 | |
| 78 | queryParameters := map[string]interface{}{ |
| 79 | "comp": autorest.Encode("query", "block"), |
| 80 | "blockid": autorest.Encode("query", input.BlockID), |
| 81 | } |
| 82 | |
| 83 | headers := map[string]interface{}{ |
| 84 | "x-ms-version": APIVersion, |
| 85 | "x-ms-copy-source": input.CopySource, |
| 86 | } |
| 87 | |
| 88 | if input.ContentMD5 != nil { |
| 89 | headers["x-ms-source-content-md5"] = *input.ContentMD5 |
| 90 | } |
| 91 | if input.LeaseID != nil { |
| 92 | headers["x-ms-lease-id"] = *input.LeaseID |
| 93 | } |
| 94 | if input.Range != nil { |
| 95 | headers["x-ms-source-range"] = *input.Range |
| 96 | } |
| 97 | |
| 98 | preparer := autorest.CreatePreparer( |
| 99 | autorest.AsPut(), |
| 100 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 101 | autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 102 | autorest.WithQueryParameters(queryParameters), |
| 103 | autorest.WithHeaders(headers)) |
| 104 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 105 | } |
| 106 | |
| 107 | // PutBlockFromURLSender sends the PutBlockFromURL request. The method will close the |
| 108 | // http.Response Body if it receives an error. |
| 109 | func (client Client) PutBlockFromURLSender(req *http.Request) (*http.Response, error) { |
| 110 | return autorest.SendWithSender(client, req, |
| 111 | azure.DoRetryWithRegistration(client.Client)) |
| 112 | } |
| 113 | |
| 114 | // PutBlockFromURLResponder handles the response to the PutBlockFromURL request. The method always |
| 115 | // closes the http.Response Body. |
| 116 | func (client Client) PutBlockFromURLResponder(resp *http.Response) (result PutBlockFromURLResult, err error) { |
| 117 | if resp != nil && resp.Header != nil { |
| 118 | result.ContentMD5 = resp.Header.Get("Content-MD5") |
| 119 | } |
| 120 | |
| 121 | err = autorest.Respond( |
| 122 | resp, |
| 123 | client.ByInspecting(), |
| 124 | azure.WithErrorUnlessStatusCode(http.StatusCreated), |
| 125 | autorest.ByClosing()) |
| 126 | result.Response = autorest.Response{Response: resp} |
| 127 | |
| 128 | return |
| 129 | } |