Skip to content

Commit

Permalink
@uppy/aws-s3: always set S3 to UppyFile & include key
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon committed Jan 14, 2025
1 parent a2f1b0c commit 4ef4c82
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
24 changes: 15 additions & 9 deletions packages/@uppy/aws-s3/src/HTTPCommunicationQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,20 @@ export class HTTPCommunicationQueue<M extends Meta, B extends Body> {
signal,
}).abortOn(signal)) as unknown as B // todo this doesn't make sense

// location will be missing from result if CORS is not correctly set up on the bucket.
return 'location' in result ? result : (
{
// todo `url` is not really the final location URL of the resulting file, it's just the base URL of the bucket
// https://github.com/transloadit/uppy/issues/5388
location: removeMetadataFromURL(url),
...result,
}
const key = fields?.key
if (!key) {
throw new Error(

Check failure on line 281 in packages/@uppy/aws-s3/src/HTTPCommunicationQueue.ts

View workflow job for this annotation

GitHub Actions / Unit tests (18.x)

packages/@uppy/aws-s3/src/index.test.ts > AwsS3Multipart > non-multipart upload > should handle POST uploads

Error: Expected `fields.key` to be returend but the backend/Companion ❯ HTTPCommunicationQueue.#nonMultipartUpload packages/@uppy/aws-s3/src/HTTPCommunicationQueue.ts:281:13

Check failure on line 281 in packages/@uppy/aws-s3/src/HTTPCommunicationQueue.ts

View workflow job for this annotation

GitHub Actions / Unit tests (20.x)

packages/@uppy/aws-s3/src/index.test.ts > AwsS3Multipart > non-multipart upload > should handle POST uploads

Error: Expected `fields.key` to be returend but the backend/Companion ❯ HTTPCommunicationQueue.#nonMultipartUpload packages/@uppy/aws-s3/src/HTTPCommunicationQueue.ts:281:13
'Expected `fields.key` to be returend but the backend/Companion',
)
}
this.#setS3MultipartState(file, { key })

return {
...result,
location: removeMetadataFromURL(url),
bucket: fields.bucket,
key,
}
}

async uploadFile(
Expand Down Expand Up @@ -393,7 +398,8 @@ export class HTTPCommunicationQueue<M extends Meta, B extends Body> {

try {
signature = await this.#fetchSignature(this.#getFile(file), {
uploadId,
// Always defined for multipart uploads
uploadId: uploadId!,
key,
partNumber,
body: chunkData,
Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/aws-s3/src/MultipartUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface MultipartUploaderOptions<M extends Meta, B extends Body> {
file: UppyFile<M, B>
log: Uppy<M, B>['log']

uploadId: string
uploadId?: string
key: string
}

Expand Down
6 changes: 3 additions & 3 deletions packages/@uppy/aws-s3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ export default class AwsS3Multipart<
return this.#client
.get<
AwsS3Part[]
>(`s3/multipart/${encodeURIComponent(uploadId)}?key=${filename}`, { signal })
>(`s3/multipart/${encodeURIComponent(uploadId!)}?key=${filename}`, { signal })
.then(assertServerError)
}

Expand All @@ -524,7 +524,7 @@ export default class AwsS3Multipart<
throwIfAborted(signal)

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

const filename = encodeURIComponent(key)
const uploadIdEnc = encodeURIComponent(uploadId)
const uploadIdEnc = encodeURIComponent(uploadId!)
return this.#client
.delete<void>(`s3/multipart/${uploadIdEnc}?key=${filename}`, undefined, {
signal,
Expand Down
4 changes: 3 additions & 1 deletion packages/@uppy/aws-s3/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function throwIfAborted(signal?: AbortSignal | null): void {
}
}

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

export interface AwsBody extends Body {
location: string
key: string
bucket: string
}
3 changes: 3 additions & 0 deletions packages/@uppy/companion/src/server/controllers/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ module.exports = function s3 (config) {
res.json({
key: data.Key,
uploadId: data.UploadId,
bucket: data.Bucket
})
}, next)
}
Expand Down Expand Up @@ -360,6 +361,8 @@ module.exports = function s3 (config) {
})).then(data => {
res.json({
location: data.Location,
key: data.Key,
bucket: data.Bucket
})
}, next)
}
Expand Down

0 comments on commit 4ef4c82

Please sign in to comment.