| 1 | # Multipart HTTP transfer mode proposal |
| 2 | |
| 3 | This is a proposal for a new transfer mode, designed to support multi-part HTTP uploads. This is a protocol extension to |
| 4 | Git LFS, defining a new transfer mode to be implemented by Git LFS clients and servers in addition to the current `basic` |
| 5 | transfer mode. |
| 6 | |
| 7 | This proposal is based on the experimental `multipart-basic` transfer mode originally |
| 8 | [implemented by datopian/giftless](https://giftless.datopian.com/en/latest/multipart-spec.html). |
| 9 | |
| 10 | ## Reasoning |
| 11 | Many storage vendors and cloud vendors today offer an API to upload files in "parts" or "chunks", using multiple HTTP |
| 12 | requests, allowing improved stability and performance. This is especially handy when files are multiple gigabytes in |
| 13 | size, and a failure during the upload of a file would require re-uploading it, which could be extremely time consuming. |
| 14 | |
| 15 | The purpose of the `multipart` transfer mode is to allow Git LFS servers and client facilitate direct-to-storage |
| 16 | uploads for backends supporting multipart or chunked uploads. |
| 17 | |
| 18 | As the APIs offered by storage vendors differ greatly, `multipart` transfer mode will offer abstraction over most |
| 19 | of these complexities in hope of supporting as many storage vendors as possible. |
| 20 | |
| 21 | ## Terminology |
| 22 | Throughout this document, the following terms are in use: |
| 23 | * *LFS Server* - The HTTP server to which the LFS `batch` request is sent |
| 24 | * *Client* or *LFS Client* - a client using the Git LFS protocol to push large files to storage via an LFS server |
| 25 | * *Storage Backend* - The HTTP server handling actual storage; This may or may not be the same server as the LFS |
| 26 | server, and for the purpose of this document, typically it is not. A typical implementation of this protocol would have |
| 27 | the Storage Backend be a cloud storage service such as *Amazon S3* or *Google Cloud Storage*. |
| 28 | |
| 29 | ## Design Goals |
| 30 | * Abstract vendor specific API and flow into a generic protocol |
| 31 | * Remain as close as possible to the `basic` transfer API |
| 32 | * Work at least with the multi-part APIs of |
| 33 | [Amazon S3](https://aws.amazon.com/s3/), |
| 34 | [Google Cloud Storage](https://cloud.google.com/storage) and |
| 35 | [Azure Blob Storage](https://azure.microsoft.com/en-us/services/storage/blobs/), |
| 36 | * Define how uploads can be resumed by re-doing parts and not-redoing parts that were uploaded successfully |
| 37 | (this may be vendor specific and not always supported) |
| 38 | * Do not require any state to be maintained in the server side |
| 39 | |
| 40 | ## High Level Protocol Specs |
| 41 | * The name of the transfer is `multipart` |
| 42 | * Batch requests are the same as `basic` requests except that `{"transfers": ["multipart", "basic"]}` is the |
| 43 | expected transfers value. Clients MUST retain `basic` as the fallback transfer mode to ensure compatibility with |
| 44 | servers not implementing this extension. |
| 45 | * `{"operation": "download"}` replies work exactly like `basic` download request with no change |
| 46 | * `{"operation": "upload"}` replies will break the upload into several `actions`: |
| 47 | * `parts` (optional), a list of zero or more part upload actions |
| 48 | * `verify` (optional), an action to verify the file is in storage, similar to `basic` upload verify actions |
| 49 | * `abort` (optional), an action to abort the upload and clean up all unfinished chunks and state |
| 50 | * Just like `basic` transfers, if the file fully exists and is committed to storage, no `actions` will be provided |
| 51 | in the reply and the upload can simply be skipped |
| 52 | * If a `verify` action is provided, calling it is required and not optional. In some cases, this endpoint may be used |
| 53 | to finalize the upload. |
| 54 | * An empty or missing list of `parts` with a `verify` action may mean all parts have been uploaded but `verify` still |
| 55 | needs to be called by the client. |
| 56 | * Authentication and authorization behave just like with the `basic` protocol. |
| 57 | |
| 58 | ## Action Objects |
| 59 | Each one of the `parts` and the `verify` and `abort` actions contain instructions for the client on how to send a |
| 60 | request performing a the particular action. These are similar to `basic` transfer adapter `actions` but may include |
| 61 | some common, as well as action specific additional parameters. |
| 62 | |
| 63 | All actions allow `href`, `header` and `expires_in` parameters just like `basic` transfer actions. |
| 64 | |
| 65 | ### `parts` actions |
| 66 | Each `parts` action should include the `pos` and `size` attributes, in addition to the attributes specified |
| 67 | above: |
| 68 | * `pos` indicate the position in bytes within the file in which the part should begin. If not specified, `0` (that is the |
| 69 | beginning of the file) is assumed. |
| 70 | * `size` is the size of the part in bytes. If `size` is omitted, default to read until the end of file. |
| 71 | * If both `pos` and `size` are omitted, the action is expected to be a single-part upload of the entire file |
| 72 | |
| 73 | In addition, `parts` actions may include the following parameters: |
| 74 | * `method`, with `PUT` as the default method if none is specified. This allows customizing the HTTP method used when uploading |
| 75 | object parts. |
| 76 | * `want_digest` to specify an expected HTTP `Digest` header, as described below. |
| 77 | |
| 78 | ### `verify` action |
| 79 | The `verify` action is similar to `basic` transfer mode `verify`, with the following additional parameters: |
| 80 | * `params` - an object with additional parameters to send to the server when sending the `verify` request. |
| 81 | These parameters are to be sent to the server exactly as provided, as the value of the `params` JSON attribute. |
| 82 | |
| 83 | ### `abort` action |
| 84 | The `abort` action may include the `method` attribute as specified for `parts` actions above. |
| 85 | |
| 86 | ## Batch Request / Response Examples |
| 87 | |
| 88 | ### Upload Batch Request |
| 89 | The following is a ~10mb file upload request: |
| 90 | ```json |
| 91 | { |
| 92 | "transfers": ["multipart", "basic"], |
| 93 | "operation": "upload", |
| 94 | "objects": [ |
| 95 | { |
| 96 | "oid": "20492a4d0d84f8beb1767f6616229f85d44c2827b64bdbfb260ee12fa1109e0e", |
| 97 | "size": 10000000 |
| 98 | } |
| 99 | ] |
| 100 | } |
| 101 | ``` |
| 102 | |
| 103 | ### Upload Batch Response |
| 104 | The following is a response for the same request, given an imaginary storage backend: |
| 105 | |
| 106 | ```json |
| 107 | { |
| 108 | "transfer": "multipart", |
| 109 | "objects": [ |
| 110 | { |
| 111 | "oid": "20492a4d0d84f8beb1767f6616229f85d44c2827b64bdbfb260ee12fa1109e0e", |
| 112 | "size": 10000000, |
| 113 | "actions": { |
| 114 | "parts": [ |
| 115 | { |
| 116 | "href": "https://storage.cloud.example/storage/upload/20492a4d0d84?part=0", |
| 117 | "header": { |
| 118 | "Authorization": "Bearer someauthorizationtokenwillbesethere" |
| 119 | }, |
| 120 | "pos": 0, |
| 121 | "size": 2500000, |
| 122 | "expires_in": 86400 |
| 123 | }, |
| 124 | { |
| 125 | "href": "https://storage.cloud.example/storage/upload/20492a4d0d84?part=1", |
| 126 | "header": { |
| 127 | "Authorization": "Bearer someauthorizationtokenwillbesethere" |
| 128 | }, |
| 129 | "pos": 2500000, |
| 130 | "size": 2500000, |
| 131 | "expires_in": 86400 |
| 132 | }, |
| 133 | { |
| 134 | "href": "https://storage.cloud.example/storage/upload/20492a4d0d84?part=2", |
| 135 | "header": { |
| 136 | "Authorization": "Bearer someauthorizationtokenwillbesethere" |
| 137 | }, |
| 138 | "pos": 5000000, |
| 139 | "size": 2500000, |
| 140 | "expires_in": 86400 |
| 141 | }, |
| 142 | { |
| 143 | "href": "https://storage.cloud.example/storage/upload/20492a4d0d84?part=3", |
| 144 | "header": { |
| 145 | "Authorization": "Bearer someauthorizationtokenwillbesethere" |
| 146 | }, |
| 147 | "pos": 7500000, |
| 148 | "expires_in": 86400 |
| 149 | } |
| 150 | ], |
| 151 | "verify": { |
| 152 | "href": "https://lfs.mycompany.example/myorg/myrepo/multipart/verify", |
| 153 | "authenticated": true, |
| 154 | "header": { |
| 155 | "Authorization": "Basic 123abc123abc123abc123abc123=" |
| 156 | }, |
| 157 | "expires_in": 86400, |
| 158 | "params": { |
| 159 | "uploadId": "20492a4d0d84", |
| 160 | "partIds": [0, 1, 2, 3] |
| 161 | } |
| 162 | }, |
| 163 | "abort": { |
| 164 | "href": "https://storage.cloud.example/storage/upload/20492a4d0d84", |
| 165 | "authenticated": true, |
| 166 | "header": { |
| 167 | "Authorization": "Basic 123abc123abc123abc123abc123=" |
| 168 | }, |
| 169 | "method": "DELETE", |
| 170 | "expires_in": 86400 |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | ] |
| 175 | } |
| 176 | ``` |
| 177 | |
| 178 | ### `verify` request example |
| 179 | Given the `batch` response above, after all parts have been uploaded the client should send the following `verify` |
| 180 | request to `https://lfs.mycompany.example/myorg/myrepo/multipart/verify`: |
| 181 | |
| 182 | ``` |
| 183 | POST /myorg/myrepo/multipart/verify |
| 184 | Host: lfs.mycompany.example |
| 185 | Authorization: Basic 123abc123abc123abc123abc123= |
| 186 | Content-type: application/json |
| 187 | |
| 188 | { |
| 189 | "oid": "20492a4d0d84f8beb1767f6616229f85d44c2827b64bdbfb260ee12fa1109e0e", |
| 190 | "size": 10000000, |
| 191 | "params": { |
| 192 | "uploadId": "20492a4d0d84", |
| 193 | "partIds": [0, 1, 2, 3] |
| 194 | } |
| 195 | } |
| 196 | ``` |
| 197 | |
| 198 | Assuming that all parts were uploaded successfully, the server should respond with a `200 OK` response. |
| 199 | |
| 200 | ### `abort` request example |
| 201 | Given the `batch` response above, the client may choose to cancel the upload by sending the following |
| 202 | `abort` request to `https://storage.cloud.example/storage/upload/20492a4d0d84`: |
| 203 | |
| 204 | ``` |
| 205 | > DELETE /storage/upload/20492a4d0d84 |
| 206 | > Host: storage.cloud.example |
| 207 | > Content-length: 0 |
| 208 | ``` |
| 209 | |
| 210 | ## Uploaded Part Digest |
| 211 | Some storage backends will support, or even require, uploading clients to send a digest of the uploaded part when |
| 212 | uploading the part. This is a useful capability even if not required, as it allows backends to validate each part |
| 213 | separately as it is uploaded. |
| 214 | |
| 215 | To support this, `parts` request objects may include a `want_digest` value, which is expected to be a list of digest |
| 216 | algorithms in the same format of the `Want-Digest` HTTP header specified by [RFC-3230](https://tools.ietf.org/html/rfc3230). |
| 217 | |
| 218 | Any cryptographically secure digest algorithm [registered with IANA](https://www.iana.org/assignments/http-dig-alg/http-dig-alg.xhtml) |
| 219 | via the process outlined in [RFC-3230](https://tools.ietf.org/html/rfc3230) may be specified in `want_digest`. Algorithms |
| 220 | considered cryptographically insecure, including `MD5` and `SHA-1`, should not be accepted. Namely, `contentMD5` is |
| 221 | **not** an accepted value of `want_digest`. |
| 222 | |
| 223 | If one or more digest algorithms with non-zero q-value is specified in `want_digest`, clients *should* select a favored |
| 224 | supported algorithm, calculate the part digest using that algorithm, and send it when uploading the part using the `Digest` HTTP |
| 225 | header as specified by [RFC-3230 section 4.3.1](https://tools.ietf.org/html/rfc3230#section-4.3.1). |
| 226 | |
| 227 | While clients may include the part digest calculated using more than one algorithm, this is typically not required and |
| 228 | should be avoided. |
| 229 | |
| 230 | Note that if `want_digest` is specified but the client cannot support any of the requested algorithms, the client may |
| 231 | still choose to continue uploading parts without sending a `Digest` header. However, the storage server may choose |
| 232 | to reject the request in such cases. |
| 233 | |
| 234 | ### Uploaded Part Digest Example |
| 235 | |
| 236 | #### Examples of a batch response with `want_digest` in the reply |
| 237 | |
| 238 | With SHA-512 as a preferred algorithm, and SHA-256 as a less preferred option if SHA-512 is not possible: |
| 239 | |
| 240 | ```json |
| 241 | { |
| 242 | "actions": { |
| 243 | "parts": [ |
| 244 | { |
| 245 | "href": "https://storage.cloud.example/storage/upload/20492a4d0d84?part=3", |
| 246 | "header": { |
| 247 | "Authorization": "Bearer someauthorizationtokenwillbesethere" |
| 248 | }, |
| 249 | "pos": 7500001, |
| 250 | "want_digest": "sha-512;q=1.0, sha-256;q=0.5" |
| 251 | } |
| 252 | ] |
| 253 | } |
| 254 | } |
| 255 | ``` |
| 256 | |
| 257 | #### Example of part upload request send to the storage server |
| 258 | Following on the `want_digest` value specified in the last example, the client should now send the following headers |
| 259 | to the storage server when uploading the part, assuming `SHA-512` is supported: |
| 260 | |
| 261 | ``` |
| 262 | HTTP/1.1 PUT /storage/upload/20492a4d0d84?part=3 |
| 263 | Authorization: Bearer someauthorizationtokenwillbesethere |
| 264 | Digest: SHA-512=thvDyvhfIqlvFe+A9MYgxAfm1q5thvDyvhfIqlvFe+A9MYgxAfm1q5= |
| 265 | ``` |
| 266 | |
| 267 | ## Expected HTTP Responses |
| 268 | |
| 269 | For each one of the `parts`, as well as `abort` and `verify` requests sent by the client, the following responses are |
| 270 | to be expected: |
| 271 | |
| 272 | * Any response with a `20x` status code is to be considered by clients as successful. This ambiguity is by design, to |
| 273 | support variances between vendors (which may use `200` or `201` to indicate a successful upload, for example). |
| 274 | |
| 275 | * Any other response is to be considered as an error, and it is up to the client to decide whether the request should |
| 276 | be retried or not. Implementors are encouraged to follow standard HTTP error status code guidelines. |
| 277 | |
| 278 | ### `batch` replies for partially uploaded content |
| 279 | When content was already partially uploaded, the server is expected to return a normal reply but omit request and parts |
| 280 | which do not need to be repeated. If the entire file has been uploaded, it is expected that no `actions` value will be |
| 281 | returned, in which case clients should simply skip the upload. |
| 282 | |
| 283 | However, if parts of the file were successfully uploaded while others weren't, it is expected that a normal reply would |
| 284 | be returned, but with less `parts` to send. |
| 285 | |
| 286 | ### `verify` HTTP 409 errors |
| 287 | An `HTTP 409` error on `verify` requests typically indicates that the file could not be fully committed or verified. |
| 288 | In this case, clients should follow the following process to try and recover from the error: |
| 289 | * retry the `batch` request to see if any parts of the file were not uploaded yet. If there are still `parts` to upload |
| 290 | (i.e. `parts` is not empty), proceed to upload them and re-do `verify` |
| 291 | * If `parts` is empty, it is possible that the file exists in storage but is corrupt / has wrong size. In this case |
| 292 | it is recommended to issue an `abort` and re-attempt the same upload again |
| 293 | * It is recommended to take special note of the number of retries, to avoid infinite recovery attempt loops |
| 294 | |
| 295 | ## Additional Considerations |
| 296 | |
| 297 | ### Chunk sizing |
| 298 | It is up to the LFS server to decide the size of each file chunk. |
| 299 | |
| 300 | ### Action lifetime considerations |
| 301 | As multipart uploads tend to require much more time than simple uploads, it is recommended to allow for longer `expires_in` |
| 302 | values than one would consider for `basic` uploads. It is possible that the process of uploading a single object in multiple |
| 303 | parts may take several hours from `batch` to `verify`. |
| 304 | |
| 305 | ### Falling back to `basic` transfer for small files |
| 306 | Using multipart upload APIs has some complexity and speed overhead. For this reason, if a client specifies support for |
| 307 | both `multipart` and `basic` transfer modes in a batch request, and the object(s) uploaded are small enough to fit in a |
| 308 | single part upload, servers *may* choose to respond with a `basic` transfer mode even if `multipart` is supported: |
| 309 | |
| 310 | For example a small (2mb) upload batch request: |
| 311 | ``` |
| 312 | { |
| 313 | "transfers": ["multipart", "basic"], |
| 314 | "operation": "upload", |
| 315 | "objects": [ |
| 316 | { |
| 317 | "oid": "13aea96040f2133033d103008d5d96cfe98b3361f7202d77bea97b2424a7a6cd", |
| 318 | "size": 2000000 |
| 319 | } |
| 320 | ] |
| 321 | } |
| 322 | ``` |
| 323 | |
| 324 | May be responded with: |
| 325 | ``` |
| 326 | { |
| 327 | "transfer": "basic", |
| 328 | "objects": [ |
| 329 | ... |
| 330 | ] |
| 331 | } |
| 332 | ``` |
| 333 | Even if the server does support `multipart`, as `basic` can be preferable in this case. |
| 334 | |
| 335 | ## Implementation Notes |
| 336 | |
| 337 | ### Hiding initialization / commit complexities from clients |
| 338 | While `part` requests are typically quite similar between vendors, the specifics of multipart upload initialization and |
| 339 | commit procedures are very specific to vendors. For this reason, in many cases, it will be up to the LFS server to |
| 340 | take care of initialization and commit code. This is fine, as long as actual uploaded data is sent directly to the |
| 341 | storage backend. |
| 342 | |
| 343 | For example, in the case of Amazon S3: |
| 344 | * All requests need to have an "upload ID" token which is obtained in an initial request |
| 345 | * When finalizing the upload, a special "commit" request need to be sent, listing all uploaded part IDs. |
| 346 | |
| 347 | These are very hard to abstract in a way that would allow clients to send them directly to the server. In addition, as |
| 348 | we do not want to maintain any state in the server, there is a need to make two requests when finalizing the upload: |
| 349 | one to fetch a list of uploaded chunks, and another to send this list to the S3 finalization endpoint. |
| 350 | |
| 351 | For this reason, it is expected that any initialization actions will be handled by the Git LFS server during the |
| 352 | `batch` request handling. In most cases, the `verify` action will also be responsible for any finalization / commit actions. |
| 353 | The `params` attribute of the `verify` action is designed specifically to transfer some vendor-specific "state" between |
| 354 | initialization and finalization of the upload process. |
| 355 | |
| 356 | ### Implementing Complex `abort` actions |
| 357 | Some storage backends will accept a simple `DELETE` or `POST` request to a URL, with no request body, in order to abort |
| 358 | the upload. In such cases, `abort` may refer directly to the storage backend. However, in cases where aborting the upload |
| 359 | requires more complex logic or some payload in the request body, `abort` actions should point to an endpoint of the LFS |
| 360 | server, and it should be up to the LFS server to abort the upload and clean up any partially uploaded parts. |
| 361 | |
| 362 | As `abort` requests do not have a body, any parameters required by the LFS server in order to complete the request should |
| 363 | be passed as part of the URL in the `href` parameter. |
| 364 | |
| 365 | It should be noted that clients will not always be able to `abort` partial uploads cleanly. Implementors are expected to |
| 366 | ensure proper cleanup of partially uploaded files via other means, such as a periodical cron job that locates uncommitted |
| 367 | uploaded parts and deletes them. |