| 1 | package blobs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/Azure/go-autorest/autorest" |
| 11 | "github.com/Azure/go-autorest/autorest/azure" |
| 12 | "github.com/Azure/go-autorest/autorest/validation" |
| 13 | "github.com/tombuildsstuff/giovanni/storage/internal/endpoints" |
| 14 | ) |
| 15 | |
| 16 | type AppendBlockInput struct { |
| 17 | |
| 18 | // A number indicating the byte offset to compare. |
| 19 | // Append Block will succeed only if the append position is equal to this number. |
| 20 | // If it is not, the request will fail with an AppendPositionConditionNotMet |
| 21 | // error (HTTP status code 412 – Precondition Failed) |
| 22 | BlobConditionAppendPosition *int64 |
| 23 | |
| 24 | // The max length in bytes permitted for the append blob. |
| 25 | // If the Append Block operation would cause the blob to exceed that limit or if the blob size |
| 26 | // is already greater than the value specified in this header, the request will fail with |
| 27 | // an MaxBlobSizeConditionNotMet error (HTTP status code 412 – Precondition Failed). |
| 28 | BlobConditionMaxSize *int64 |
| 29 | |
| 30 | // The Bytes which should be appended to the end of this Append Blob. |
| 31 | // This can either be nil, which creates an empty blob, or a byte array |
| 32 | Content *[]byte |
| 33 | |
| 34 | // An MD5 hash of the block content. |
| 35 | // This hash is used to verify the integrity of the block during transport. |
| 36 | // When this header is specified, the storage service compares the hash of the content |
| 37 | // that has arrived with this header value. |
| 38 | // |
| 39 | // Note that this MD5 hash is not stored with the blob. |
| 40 | // If the two hashes do not match, the operation will fail with error code 400 (Bad Request). |
| 41 | ContentMD5 *string |
| 42 | |
| 43 | // Required if the blob has an active lease. |
| 44 | // To perform this operation on a blob with an active lease, specify the valid lease ID for this header. |
| 45 | LeaseID *string |
| 46 | } |
| 47 | |
| 48 | type AppendBlockResult struct { |
| 49 | autorest.Response |
| 50 | |
| 51 | BlobAppendOffset string |
| 52 | BlobCommittedBlockCount int64 |
| 53 | ContentMD5 string |
| 54 | ETag string |
| 55 | LastModified string |
| 56 | } |
| 57 | |
| 58 | // AppendBlock commits a new block of data to the end of an existing append blob. |
| 59 | func (client Client) AppendBlock(ctx context.Context, accountName, containerName, blobName string, input AppendBlockInput) (result AppendBlockResult, err error) { |
| 60 | if accountName == "" { |
| 61 | return result, validation.NewError("blobs.Client", "AppendBlock", "`accountName` cannot be an empty string.") |
| 62 | } |
| 63 | if containerName == "" { |
| 64 | return result, validation.NewError("blobs.Client", "AppendBlock", "`containerName` cannot be an empty string.") |
| 65 | } |
| 66 | if strings.ToLower(containerName) != containerName { |
| 67 | return result, validation.NewError("blobs.Client", "AppendBlock", "`containerName` must be a lower-cased string.") |
| 68 | } |
| 69 | if blobName == "" { |
| 70 | return result, validation.NewError("blobs.Client", "AppendBlock", "`blobName` cannot be an empty string.") |
| 71 | } |
| 72 | if input.Content != nil && len(*input.Content) > (4*1024*1024) { |
| 73 | return result, validation.NewError("files.Client", "PutByteRange", "`input.Content` must be at most 4MB.") |
| 74 | } |
| 75 | |
| 76 | req, err := client.AppendBlockPreparer(ctx, accountName, containerName, blobName, input) |
| 77 | if err != nil { |
| 78 | err = autorest.NewErrorWithError(err, "blobs.Client", "AppendBlock", nil, "Failure preparing request") |
| 79 | return |
| 80 | } |
| 81 | |
| 82 | resp, err := client.AppendBlockSender(req) |
| 83 | if err != nil { |
| 84 | result.Response = autorest.Response{Response: resp} |
| 85 | err = autorest.NewErrorWithError(err, "blobs.Client", "AppendBlock", resp, "Failure sending request") |
| 86 | return |
| 87 | } |
| 88 | |
| 89 | result, err = client.AppendBlockResponder(resp) |
| 90 | if err != nil { |
| 91 | err = autorest.NewErrorWithError(err, "blobs.Client", "AppendBlock", resp, "Failure responding to request") |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | return |
| 96 | } |
| 97 | |
| 98 | // AppendBlockPreparer prepares the AppendBlock request. |
| 99 | func (client Client) AppendBlockPreparer(ctx context.Context, accountName, containerName, blobName string, input AppendBlockInput) (*http.Request, error) { |
| 100 | pathParameters := map[string]interface{}{ |
| 101 | "containerName": autorest.Encode("path", containerName), |
| 102 | "blobName": autorest.Encode("path", blobName), |
| 103 | } |
| 104 | |
| 105 | queryParameters := map[string]interface{}{ |
| 106 | "comp": autorest.Encode("query", "appendblock"), |
| 107 | } |
| 108 | |
| 109 | headers := map[string]interface{}{ |
| 110 | "x-ms-version": APIVersion, |
| 111 | } |
| 112 | |
| 113 | if input.BlobConditionAppendPosition != nil { |
| 114 | headers["x-ms-blob-condition-appendpos"] = *input.BlobConditionAppendPosition |
| 115 | } |
| 116 | if input.BlobConditionMaxSize != nil { |
| 117 | headers["x-ms-blob-condition-maxsize"] = *input.BlobConditionMaxSize |
| 118 | } |
| 119 | if input.ContentMD5 != nil { |
| 120 | headers["x-ms-blob-content-md5"] = *input.ContentMD5 |
| 121 | } |
| 122 | if input.LeaseID != nil { |
| 123 | headers["x-ms-lease-id"] = *input.LeaseID |
| 124 | } |
| 125 | if input.Content != nil { |
| 126 | headers["Content-Length"] = int(len(*input.Content)) |
| 127 | } |
| 128 | |
| 129 | decorators := []autorest.PrepareDecorator{ |
| 130 | autorest.AsPut(), |
| 131 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 132 | autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 133 | autorest.WithQueryParameters(queryParameters), |
| 134 | autorest.WithHeaders(headers), |
| 135 | } |
| 136 | |
| 137 | if input.Content != nil { |
| 138 | decorators = append(decorators, autorest.WithBytes(input.Content)) |
| 139 | } |
| 140 | |
| 141 | preparer := autorest.CreatePreparer(decorators...) |
| 142 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 143 | } |
| 144 | |
| 145 | // AppendBlockSender sends the AppendBlock request. The method will close the |
| 146 | // http.Response Body if it receives an error. |
| 147 | func (client Client) AppendBlockSender(req *http.Request) (*http.Response, error) { |
| 148 | return autorest.SendWithSender(client, req, |
| 149 | azure.DoRetryWithRegistration(client.Client)) |
| 150 | } |
| 151 | |
| 152 | // AppendBlockResponder handles the response to the AppendBlock request. The method always |
| 153 | // closes the http.Response Body. |
| 154 | func (client Client) AppendBlockResponder(resp *http.Response) (result AppendBlockResult, err error) { |
| 155 | if resp != nil && resp.Header != nil { |
| 156 | result.BlobAppendOffset = resp.Header.Get("x-ms-blob-append-offset") |
| 157 | result.ContentMD5 = resp.Header.Get("ETag") |
| 158 | result.ETag = resp.Header.Get("ETag") |
| 159 | result.LastModified = resp.Header.Get("Last-Modified") |
| 160 | |
| 161 | if v := resp.Header.Get("x-ms-blob-committed-block-count"); v != "" { |
| 162 | i, innerErr := strconv.Atoi(v) |
| 163 | if innerErr != nil { |
| 164 | err = fmt.Errorf("Error parsing %q as an integer: %s", v, innerErr) |
| 165 | return |
| 166 | } |
| 167 | |
| 168 | result.BlobCommittedBlockCount = int64(i) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | err = autorest.Respond( |
| 173 | resp, |
| 174 | client.ByInspecting(), |
| 175 | azure.WithErrorUnlessStatusCode(http.StatusCreated), |
| 176 | autorest.ByClosing()) |
| 177 | result.Response = autorest.Response{Response: resp} |
| 178 | |
| 179 | return |
| 180 | } |