| 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 IncrementalCopyBlobInput struct { |
| 15 | CopySource string |
| 16 | IfModifiedSince *string |
| 17 | IfUnmodifiedSince *string |
| 18 | IfMatch *string |
| 19 | IfNoneMatch *string |
| 20 | } |
| 21 | |
| 22 | // IncrementalCopyBlob copies a snapshot of the source page blob to a destination page blob. |
| 23 | // The snapshot is copied such that only the differential changes between the previously copied |
| 24 | // snapshot are transferred to the destination. |
| 25 | // The copied snapshots are complete copies of the original snapshot and can be read or copied from as usual. |
| 26 | func (client Client) IncrementalCopyBlob(ctx context.Context, accountName, containerName, blobName string, input IncrementalCopyBlobInput) (result autorest.Response, err error) { |
| 27 | if accountName == "" { |
| 28 | return result, validation.NewError("blobs.Client", "IncrementalCopyBlob", "`accountName` cannot be an empty string.") |
| 29 | } |
| 30 | if containerName == "" { |
| 31 | return result, validation.NewError("blobs.Client", "IncrementalCopyBlob", "`containerName` cannot be an empty string.") |
| 32 | } |
| 33 | if strings.ToLower(containerName) != containerName { |
| 34 | return result, validation.NewError("blobs.Client", "IncrementalCopyBlob", "`containerName` must be a lower-cased string.") |
| 35 | } |
| 36 | if blobName == "" { |
| 37 | return result, validation.NewError("blobs.Client", "IncrementalCopyBlob", "`blobName` cannot be an empty string.") |
| 38 | } |
| 39 | if input.CopySource == "" { |
| 40 | return result, validation.NewError("blobs.Client", "IncrementalCopyBlob", "`input.CopySource` cannot be an empty string.") |
| 41 | } |
| 42 | |
| 43 | req, err := client.IncrementalCopyBlobPreparer(ctx, accountName, containerName, blobName, input) |
| 44 | if err != nil { |
| 45 | err = autorest.NewErrorWithError(err, "blobs.Client", "IncrementalCopyBlob", nil, "Failure preparing request") |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | resp, err := client.IncrementalCopyBlobSender(req) |
| 50 | if err != nil { |
| 51 | result = autorest.Response{Response: resp} |
| 52 | err = autorest.NewErrorWithError(err, "blobs.Client", "IncrementalCopyBlob", resp, "Failure sending request") |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | result, err = client.IncrementalCopyBlobResponder(resp) |
| 57 | if err != nil { |
| 58 | err = autorest.NewErrorWithError(err, "blobs.Client", "IncrementalCopyBlob", resp, "Failure responding to request") |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | // IncrementalCopyBlobPreparer prepares the IncrementalCopyBlob request. |
| 66 | func (client Client) IncrementalCopyBlobPreparer(ctx context.Context, accountName, containerName, blobName string, input IncrementalCopyBlobInput) (*http.Request, error) { |
| 67 | pathParameters := map[string]interface{}{ |
| 68 | "containerName": autorest.Encode("path", containerName), |
| 69 | "blobName": autorest.Encode("path", blobName), |
| 70 | } |
| 71 | |
| 72 | queryParameters := map[string]interface{}{ |
| 73 | "comp": autorest.Encode("query", "incrementalcopy"), |
| 74 | } |
| 75 | |
| 76 | headers := map[string]interface{}{ |
| 77 | "x-ms-version": APIVersion, |
| 78 | "x-ms-copy-source": input.CopySource, |
| 79 | } |
| 80 | |
| 81 | if input.IfModifiedSince != nil { |
| 82 | headers["If-Modified-Since"] = *input.IfModifiedSince |
| 83 | } |
| 84 | if input.IfUnmodifiedSince != nil { |
| 85 | headers["If-Unmodified-Since"] = *input.IfUnmodifiedSince |
| 86 | } |
| 87 | if input.IfMatch != nil { |
| 88 | headers["If-Match"] = *input.IfMatch |
| 89 | } |
| 90 | if input.IfNoneMatch != nil { |
| 91 | headers["If-None-Match"] = *input.IfNoneMatch |
| 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 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 101 | } |
| 102 | |
| 103 | // IncrementalCopyBlobSender sends the IncrementalCopyBlob request. The method will close the |
| 104 | // http.Response Body if it receives an error. |
| 105 | func (client Client) IncrementalCopyBlobSender(req *http.Request) (*http.Response, error) { |
| 106 | return autorest.SendWithSender(client, req, |
| 107 | azure.DoRetryWithRegistration(client.Client)) |
| 108 | } |
| 109 | |
| 110 | // IncrementalCopyBlobResponder handles the response to the IncrementalCopyBlob request. The method always |
| 111 | // closes the http.Response Body. |
| 112 | func (client Client) IncrementalCopyBlobResponder(resp *http.Response) (result autorest.Response, err error) { |
| 113 | err = autorest.Respond( |
| 114 | resp, |
| 115 | client.ByInspecting(), |
| 116 | azure.WithErrorUnlessStatusCode(http.StatusAccepted), |
| 117 | autorest.ByClosing()) |
| 118 | result = autorest.Response{Response: resp} |
| 119 | return |
| 120 | } |