-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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(storage): internal uploadData implementation #13888
Merged
AllanZhengYP
merged 5 commits into
aws-amplify:storage-browser/integrity
from
AllanZhengYP:storage-browser/internals-upload
Oct 9, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6b59aca
feat(storage): internal uploadData implementation
AllanZhengYP bcace5b
chore: update bundle size
AllanZhengYP 60bb581
Merge branch 'storage-browser/integrity' into storage-browser/interna…
AllanZhengYP b70235d
fix: address feedbacks
AllanZhengYP c54e21e
Merge remote-tracking branch 'upstream/storage-browser/integrity' int…
AllanZhengYP 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
67 changes: 67 additions & 0 deletions
67
packages/storage/__tests__/internals/apis/uploadData.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,67 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { uploadData as advancedUploadData } from '../../../src/internals'; | ||
import { uploadData as uploadDataInternal } from '../../../src/providers/s3/apis/internal/uploadData'; | ||
|
||
jest.mock('../../../src/providers/s3/apis/internal/uploadData'); | ||
const mockedUploadDataInternal = jest.mocked(uploadDataInternal); | ||
const mockedUploadTask = 'UPLOAD_TASK'; | ||
|
||
describe('uploadData (internal)', () => { | ||
beforeEach(() => { | ||
mockedUploadDataInternal.mockReturnValue(mockedUploadTask as any); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should pass advanced option locationCredentialsProvider to internal remove', 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 contentDisposition = { type: 'attachment', filename: 'foo' } as const; | ||
const onProgress = jest.fn(); | ||
const metadata = { foo: 'bar' }; | ||
|
||
const result = advancedUploadData({ | ||
path: 'input/path/to/mock/object', | ||
data: 'data', | ||
options: { | ||
useAccelerateEndpoint, | ||
bucket, | ||
locationCredentialsProvider, | ||
contentDisposition, | ||
contentEncoding: 'gzip', | ||
contentType: 'text/html', | ||
onProgress, | ||
metadata, | ||
}, | ||
}); | ||
|
||
expect(mockedUploadDataInternal).toHaveBeenCalledTimes(1); | ||
expect(mockedUploadDataInternal).toHaveBeenCalledWith({ | ||
path: 'input/path/to/mock/object', | ||
data: 'data', | ||
options: { | ||
useAccelerateEndpoint, | ||
bucket, | ||
locationCredentialsProvider, | ||
contentDisposition, | ||
contentEncoding: 'gzip', | ||
contentType: 'text/html', | ||
onProgress, | ||
metadata, | ||
}, | ||
}); | ||
expect(result).toEqual(mockedUploadTask); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
packages/storage/__tests__/providers/s3/apis/uploadData/byteLength.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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { UploadDataInput } from '../types/inputs'; | ||
import { UploadDataOutput } from '../types/outputs'; | ||
import { uploadData as uploadDataInternal } from '../../providers/s3/apis/internal/uploadData'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const uploadData = (input: UploadDataInput) => { | ||
const { data, path, options } = input; | ||
|
||
return uploadDataInternal({ | ||
path, | ||
data, | ||
options: { | ||
useAccelerateEndpoint: options?.useAccelerateEndpoint, | ||
bucket: options?.bucket, | ||
onProgress: options?.onProgress, | ||
contentDisposition: options?.contentDisposition, | ||
contentEncoding: options?.contentEncoding, | ||
contentType: options?.contentType, | ||
metadata: options?.metadata, | ||
preventOverwrite: options?.preventOverwrite, | ||
|
||
// Advanced options | ||
locationCredentialsProvider: options?.locationCredentialsProvider, | ||
}, | ||
// Type casting is necessary because `uploadDataInternal` supports both Gen1 and Gen2 signatures, but here | ||
// given in input can only be Gen2 signature, the return can only ben Gen2 signature. | ||
}) as UploadDataOutput; | ||
}; |
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
File renamed without changes.
53 changes: 53 additions & 0 deletions
53
packages/storage/src/providers/s3/apis/internal/uploadData/index.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,53 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { UploadDataInput } from '../../../types'; | ||
// TODO: Remove this interface when we move to public advanced APIs. | ||
import { UploadDataInput as UploadDataWithPathInputWithAdvancedOptions } from '../../../../../internals/types/inputs'; | ||
AllanZhengYP marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { createUploadTask } from '../../../utils'; | ||
import { assertValidationError } from '../../../../../errors/utils/assertValidationError'; | ||
import { StorageValidationErrorCode } from '../../../../../errors/types/validation'; | ||
import { DEFAULT_PART_SIZE, MAX_OBJECT_SIZE } from '../../../utils/constants'; | ||
|
||
import { byteLength } from './byteLength'; | ||
import { putObjectJob } from './putObjectJob'; | ||
import { getMultipartUploadHandlers } from './multipart'; | ||
|
||
export const uploadData = ( | ||
input: UploadDataInput | UploadDataWithPathInputWithAdvancedOptions, | ||
) => { | ||
const { data } = input; | ||
|
||
const dataByteLength = byteLength(data); | ||
assertValidationError( | ||
dataByteLength === undefined || dataByteLength <= MAX_OBJECT_SIZE, | ||
StorageValidationErrorCode.ObjectIsTooLarge, | ||
); | ||
|
||
if (dataByteLength && dataByteLength <= DEFAULT_PART_SIZE) { | ||
// Single part upload | ||
const abortController = new AbortController(); | ||
|
||
return createUploadTask({ | ||
isMultipartUpload: false, | ||
job: putObjectJob(input, abortController.signal, dataByteLength), | ||
onCancel: (message?: string) => { | ||
abortController.abort(message); | ||
}, | ||
}); | ||
} else { | ||
// Multipart upload | ||
const { multipartUploadJob, onPause, onResume, onCancel } = | ||
getMultipartUploadHandlers(input, dataByteLength); | ||
|
||
return createUploadTask({ | ||
isMultipartUpload: true, | ||
job: multipartUploadJob, | ||
onCancel: (message?: string) => { | ||
onCancel(message); | ||
}, | ||
onPause, | ||
onResume, | ||
}); | ||
} | ||
}; |
5 changes: 4 additions & 1 deletion
5
...uploadData/multipart/calculatePartSize.ts → ...uploadData/multipart/calculatePartSize.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
6 changes: 3 additions & 3 deletions
6
...is/uploadData/multipart/getDataChunker.ts → ...al/uploadData/multipart/getDataChunker.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
File renamed without changes.
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.
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.
This change to support the uploadData input which contains additonal
data
properties besidespath
.