Skip to content

Commit 45aecae

Browse files
authored
@uppy/aws-s3: always set S3 meta to UppyFile & include key (#5602)
1 parent b66a794 commit 45aecae

File tree

6 files changed

+32
-15
lines changed

6 files changed

+32
-15
lines changed

packages/@uppy/aws-s3/src/HTTPCommunicationQueue.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,21 @@ export class HTTPCommunicationQueue<M extends Meta, B extends Body> {
276276
signal,
277277
}).abortOn(signal)) as unknown as B // todo this doesn't make sense
278278

279-
// location will be missing from result if CORS is not correctly set up on the bucket.
280-
return 'location' in result ? result : (
281-
{
282-
// todo `url` is not really the final location URL of the resulting file, it's just the base URL of the bucket
283-
// https://github.com/transloadit/uppy/issues/5388
284-
location: removeMetadataFromURL(url),
285-
...result,
286-
}
279+
const key = fields?.key
280+
if (!key) {
281+
console.error(
282+
'Expected `fields.key` to be returend but the backend/Companion',
287283
)
284+
}
285+
this.#setS3MultipartState(file, { key: key! })
286+
287+
return {
288+
...result,
289+
location:
290+
(result.location as string | undefined) ?? removeMetadataFromURL(url),
291+
bucket: fields?.bucket,
292+
key,
293+
}
288294
}
289295

290296
async uploadFile(
@@ -393,7 +399,8 @@ export class HTTPCommunicationQueue<M extends Meta, B extends Body> {
393399

394400
try {
395401
signature = await this.#fetchSignature(this.#getFile(file), {
396-
uploadId,
402+
// Always defined for multipart uploads
403+
uploadId: uploadId!,
397404
key,
398405
partNumber,
399406
body: chunkData,

packages/@uppy/aws-s3/src/MultipartUploader.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface MultipartUploaderOptions<M extends Meta, B extends Body> {
1616
file: UppyFile<M, B>
1717
log: Uppy<M, B>['log']
1818

19-
uploadId: string
19+
uploadId?: string
2020
key: string
2121
}
2222

packages/@uppy/aws-s3/src/index.test.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ describe('AwsS3Multipart', () => {
5050
getUploadParameters: () => ({
5151
method: 'POST',
5252
url: 'https://bucket.s3.us-east-2.amazonaws.com/',
53-
fields: {},
53+
fields: {
54+
key: 'file',
55+
bucket: 'https://bucket.s3.us-east-2.amazonaws.com/',
56+
},
5457
}),
5558
})
5659
const scope = nock(
@@ -89,6 +92,8 @@ describe('AwsS3Multipart', () => {
8992
ETag: 'test',
9093
etag: 'test',
9194
location: 'http://example.com',
95+
key: 'file',
96+
bucket: 'https://bucket.s3.us-east-2.amazonaws.com/',
9297
},
9398
status: 200,
9499
uploadURL: 'http://example.com',

packages/@uppy/aws-s3/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ export default class AwsS3Multipart<
510510
return this.#client
511511
.get<
512512
AwsS3Part[]
513-
>(`s3/multipart/${encodeURIComponent(uploadId)}?key=${filename}`, { signal })
513+
>(`s3/multipart/${encodeURIComponent(uploadId!)}?key=${filename}`, { signal })
514514
.then(assertServerError)
515515
}
516516

@@ -524,7 +524,7 @@ export default class AwsS3Multipart<
524524
throwIfAborted(signal)
525525

526526
const filename = encodeURIComponent(key)
527-
const uploadIdEnc = encodeURIComponent(uploadId)
527+
const uploadIdEnc = encodeURIComponent(uploadId!)
528528
return this.#client
529529
.post<B>(
530530
`s3/multipart/${uploadIdEnc}/complete?key=${filename}`,
@@ -633,7 +633,7 @@ export default class AwsS3Multipart<
633633
this.#assertHost('abortMultipartUpload')
634634

635635
const filename = encodeURIComponent(key)
636-
const uploadIdEnc = encodeURIComponent(uploadId)
636+
const uploadIdEnc = encodeURIComponent(uploadId!)
637637
return this.#client
638638
.delete<void>(`s3/multipart/${uploadIdEnc}?key=${filename}`, undefined, {
639639
signal,

packages/@uppy/aws-s3/src/utils.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function throwIfAborted(signal?: AbortSignal | null): void {
1111
}
1212
}
1313

14-
export type UploadResult = { key: string; uploadId: string }
14+
export type UploadResult = { key: string; uploadId?: string; bucket?: string }
1515
export type UploadResultWithSignal = UploadResult & { signal?: AbortSignal }
1616
export type MultipartUploadResult = UploadResult & { parts: AwsS3Part[] }
1717
export type MultipartUploadResultWithSignal = MultipartUploadResult & {
@@ -25,4 +25,6 @@ export type UploadPartBytesResult = {
2525

2626
export interface AwsBody extends Body {
2727
location: string
28+
key: string
29+
bucket: string
2830
}

packages/@uppy/companion/src/server/controllers/s3.js

+3
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ module.exports = function s3 (config) {
135135
res.json({
136136
key: data.Key,
137137
uploadId: data.UploadId,
138+
bucket: data.Bucket
138139
})
139140
}, next)
140141
}
@@ -360,6 +361,8 @@ module.exports = function s3 (config) {
360361
})).then(data => {
361362
res.json({
362363
location: data.Location,
364+
key: data.Key,
365+
bucket: data.Bucket
363366
})
364367
}, next)
365368
}

0 commit comments

Comments
 (0)