| 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 | "github.com/tombuildsstuff/giovanni/storage/internal/metadata" |
| 14 | ) |
| 15 | |
| 16 | type PutAppendBlobInput struct { |
| 17 | CacheControl *string |
| 18 | ContentDisposition *string |
| 19 | ContentEncoding *string |
| 20 | ContentLanguage *string |
| 21 | ContentMD5 *string |
| 22 | ContentType *string |
| 23 | LeaseID *string |
| 24 | MetaData map[string]string |
| 25 | } |
| 26 | |
| 27 | // PutAppendBlob is a wrapper around the Put API call (with a stricter input object) |
| 28 | // which creates a new append blob, or updates the content of an existing blob. |
| 29 | func (client Client) PutAppendBlob(ctx context.Context, accountName, containerName, blobName string, input PutAppendBlobInput) (result autorest.Response, err error) { |
| 30 | if accountName == "" { |
| 31 | return result, validation.NewError("blobs.Client", "PutAppendBlob", "`accountName` cannot be an empty string.") |
| 32 | } |
| 33 | if containerName == "" { |
| 34 | return result, validation.NewError("blobs.Client", "PutAppendBlob", "`containerName` cannot be an empty string.") |
| 35 | } |
| 36 | if strings.ToLower(containerName) != containerName { |
| 37 | return result, validation.NewError("blobs.Client", "PutAppendBlob", "`containerName` must be a lower-cased string.") |
| 38 | } |
| 39 | if blobName == "" { |
| 40 | return result, validation.NewError("blobs.Client", "PutAppendBlob", "`blobName` cannot be an empty string.") |
| 41 | } |
| 42 | if err := metadata.Validate(input.MetaData); err != nil { |
| 43 | return result, validation.NewError("blobs.Client", "PutAppendBlob", fmt.Sprintf("`input.MetaData` is not valid: %s.", err)) |
| 44 | } |
| 45 | |
| 46 | req, err := client.PutAppendBlobPreparer(ctx, accountName, containerName, blobName, input) |
| 47 | if err != nil { |
| 48 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutAppendBlob", nil, "Failure preparing request") |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | resp, err := client.PutAppendBlobSender(req) |
| 53 | if err != nil { |
| 54 | result = autorest.Response{Response: resp} |
| 55 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutAppendBlob", resp, "Failure sending request") |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | result, err = client.PutAppendBlobResponder(resp) |
| 60 | if err != nil { |
| 61 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutAppendBlob", resp, "Failure responding to request") |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | // PutAppendBlobPreparer prepares the PutAppendBlob request. |
| 69 | func (client Client) PutAppendBlobPreparer(ctx context.Context, accountName, containerName, blobName string, input PutAppendBlobInput) (*http.Request, error) { |
| 70 | pathParameters := map[string]interface{}{ |
| 71 | "containerName": autorest.Encode("path", containerName), |
| 72 | "blobName": autorest.Encode("path", blobName), |
| 73 | } |
| 74 | |
| 75 | headers := map[string]interface{}{ |
| 76 | "x-ms-blob-type": string(AppendBlob), |
| 77 | "x-ms-version": APIVersion, |
| 78 | |
| 79 | // For a page blob or an append blob, the value of this header must be set to zero, |
| 80 | // as Put Blob is used only to initialize the blob |
| 81 | "Content-Length": 0, |
| 82 | } |
| 83 | |
| 84 | if input.CacheControl != nil { |
| 85 | headers["x-ms-blob-cache-control"] = *input.CacheControl |
| 86 | } |
| 87 | if input.ContentDisposition != nil { |
| 88 | headers["x-ms-blob-content-disposition"] = *input.ContentDisposition |
| 89 | } |
| 90 | if input.ContentEncoding != nil { |
| 91 | headers["x-ms-blob-content-encoding"] = *input.ContentEncoding |
| 92 | } |
| 93 | if input.ContentLanguage != nil { |
| 94 | headers["x-ms-blob-content-language"] = *input.ContentLanguage |
| 95 | } |
| 96 | if input.ContentMD5 != nil { |
| 97 | headers["x-ms-blob-content-md5"] = *input.ContentMD5 |
| 98 | } |
| 99 | if input.ContentType != nil { |
| 100 | headers["x-ms-blob-content-type"] = *input.ContentType |
| 101 | } |
| 102 | if input.LeaseID != nil { |
| 103 | headers["x-ms-lease-id"] = *input.LeaseID |
| 104 | } |
| 105 | |
| 106 | headers = metadata.SetIntoHeaders(headers, input.MetaData) |
| 107 | |
| 108 | preparer := autorest.CreatePreparer( |
| 109 | autorest.AsPut(), |
| 110 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 111 | autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 112 | autorest.WithHeaders(headers)) |
| 113 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 114 | } |
| 115 | |
| 116 | // PutAppendBlobSender sends the PutAppendBlob request. The method will close the |
| 117 | // http.Response Body if it receives an error. |
| 118 | func (client Client) PutAppendBlobSender(req *http.Request) (*http.Response, error) { |
| 119 | return autorest.SendWithSender(client, req, |
| 120 | azure.DoRetryWithRegistration(client.Client)) |
| 121 | } |
| 122 | |
| 123 | // PutAppendBlobResponder handles the response to the PutAppendBlob request. The method always |
| 124 | // closes the http.Response Body. |
| 125 | func (client Client) PutAppendBlobResponder(resp *http.Response) (result autorest.Response, err error) { |
| 126 | err = autorest.Respond( |
| 127 | resp, |
| 128 | client.ByInspecting(), |
| 129 | azure.WithErrorUnlessStatusCode(http.StatusCreated), |
| 130 | autorest.ByClosing()) |
| 131 | result = autorest.Response{Response: resp} |
| 132 | |
| 133 | return |
| 134 | } |