Skip to content
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

@uppy/aws-s3-multipart: pass the uploadURL back to the caller #4614

Merged
merged 5 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/@uppy/aws-s3-multipart/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,13 @@ export default class AwsS3Multipart extends UploaderPlugin {

// NOTE This must be allowed by CORS.
const etag = ev.target.getResponseHeader('ETag')
const location = ev.target.getResponseHeader('Location')

if (method === 'POST' && location === null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (method === 'POST' && location === null) {
if (method?.toLowerCase() === 'POST'.toLowerCase() && location === null) {

'post' === 'POST' = false

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

== instead of === on the location null check?

// Not being able to read the Location header is not a fatal error.
// eslint-disable-next-line no-console
console.warn('AwsS3/Multipart: Could not read the Location header. This likely means CORS is not configured correctly on the S3 Bucket. See https://uppy.io/docs/aws-s3-multipart#S3-Bucket-Configuration for instructions.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this not uppy.log(msg, 'warn') instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uppy is not defined in this context (it's a static method, so this.uppy won't work either).

}
if (etag === null) {
reject(new Error('AwsS3/Multipart: Could not read the ETag header. This likely means CORS is not configured correctly on the S3 Bucket. See https://uppy.io/docs/aws-s3-multipart#S3-Bucket-Configuration for instructions.'))
return
Expand All @@ -675,6 +681,7 @@ export default class AwsS3Multipart extends UploaderPlugin {
onComplete?.(etag)
resolve({
ETag: etag,
...(location ? { location } : undefined),
})
})

Expand Down
55 changes: 55 additions & 0 deletions packages/@uppy/aws-s3-multipart/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,61 @@ describe('AwsS3Multipart', () => {
})
})

describe('non-multipart upload', () => {
it('should handle POST uploads', async () => {
const core = new Core()
core.use(AwsS3Multipart, {
shouldUseMultipart: false,
limit: 0,
getUploadParameters: () => ({
method: 'POST',
url: 'https://bucket.s3.us-east-2.amazonaws.com/',
fields: {},
}),
})
const scope = nock(
'https://bucket.s3.us-east-2.amazonaws.com',
).defaultReplyHeaders({
'access-control-allow-headers': '*',
'access-control-allow-method': 'POST',
'access-control-allow-origin': '*',
'access-control-expose-headers': 'ETag, Location',
})

scope.options('/').reply(204, '')
scope
.post('/')
.reply(201, '', { ETag: 'test', Location: 'http://example.com' })

const fileSize = 1

core.addFile({
source: 'jest',
name: 'multitest.dat',
type: 'application/octet-stream',
data: new File([new Uint8Array(fileSize)], {
type: 'application/octet-stream',
}),
})

const uploadSuccessHandler = jest.fn()
core.on('upload-success', uploadSuccessHandler)

await core.upload()

expect(uploadSuccessHandler.mock.calls).toHaveLength(1)
expect(uploadSuccessHandler.mock.calls[0][1]).toStrictEqual({
body: {
ETag: 'test',
location: 'http://example.com',
},
uploadURL: 'http://example.com',
})

scope.done()
})
})

describe('without companionUrl (custom main functions)', () => {
let core
let awsS3Multipart
Expand Down