-
Notifications
You must be signed in to change notification settings - Fork 712
feat(gax): support resumable uploads #9287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -149,6 +149,48 @@ in the second parameter: | |
| const [response] = await client.sampleMethod(request, options); | ||
| ``` | ||
|
|
||
| ### Resumable uploads | ||
|
|
||
| Some APIs expose methods that upload large payloads through the resumable | ||
| upload protocol. For these methods, the client method no | ||
| longer returns the response directly; it returns a | ||
| [`ResumableUploadSession`](https://googleapis.dev/nodejs/google-gax/latest/classes/ResumableUploadSession.html) | ||
| helper. Call `start()` with a `ResumableSource` (see `getResumableSource()` | ||
| below) and await `finished()` for the final response: | ||
|
|
||
| ```ts | ||
| const helper = await client.createResumableUpload(request); | ||
| await helper.start({ | ||
| uploadSource: client.getResumableSource(filePath), | ||
| chunkSize: 8 * 1024 * 1024, // 8MB chunks | ||
| onProgress: status => { | ||
| console.log(`Committed ${status.bytesUploaded} bytes to ${status.uploadUrl}`); | ||
| }, | ||
| }); | ||
| const response = await helper.finished(); | ||
| ``` | ||
|
|
||
| The session URL is available as `helper.uploadUrl` once the upload has | ||
| started. Save it if you need to resume the upload later — for example after a | ||
| process crash or network drop. To resume, pass the saved URL to `start()` on a | ||
| new helper, along with a new source for the same payload: | ||
|
|
||
| ```ts | ||
| const helper = await client.createResumableUpload(); | ||
| await helper.start({ | ||
| uploadSource: client.getResumableSource(filePath), | ||
| resumeUrl: savedUploadUrl, | ||
| }); | ||
|
Comment on lines
+180
to
+183
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not manually edit auto-generated markdown files to make changes, as these edits will be overwritten during the next regeneration. Instead, apply the fix upstream in the generator or templates so that the example correctly uses References
|
||
| const response = await helper.finished(); | ||
| ``` | ||
|
|
||
| The current implementation requires a seekable stream (for example, a file | ||
| stream). Errors fall into three categories: transient errors (retried with | ||
| exponential backoff), state mismatches (recovered by querying the server for | ||
| the committed byte offset), and fatal errors (propagated to the caller). | ||
| The whole session is bounded by a global deadline (10 minutes by default, | ||
| scaled up for large payloads and overridable via `globalDeadlineMs`). | ||
|
|
||
| ### Long-running operations | ||
|
|
||
| Some methods are expected to run longer. They return an object of type | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /** | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import {createReadStream, statSync} from 'fs'; | ||
|
|
||
| import {ResumableSource} from './resumableUpload'; | ||
|
|
||
| /** | ||
| * Creates a {@link ResumableSource} backed by a local file. | ||
| * | ||
| * This factory lives outside the fallback transport entrypoint so browser | ||
| * builds do not pull in the Node.js `fs` module. Generated clients should | ||
| * delegate to this function rather than constructing file streams directly. | ||
| */ | ||
| export function resumableSourceFromFile(filePath: string): ResumableSource { | ||
| const stat = statSync(filePath); | ||
| return { | ||
| size: stat.size, | ||
| getStream: (offset?: number) => { | ||
| const start = offset ?? 0; | ||
| if (start < 0 || start > stat.size) { | ||
| throw new RangeError( | ||
| `Invalid start offset ${start} for file of size ${stat.size}.`, | ||
| ); | ||
| } | ||
| return createReadStream(filePath, {start}); | ||
| }, | ||
| }; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not manually edit auto-generated markdown files to make changes, as these edits will be overwritten during the next regeneration. Instead, apply the fix upstream in the generator or templates so that the example correctly uses
uploadSource(of typeResumableSource) rather thanuploadStreamto avoid runtime errors.References