Debian Codegraph @

storage/2018-11-09/blob/blobs/copy.go

raw
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 CopyInput struct {
16 // Specifies the name of the source blob or file.
17 // Beginning with version 2012-02-12, this value may be a URL of up to 2 KB in length that specifies a blob.
18 // The value should be URL-encoded as it would appear in a request URI.
19 // A source blob in the same storage account can be authenticated via Shared Key.
20 // However, if the source is a blob in another account,
21 // the source blob must either be public or must be authenticated via a shared access signature.
22 // If the source blob is public, no authentication is required to perform the copy operation.
23 //
24 // Beginning with version 2015-02-21, the source object may be a file in the Azure File service.
25 // If the source object is a file that is to be copied to a blob, then the source file must be authenticated
26 // using a shared access signature, whether it resides in the same account or in a different account.
27 //
28 // Only storage accounts created on or after June 7th, 2012 allow the Copy Blob operation to
29 // copy from another storage account.
30 CopySource string
31
32 // The ID of the Lease
33 // Required if the destination blob has an active lease.
34 // The lease ID specified for this header must match the lease ID of the destination blob.
35 // If the request does not include the lease ID or it is not valid,
36 // the operation fails with status code 412 (Precondition Failed).
37 //
38 // If this header is specified and the destination blob does not currently have an active lease,
39 // the operation will also fail with status code 412 (Precondition Failed).
40 LeaseID *string
41
42 // The ID of the Lease on the Source Blob
43 // Specify to perform the Copy Blob operation only if the lease ID matches the active lease ID of the source blob.
44 SourceLeaseID *string
45
46 // For page blobs on a premium account only. Specifies the tier to be set on the target blob
47 AccessTier *AccessTier
48
49 // A user-defined name-value pair associated with the blob.
50 // If no name-value pairs are specified, the operation will copy the metadata from the source blob or
51 // file to the destination blob.
52 // If one or more name-value pairs are specified, the destination blob is created with the specified metadata,
53 // and metadata is not copied from the source blob or file.
54 MetaData map[string]string
55
56 // An ETag value.
57 // Specify an ETag value for this conditional header to copy the blob only if the specified
58 // ETag value matches the ETag value for an existing destination blob.
59 // If the ETag for the destination blob does not match the ETag specified for If-Match,
60 // the Blob service returns status code 412 (Precondition Failed).
61 IfMatch *string
62
63 // An ETag value, or the wildcard character (*).
64 // Specify an ETag value for this conditional header to copy the blob only if the specified
65 // ETag value does not match the ETag value for the destination blob.
66 // Specify the wildcard character (*) to perform the operation only if the destination blob does not exist.
67 // If the specified condition isn't met, the Blob service returns status code 412 (Precondition Failed).
68 IfNoneMatch *string
69
70 // A DateTime value.
71 // Specify this conditional header to copy the blob only if the destination blob
72 // has been modified since the specified date/time.
73 // If the destination blob has not been modified, the Blob service returns status code 412 (Precondition Failed).
74 IfModifiedSince *string
75
76 // A DateTime value.
77 // Specify this conditional header to copy the blob only if the destination blob
78 // has not been modified since the specified date/time.
79 // If the destination blob has been modified, the Blob service returns status code 412 (Precondition Failed).
80 IfUnmodifiedSince *string
81
82 // An ETag value.
83 // Specify this conditional header to copy the source blob only if its ETag matches the value specified.
84 // If the ETag values do not match, the Blob service returns status code 412 (Precondition Failed).
85 // This cannot be specified if the source is an Azure File.
86 SourceIfMatch *string
87
88 // An ETag value.
89 // Specify this conditional header to copy the blob only if its ETag does not match the value specified.
90 // If the values are identical, the Blob service returns status code 412 (Precondition Failed).
91 // This cannot be specified if the source is an Azure File.
92 SourceIfNoneMatch *string
93
94 // A DateTime value.
95 // Specify this conditional header to copy the blob only if the source blob has been modified
96 // since the specified date/time.
97 // If the source blob has not been modified, the Blob service returns status code 412 (Precondition Failed).
98 // This cannot be specified if the source is an Azure File.
99 SourceIfModifiedSince *string
100
101 // A DateTime value.
102 // Specify this conditional header to copy the blob only if the source blob has not been modified
103 // since the specified date/time.
104 // If the source blob has been modified, the Blob service returns status code 412 (Precondition Failed).
105 // This header cannot be specified if the source is an Azure File.
106 SourceIfUnmodifiedSince *string
107 }
108
109 type CopyResult struct {
110 autorest.Response
111
112 CopyID string
113 CopyStatus string
114 }
115
116 // Copy copies a blob to a destination within the storage account asynchronously.
117 func (client Client) Copy(ctx context.Context, accountName, containerName, blobName string, input CopyInput) (result CopyResult, err error) {
118 if accountName == "" {
119 return result, validation.NewError("blobs.Client", "Copy", "`accountName` cannot be an empty string.")
120 }
121 if containerName == "" {
122 return result, validation.NewError("blobs.Client", "Copy", "`containerName` cannot be an empty string.")
123 }
124 if strings.ToLower(containerName) != containerName {
125 return result, validation.NewError("blobs.Client", "Copy", "`containerName` must be a lower-cased string.")
126 }
127 if blobName == "" {
128 return result, validation.NewError("blobs.Client", "Copy", "`blobName` cannot be an empty string.")
129 }
130 if input.CopySource == "" {
131 return result, validation.NewError("blobs.Client", "Copy", "`input.CopySource` cannot be an empty string.")
132 }
133
134 req, err := client.CopyPreparer(ctx, accountName, containerName, blobName, input)
135 if err != nil {
136 err = autorest.NewErrorWithError(err, "blobs.Client", "Copy", nil, "Failure preparing request")
137 return
138 }
139
140 resp, err := client.CopySender(req)
141 if err != nil {
142 result.Response = autorest.Response{Response: resp}
143 err = autorest.NewErrorWithError(err, "blobs.Client", "Copy", resp, "Failure sending request")
144 return
145 }
146
147 result, err = client.CopyResponder(resp)
148 if err != nil {
149 err = autorest.NewErrorWithError(err, "blobs.Client", "Copy", resp, "Failure responding to request")
150 return
151 }
152
153 return
154 }
155
156 // CopyPreparer prepares the Copy request.
157 func (client Client) CopyPreparer(ctx context.Context, accountName, containerName, blobName string, input CopyInput) (*http.Request, error) {
158 pathParameters := map[string]interface{}{
159 "containerName": autorest.Encode("path", containerName),
160 "blobName": autorest.Encode("path", blobName),
161 }
162
163 headers := map[string]interface{}{
164 "x-ms-version": APIVersion,
165 "x-ms-copy-source": autorest.Encode("header", input.CopySource),
166 }
167
168 if input.LeaseID != nil {
169 headers["x-ms-lease-id"] = *input.LeaseID
170 }
171 if input.SourceLeaseID != nil {
172 headers["x-ms-source-lease-id"] = *input.SourceLeaseID
173 }
174 if input.AccessTier != nil {
175 headers["x-ms-access-tier"] = string(*input.AccessTier)
176 }
177
178 if input.IfMatch != nil {
179 headers["If-Match"] = *input.IfMatch
180 }
181 if input.IfNoneMatch != nil {
182 headers["If-None-Match"] = *input.IfNoneMatch
183 }
184 if input.IfUnmodifiedSince != nil {
185 headers["If-Unmodified-Since"] = *input.IfUnmodifiedSince
186 }
187 if input.IfModifiedSince != nil {
188 headers["If-Modified-Since"] = *input.IfModifiedSince
189 }
190
191 if input.SourceIfMatch != nil {
192 headers["x-ms-source-if-match"] = *input.SourceIfMatch
193 }
194 if input.SourceIfNoneMatch != nil {
195 headers["x-ms-source-if-none-match"] = *input.SourceIfNoneMatch
196 }
197 if input.SourceIfModifiedSince != nil {
198 headers["x-ms-source-if-modified-since"] = *input.SourceIfModifiedSince
199 }
200 if input.SourceIfUnmodifiedSince != nil {
201 headers["x-ms-source-if-unmodified-since"] = *input.SourceIfUnmodifiedSince
202 }
203
204 headers = metadata.SetIntoHeaders(headers, input.MetaData)
205
206 preparer := autorest.CreatePreparer(
207 autorest.AsPut(),
208 autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)),
209 autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters),
210 autorest.WithHeaders(headers))
211 return preparer.Prepare((&http.Request{}).WithContext(ctx))
212 }
213
214 // CopySender sends the Copy request. The method will close the
215 // http.Response Body if it receives an error.
216 func (client Client) CopySender(req *http.Request) (*http.Response, error) {
217 return autorest.SendWithSender(client, req,
218 azure.DoRetryWithRegistration(client.Client))
219 }
220
221 // CopyResponder handles the response to the Copy request. The method always
222 // closes the http.Response Body.
223 func (client Client) CopyResponder(resp *http.Response) (result CopyResult, err error) {
224 if resp != nil && resp.Header != nil {
225 result.CopyID = resp.Header.Get("x-ms-copy-id")
226 }
227
228 err = autorest.Respond(
229 resp,
230 client.ByInspecting(),
231 azure.WithErrorUnlessStatusCode(http.StatusAccepted),
232 autorest.ByClosing())
233 result.Response = autorest.Response{Response: resp}
234 return
235 }