Skip to content

Commit d01b650

Browse files
authored
feat(storage): add server uploadData storage api
* feat(storage): add server uploadData api
1 parent aa831db commit d01b650

21 files changed

Lines changed: 423 additions & 51 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@aws-amplify/storage': minor
3+
'aws-amplify': minor
4+
---
5+
6+
feat(storage): add server-side `uploadData` storage API.

packages/storage/__tests__/internals/apis/uploadData.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import { Amplify } from '@aws-amplify/core';
5+
46
import { uploadData as advancedUploadData } from '../../../src/internals';
57
import { uploadData as uploadDataInternal } from '../../../src/providers/s3/apis/internal/uploadData';
68

@@ -54,7 +56,7 @@ describe('uploadData (internal)', () => {
5456
});
5557

5658
expect(mockedUploadDataInternal).toHaveBeenCalledTimes(1);
57-
expect(mockedUploadDataInternal).toHaveBeenCalledWith({
59+
expect(mockedUploadDataInternal).toHaveBeenCalledWith(Amplify, {
5860
path: 'input/path/to/mock/object',
5961
data: 'data',
6062
options: {

packages/storage/__tests__/providers/s3/apis/internal/uploadData/index.test.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import { AmplifyClassV6 } from '@aws-amplify/core';
5+
46
import { uploadData } from '../../../../../../src/providers/s3/apis/internal/uploadData';
57
import { MAX_OBJECT_SIZE } from '../../../../../../src/providers/s3/utils/constants';
68
import { createUploadTask } from '../../../../../../src/providers/s3/utils';
@@ -23,6 +25,8 @@ jest.mock(
2325
'../../../../../../src/providers/s3/apis/internal/uploadData/multipart',
2426
);
2527

28+
const mockAmplifyInstance = {} as AmplifyClassV6;
29+
2630
const testPath = 'testPath/object';
2731
const validBucketOwner = '111122223333';
2832
const mockCreateUploadTask = createUploadTask as jest.Mock;
@@ -48,7 +52,7 @@ describe('uploadData with key', () => {
4852
key: 'key',
4953
data: { size: MAX_OBJECT_SIZE + 1 } as any,
5054
};
51-
expect(() => uploadData(mockUploadInput)).toThrow(
55+
expect(() => uploadData(mockAmplifyInstance, mockUploadInput)).toThrow(
5256
expect.objectContaining(
5357
validationErrorMap[StorageValidationErrorCode.ObjectIsTooLarge],
5458
),
@@ -57,7 +61,7 @@ describe('uploadData with key', () => {
5761

5862
it('should throw if data size is unknown', async () => {
5963
expect(() =>
60-
uploadData({
64+
uploadData(mockAmplifyInstance, {
6165
key: 'key',
6266
data: {} as any,
6367
}),
@@ -72,7 +76,7 @@ describe('uploadData with key', () => {
7276
describe('use putObject for small uploads', () => {
7377
const smallData = { size: 5 * 1024 * 1024 } as any;
7478
it('should use putObject if data size is <= 5MB', async () => {
75-
uploadData({
79+
uploadData(mockAmplifyInstance, {
7680
key: 'key',
7781
data: smallData,
7882
});
@@ -86,9 +90,10 @@ describe('uploadData with key', () => {
8690
data: '', // 0 bytes
8791
};
8892

89-
uploadData(testInput);
93+
uploadData(mockAmplifyInstance, testInput);
9094

9195
expect(mockPutObjectJob).toHaveBeenCalledWith(
96+
mockAmplifyInstance,
9297
expect.objectContaining(testInput),
9398
expect.any(AbortSignal),
9499
expect.any(Number),
@@ -99,7 +104,7 @@ describe('uploadData with key', () => {
99104
it('should use uploadTask', async () => {
100105
mockPutObjectJob.mockReturnValueOnce('putObjectJob');
101106
mockCreateUploadTask.mockReturnValueOnce('uploadTask');
102-
const task = uploadData({
107+
const task = uploadData(mockAmplifyInstance, {
103108
key: 'key',
104109
data: smallData,
105110
});
@@ -117,7 +122,7 @@ describe('uploadData with key', () => {
117122
describe('use multipartUpload for large uploads', () => {
118123
const biggerData = { size: 5 * 1024 * 1024 + 1 } as any;
119124
it('should use multipartUpload if data size is > 5MB', async () => {
120-
uploadData({
125+
uploadData(mockAmplifyInstance, {
121126
key: 'key',
122127
data: biggerData,
123128
});
@@ -127,7 +132,7 @@ describe('uploadData with key', () => {
127132

128133
it('should use uploadTask', async () => {
129134
mockCreateUploadTask.mockReturnValueOnce('uploadTask');
130-
const task = uploadData({
135+
const task = uploadData(mockAmplifyInstance, {
131136
key: 'key',
132137
data: biggerData,
133138
});
@@ -144,7 +149,7 @@ describe('uploadData with key', () => {
144149
});
145150

146151
it('should call getMultipartUploadHandlers', async () => {
147-
uploadData({
152+
uploadData(mockAmplifyInstance, {
148153
key: 'key',
149154
data: biggerData,
150155
});
@@ -164,7 +169,7 @@ describe('uploadData with path', () => {
164169
path: testPath,
165170
data: { size: MAX_OBJECT_SIZE + 1 } as any,
166171
};
167-
expect(() => uploadData(mockUploadInput)).toThrow(
172+
expect(() => uploadData(mockAmplifyInstance, mockUploadInput)).toThrow(
168173
expect.objectContaining(
169174
validationErrorMap[StorageValidationErrorCode.ObjectIsTooLarge],
170175
),
@@ -173,7 +178,7 @@ describe('uploadData with path', () => {
173178

174179
it('should throw if data size is unknown', async () => {
175180
expect(() =>
176-
uploadData({
181+
uploadData(mockAmplifyInstance, {
177182
path: testPath,
178183
data: {} as any,
179184
}),
@@ -203,9 +208,10 @@ describe('uploadData with path', () => {
203208
data: smallData,
204209
};
205210

206-
uploadData(testInput);
211+
uploadData(mockAmplifyInstance, testInput);
207212

208213
expect(mockPutObjectJob).toHaveBeenCalledWith(
214+
mockAmplifyInstance,
209215
expect.objectContaining(testInput),
210216
expect.any(AbortSignal),
211217
expect.any(Number),
@@ -220,9 +226,10 @@ describe('uploadData with path', () => {
220226
data: '', // 0 bytes
221227
};
222228

223-
uploadData(testInput);
229+
uploadData(mockAmplifyInstance, testInput);
224230

225231
expect(mockPutObjectJob).toHaveBeenCalledWith(
232+
mockAmplifyInstance,
226233
expect.objectContaining(testInput),
227234
expect.any(AbortSignal),
228235
expect.any(Number),
@@ -234,7 +241,7 @@ describe('uploadData with path', () => {
234241
mockPutObjectJob.mockReturnValueOnce('putObjectJob');
235242
mockCreateUploadTask.mockReturnValueOnce('uploadTask');
236243

237-
const task = uploadData({
244+
const task = uploadData(mockAmplifyInstance, {
238245
path: testPath,
239246
data: smallData,
240247
});
@@ -258,18 +265,19 @@ describe('uploadData with path', () => {
258265
data: biggerData,
259266
};
260267

261-
uploadData(testInput);
268+
uploadData(mockAmplifyInstance, testInput);
262269

263270
expect(mockPutObjectJob).not.toHaveBeenCalled();
264271
expect(mockGetMultipartUploadHandlers).toHaveBeenCalledWith(
272+
mockAmplifyInstance,
265273
expect.objectContaining(testInput),
266274
expect.any(Number),
267275
);
268276
});
269277

270278
it('should use uploadTask', async () => {
271279
mockCreateUploadTask.mockReturnValueOnce('uploadTask');
272-
const task = uploadData({
280+
const task = uploadData(mockAmplifyInstance, {
273281
path: testPath,
274282
data: biggerData,
275283
});
@@ -290,14 +298,15 @@ describe('uploadData with path', () => {
290298
it('should include expectedBucketOwner in headers when provided for singlepartUpload', async () => {
291299
mockPutObjectJob.mockReturnValueOnce('putObjectJob');
292300
const smallData = 'smallData';
293-
uploadData({
301+
uploadData(mockAmplifyInstance, {
294302
path: testPath,
295303
data: smallData,
296304
options: {
297305
expectedBucketOwner: validBucketOwner,
298306
},
299307
});
300308
expect(mockPutObjectJob).toHaveBeenCalledWith(
309+
mockAmplifyInstance,
301310
expect.objectContaining({
302311
path: 'testPath/object',
303312
data: 'smallData',
@@ -320,8 +329,9 @@ describe('uploadData with path', () => {
320329
expectedBucketOwner: validBucketOwner,
321330
},
322331
};
323-
uploadData(testInput);
332+
uploadData(mockAmplifyInstance, testInput);
324333
expect(mockGetMultipartUploadHandlers).toHaveBeenCalledWith(
334+
mockAmplifyInstance,
325335
{
326336
...testInput,
327337
options: expect.objectContaining(testInput.options),

0 commit comments

Comments
 (0)