| 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 PutBlockBlobInput struct { |
| 17 | CacheControl *string |
| 18 | Content *[]byte |
| 19 | ContentDisposition *string |
| 20 | ContentEncoding *string |
| 21 | ContentLanguage *string |
| 22 | ContentMD5 *string |
| 23 | ContentType *string |
| 24 | LeaseID *string |
| 25 | MetaData map[string]string |
| 26 | } |
| 27 | |
| 28 | // PutBlockBlob is a wrapper around the Put API call (with a stricter input object) |
| 29 | // which creates a new block append blob, or updates the content of an existing block blob. |
| 30 | func (client Client) PutBlockBlob(ctx context.Context, accountName, containerName, blobName string, input PutBlockBlobInput) (result autorest.Response, err error) { |
| 31 | if accountName == "" { |
| 32 | return result, validation.NewError("blobs.Client", "PutBlockBlob", "`accountName` cannot be an empty string.") |
| 33 | } |
| 34 | if containerName == "" { |
| 35 | return result, validation.NewError("blobs.Client", "PutBlockBlob", "`containerName` cannot be an empty string.") |
| 36 | } |
| 37 | if strings.ToLower(containerName) != containerName { |
| 38 | return result, validation.NewError("blobs.Client", "PutBlockBlob", "`containerName` must be a lower-cased string.") |
| 39 | } |
| 40 | if blobName == "" { |
| 41 | return result, validation.NewError("blobs.Client", "PutBlockBlob", "`blobName` cannot be an empty string.") |
| 42 | } |
| 43 | if input.Content != nil && len(*input.Content) == 0 { |
| 44 | return result, validation.NewError("blobs.Client", "PutBlockBlob", "`input.Content` must either be nil or not empty.") |
| 45 | } |
| 46 | if err := metadata.Validate(input.MetaData); err != nil { |
| 47 | return result, validation.NewError("blobs.Client", "PutBlockBlob", fmt.Sprintf("`input.MetaData` is not valid: %s.", err)) |
| 48 | } |
| 49 | |
| 50 | req, err := client.PutBlockBlobPreparer(ctx, accountName, containerName, blobName, input) |
| 51 | if err != nil { |
| 52 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutBlockBlob", nil, "Failure preparing request") |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | resp, err := client.PutBlockBlobSender(req) |
| 57 | if err != nil { |
| 58 | result = autorest.Response{Response: resp} |
| 59 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutBlockBlob", resp, "Failure sending request") |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | result, err = client.PutBlockBlobResponder(resp) |
| 64 | if err != nil { |
| 65 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutBlockBlob", resp, "Failure responding to request") |
| 66 | return |
| 67 | } |
| 68 | |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | // PutBlockBlobPreparer prepares the PutBlockBlob request. |
| 73 | func (client Client) PutBlockBlobPreparer(ctx context.Context, accountName, containerName, blobName string, input PutBlockBlobInput) (*http.Request, error) { |
| 74 | pathParameters := map[string]interface{}{ |
| 75 | "containerName": autorest.Encode("path", containerName), |
| 76 | "blobName": autorest.Encode("path", blobName), |
| 77 | } |
| 78 | |
| 79 | headers := map[string]interface{}{ |
| 80 | "x-ms-blob-type": string(BlockBlob), |
| 81 | "x-ms-version": APIVersion, |
| 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 | if input.Content != nil { |
| 106 | headers["Content-Length"] = int(len(*input.Content)) |
| 107 | } |
| 108 | |
| 109 | headers = metadata.SetIntoHeaders(headers, input.MetaData) |
| 110 | |
| 111 | decorators := []autorest.PrepareDecorator{ |
| 112 | autorest.AsPut(), |
| 113 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 114 | autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 115 | autorest.WithHeaders(headers), |
| 116 | } |
| 117 | |
| 118 | if input.Content != nil { |
| 119 | decorators = append(decorators, autorest.WithBytes(input.Content)) |
| 120 | } |
| 121 | |
| 122 | preparer := autorest.CreatePreparer(decorators...) |
| 123 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 124 | } |
| 125 | |
| 126 | // PutBlockBlobSender sends the PutBlockBlob request. The method will close the |
| 127 | // http.Response Body if it receives an error. |
| 128 | func (client Client) PutBlockBlobSender(req *http.Request) (*http.Response, error) { |
| 129 | return autorest.SendWithSender(client, req, |
| 130 | azure.DoRetryWithRegistration(client.Client)) |
| 131 | } |
| 132 | |
| 133 | // PutBlockBlobResponder handles the response to the PutBlockBlob request. The method always |
| 134 | // closes the http.Response Body. |
| 135 | func (client Client) PutBlockBlobResponder(resp *http.Response) (result autorest.Response, err error) { |
| 136 | err = autorest.Respond( |
| 137 | resp, |
| 138 | client.ByInspecting(), |
| 139 | azure.WithErrorUnlessStatusCode(http.StatusCreated), |
| 140 | autorest.ByClosing()) |
| 141 | result = autorest.Response{Response: resp} |
| 142 | |
| 143 | return |
| 144 | } |