-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fio 7239: Support for AWS S3 multipart uploads (4.18.x) (#5376)
* adapt multipart upload feature to 4.18.x * send token with requests that need them; error handling * add logging for error during tests * add abortcontroller polyfill * try to lazily require abortcontroller polyfill
- Loading branch information
1 parent
22b32cf
commit 59ec20a
Showing
10 changed files
with
373 additions
and
107 deletions.
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
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,79 @@ | ||
import assert from 'assert'; | ||
import sinon from 'sinon'; | ||
import fetchMock from 'fetch-mock'; | ||
|
||
import S3 from './s3'; | ||
import { withRetries } from './util'; | ||
|
||
describe('S3 Provider', () => { | ||
describe('Function Unit Tests', () => { | ||
it('withRetries should retry a given function three times, then throw the provided error', (done) => { | ||
function sleepAndReject(ms) { | ||
return new Promise((_, reject) => setTimeout(reject, ms)); | ||
} | ||
|
||
const spy = sinon.spy(sleepAndReject); | ||
withRetries(spy, [200], 3, 'Custom error message').catch((err) => { | ||
assert.equal(err.message, 'Custom error message'); | ||
assert.equal(spy.callCount, 3); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Provider Integration Tests', () => { | ||
describe('AWS S3 Multipart Uploads', () => { | ||
before('Mocks fetch', () => { | ||
fetchMock | ||
.post('https://fakeproject.form.io/fakeform/storage/s3', { | ||
signed: new Array(5).fill('https://fakebucketurl.aws.com/signed'), | ||
minio: false, | ||
url: 'https://fakebucketurl.aws.com', | ||
bucket: 'fakebucket', | ||
uploadId: 'fakeuploadid', | ||
key: 'test.jpg', | ||
partSizeActual: 1, | ||
data: {} | ||
}) | ||
.put('https://fakebucketurl.aws.com/signed', { status: 200, headers: { 'Etag': 'fakeetag' } }) | ||
.post('https://fakeproject.form.io/fakeform/storage/s3/multipart/complete', 200) | ||
.post('https://fakeproject.form.io/fakeform/storage/s3/multipart/abort', 200); | ||
}); | ||
it('Given an array of signed urls it should upload a file to S3 using multipart upload', (done) => { | ||
const mockFormio = { | ||
formUrl: 'https://fakeproject.form.io/fakeform', | ||
getToken: () => {} | ||
}; | ||
const s3 = new S3(mockFormio); | ||
const uploadSpy = sinon.spy(s3, 'uploadParts'); | ||
const completeSpy = sinon.spy(s3, 'completeMultipartUpload'); | ||
|
||
const mockFile = new File(['test!'], 'test.jpg', { type: 'image/jpeg' }); | ||
s3.uploadFile( | ||
mockFile, | ||
'test.jpg', | ||
'', | ||
() => {}, | ||
'', | ||
{}, | ||
'test.jpg', | ||
{}, | ||
'', | ||
() => {}, | ||
{ partSize: 1, changeMessage: () => {}, progressCallback: () => {} } | ||
).then((response) => { | ||
assert.equal(response.storage, 's3'); | ||
assert.equal(response.name, 'test.jpg'); | ||
assert.equal(response.bucket, 'fakebucket'); | ||
assert.equal(response.url, 'https://fakebucketurl.aws.com/test.jpg'); | ||
assert.equal(response.acl, undefined); | ||
assert.equal(response.size, 5); | ||
assert.equal(response.type, 'image/jpeg'); | ||
assert.equal(uploadSpy.callCount, 1); | ||
assert.equal(completeSpy.callCount, 1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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,6 @@ | ||
export async function withRetries(fn, args, retries = 3, err = null) { | ||
if (!retries) { | ||
throw new Error(err); | ||
} | ||
return fn(...args).catch(() => withRetries(fn, args, retries - 1, err)); | ||
} |
Oops, something went wrong.