-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage): add new internal downloadData api (#13887)
* feat(internal-remove): add new internal downloadData api * code cleanup * code cleanup * chore: fix ts doc --------- Co-authored-by: Ashwin Kumar <[email protected]>
- Loading branch information
1 parent
20f62a6
commit 7318ba2
Showing
9 changed files
with
243 additions
and
86 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
packages/storage/__tests__/internals/apis/downloadData.test.ts
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { downloadData as advancedDownloadData } from '../../../src/internals'; | ||
import { downloadData as downloadDataInternal } from '../../../src/providers/s3/apis/internal/downloadData'; | ||
|
||
jest.mock('../../../src/providers/s3/apis/internal/downloadData'); | ||
const mockedDownloadDataInternal = jest.mocked(downloadDataInternal); | ||
|
||
describe('downloadData (internal)', () => { | ||
beforeEach(() => { | ||
mockedDownloadDataInternal.mockReturnValue({ | ||
result: Promise.resolve({ | ||
path: 'output/path/to/mock/object', | ||
body: { | ||
blob: () => Promise.resolve(new Blob()), | ||
json: () => Promise.resolve(''), | ||
text: () => Promise.resolve(''), | ||
}, | ||
}), | ||
cancel: jest.fn(), | ||
state: 'SUCCESS', | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should pass advanced option locationCredentialsProvider to internal downloadData', async () => { | ||
const useAccelerateEndpoint = true; | ||
const bucket = { bucketName: 'bucket', region: 'us-east-1' }; | ||
const locationCredentialsProvider = async () => ({ | ||
credentials: { | ||
accessKeyId: 'akid', | ||
secretAccessKey: 'secret', | ||
sessionToken: 'token', | ||
expiration: new Date(), | ||
}, | ||
}); | ||
const onProgress = jest.fn(); | ||
const bytesRange = { start: 1024, end: 2048 }; | ||
|
||
const output = await advancedDownloadData({ | ||
path: 'input/path/to/mock/object', | ||
options: { | ||
useAccelerateEndpoint, | ||
bucket, | ||
locationCredentialsProvider, | ||
onProgress, | ||
bytesRange, | ||
}, | ||
}); | ||
|
||
expect(mockedDownloadDataInternal).toHaveBeenCalledTimes(1); | ||
expect(mockedDownloadDataInternal).toHaveBeenCalledWith({ | ||
path: 'input/path/to/mock/object', | ||
options: { | ||
useAccelerateEndpoint, | ||
bucket, | ||
locationCredentialsProvider, | ||
onProgress, | ||
bytesRange, | ||
}, | ||
}); | ||
|
||
expect(await output.result).toEqual({ | ||
path: 'output/path/to/mock/object', | ||
body: { | ||
blob: expect.any(Function), | ||
json: expect.any(Function), | ||
text: expect.any(Function), | ||
}, | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { downloadData as downloadDataInternal } from '../../providers/s3/apis/internal/downloadData'; | ||
import { DownloadDataInput } from '../types/inputs'; | ||
import { DownloadDataOutput } from '../types/outputs'; | ||
|
||
/** | ||
* Download S3 object data to memory | ||
* | ||
* @param input - The `DownloadDataInput` object. | ||
* @returns A cancelable task exposing result promise from `result` property. | ||
* @throws service: `S3Exception` - thrown when checking for existence of the object | ||
* @throws validation: `StorageValidationErrorCode` - Validation errors | ||
* | ||
* @example | ||
* ```ts | ||
* // Download a file from s3 bucket | ||
* const { body, eTag } = await downloadData({ path, options: { | ||
* onProgress, // Optional progress callback. | ||
* } }).result; | ||
* ``` | ||
* @example | ||
* ```ts | ||
* // Cancel a task | ||
* const downloadTask = downloadData({ path }); | ||
* //... | ||
* downloadTask.cancel(); | ||
* try { | ||
* await downloadTask.result; | ||
* } catch (error) { | ||
* if(isCancelError(error)) { | ||
* // Handle error thrown by task cancelation. | ||
* } | ||
* } | ||
*``` | ||
* | ||
* @internal | ||
*/ | ||
export const downloadData = (input: DownloadDataInput): DownloadDataOutput => | ||
downloadDataInternal({ | ||
path: input.path, | ||
options: { | ||
useAccelerateEndpoint: input?.options?.useAccelerateEndpoint, | ||
bucket: input?.options?.bucket, | ||
locationCredentialsProvider: input?.options?.locationCredentialsProvider, | ||
bytesRange: input?.options?.bytesRange, | ||
onProgress: input?.options?.onProgress, | ||
}, | ||
// Type casting is necessary because `downloadDataInternal` supports both Gen1 and Gen2 signatures, but here | ||
// given in input can only be Gen2 signature, the return can only ben Gen2 signature. | ||
}) as DownloadDataOutput; |
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
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
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
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
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
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
Oops, something went wrong.