-
Notifications
You must be signed in to change notification settings - Fork 373
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
feat: add headers option to MPU #2303
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c40290f
feat: add headers option to MPU
ddelgrosso1 231d1c6
remove proxyquire from transfer manager tests
ddelgrosso1 33da88f
add abort option to MPU
ddelgrosso1 732ef06
turn auto abort on by default and add option to disable
ddelgrosso1 eefa07c
change wording in docs
ddelgrosso1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,22 +91,25 @@ export interface UploadFileInChunksOptions { | |
uploadName?: string; | ||
maxQueueSize?: number; | ||
uploadId?: string; | ||
autoAbortFailure?: boolean; | ||
partsMap?: Map<number, string>; | ||
validation?: 'md5' | false; | ||
headers?: {[key: string]: string}; | ||
} | ||
|
||
export interface MultiPartUploadHelper { | ||
bucket: Bucket; | ||
fileName: string; | ||
uploadId?: string; | ||
partsMap?: Map<number, string>; | ||
initiateUpload(): Promise<void>; | ||
initiateUpload(headers?: {[key: string]: string}): Promise<void>; | ||
uploadPart( | ||
partNumber: number, | ||
chunk: Buffer, | ||
validation?: 'md5' | false | ||
): Promise<void>; | ||
completeUpload(): Promise<GaxiosResponse | undefined>; | ||
abortUpload(): Promise<void>; | ||
} | ||
|
||
export type MultiPartHelperGenerator = ( | ||
|
@@ -185,13 +188,14 @@ class XMLMultiPartUploadHelper implements MultiPartUploadHelper { | |
* | ||
* @returns {Promise<void>} | ||
*/ | ||
async initiateUpload(): Promise<void> { | ||
async initiateUpload(headers: {[key: string]: string} = {}): Promise<void> { | ||
const url = `${this.baseUrl}?uploads`; | ||
return retry(async bail => { | ||
try { | ||
const res = await this.authClient.request({ | ||
method: 'POST', | ||
url, | ||
headers, | ||
}); | ||
if (res.data && res.data.error) { | ||
throw res.data.error; | ||
|
@@ -281,6 +285,30 @@ class XMLMultiPartUploadHelper implements MultiPartUploadHelper { | |
}, this.retryOptions); | ||
} | ||
|
||
/** | ||
* Aborts an multipart upload that is in progress. Once aborted, any parts in the process of being uploaded fail, | ||
* and future requests using the upload ID fail. | ||
* | ||
* @returns {Promise<void>} | ||
*/ | ||
async abortUpload(): Promise<void> { | ||
const url = `${this.baseUrl}?uploadId=${this.uploadId}`; | ||
return retry(async bail => { | ||
try { | ||
const res = await this.authClient.request({ | ||
url, | ||
method: 'DELETE', | ||
}); | ||
if (res.data && res.data.error) { | ||
throw res.data.error; | ||
} | ||
} catch (e) { | ||
this.#handleErrorResponse(e as Error, bail); | ||
return; | ||
} | ||
}, this.retryOptions); | ||
} | ||
|
||
/** | ||
* Handles error responses and calls the bail function if the error should not be retried. | ||
* | ||
|
@@ -611,6 +639,10 @@ export class TransferManager { | |
* @property {string} [uploadId] If specified attempts to resume a previous upload. | ||
* @property {Map} [partsMap] If specified alongside uploadId, attempts to resume a previous upload from the last chunk | ||
* specified in partsMap | ||
* @property {object} [headers] headers to be sent when initiating the multipart upload. | ||
* See {@link https://cloud.google.com/storage/docs/xml-api/post-object-multipart#request_headers| Request Headers: Initiate a Multipart Upload} | ||
* @property {boolean} [autoAbortFailure] boolean to indicate if an in progress upload session will be automatically aborted upon failure. If not set, | ||
* failures will be automatically aborted. | ||
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.
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. Updated, thanks @frankyn |
||
* @experimental | ||
*/ | ||
/** | ||
|
@@ -665,7 +697,7 @@ export class TransferManager { | |
let promises: Promise<void>[] = []; | ||
try { | ||
if (options.uploadId === undefined) { | ||
await mpuHelper.initiateUpload(); | ||
await mpuHelper.initiateUpload(options.headers); | ||
} | ||
const startOrResumptionByte = mpuHelper.partsMap!.size * chunkSize; | ||
const readStream = createReadStream(filePath, { | ||
|
@@ -688,6 +720,21 @@ export class TransferManager { | |
await Promise.all(promises); | ||
return await mpuHelper.completeUpload(); | ||
} catch (e) { | ||
if ( | ||
(options.autoAbortFailure === undefined || options.autoAbortFailure) && | ||
mpuHelper.uploadId | ||
) { | ||
try { | ||
await mpuHelper.abortUpload(); | ||
return; | ||
} catch (e) { | ||
throw new MultiPartUploadError( | ||
(e as Error).message, | ||
mpuHelper.uploadId!, | ||
mpuHelper.partsMap! | ||
); | ||
} | ||
} | ||
throw new MultiPartUploadError( | ||
(e as Error).message, | ||
mpuHelper.uploadId!, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you make this private?
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.
This entire XML implementation is not exported and therefore not accessible.