@@ -66,7 +66,7 @@ export class Upload extends EventEmitter {
6666
6767 private uploadedParts : CompletedPart [ ] = [ ] ;
6868 private uploadEnqueuedPartsCount = 0 ;
69- private expectedPartsCount = 0 ;
69+ private expectedPartsCount ?: number ;
7070 /**
7171 * Last UploadId if the upload was done with MultipartUpload and not PutObject.
7272 */
@@ -93,8 +93,8 @@ export class Upload extends EventEmitter {
9393 this . bytesUploadedSoFar = 0 ;
9494 this . abortController = options . abortController ?? new AbortController ( ) ;
9595
96- this . partSize = this . __calculatePartSize ( this . totalBytes ?? 0 , Upload . MIN_PART_SIZE ) ;
97- this . expectedPartsCount = this . totalBytes ? Math . ceil ( this . totalBytes / this . partSize ) : 1 ;
96+ this . partSize = Math . max ( Upload . MIN_PART_SIZE , Math . floor ( ( this . totalBytes || 0 ) / this . MAX_PARTS ) ) ;
97+ this . expectedPartsCount = this . totalBytes !== undefined ? Math . ceil ( this . totalBytes / this . partSize ) : undefined ;
9898
9999 this . __validateInput ( ) ;
100100 }
@@ -237,8 +237,6 @@ export class Upload extends EventEmitter {
237237 return ;
238238 }
239239
240- this . __validateUploadPart ( dataPart , this . partSize ) ;
241-
242240 // Use put instead of multipart for one chunk uploads.
243241 if ( dataPart . partNumber === 1 && dataPart . lastPart ) {
244242 return await this . __uploadUsingPut ( dataPart ) ;
@@ -287,6 +285,8 @@ export class Upload extends EventEmitter {
287285
288286 this . uploadEnqueuedPartsCount += 1 ;
289287
288+ this . __validateUploadPart ( dataPart ) ;
289+
290290 const partResult = await this . client . send (
291291 new UploadPartCommand ( {
292292 ...this . params ,
@@ -369,10 +369,9 @@ export class Upload extends EventEmitter {
369369
370370 let result ;
371371 if ( this . isMultiPart ) {
372- if ( this . totalBytes && this . uploadedParts . length !== this . expectedPartsCount ) {
373- throw new Error (
374- `Expected ${ this . expectedPartsCount } number of parts but uploaded only ${ this . uploadedParts . length } parts`
375- ) ;
372+ const { expectedPartsCount, uploadedParts } = this ;
373+ if ( expectedPartsCount !== undefined && uploadedParts . length !== expectedPartsCount ) {
374+ throw new Error ( `Expected ${ expectedPartsCount } part(s) but uploaded ${ uploadedParts . length } part(s).` ) ;
376375 }
377376
378377 this . uploadedParts . sort ( ( a , b ) => a . PartNumber ! - b . PartNumber ! ) ;
@@ -438,27 +437,24 @@ export class Upload extends EventEmitter {
438437 } ) ;
439438 }
440439
441- private __calculatePartSize ( targetPartSizeBytes : number , minPartSize : number ) : number {
442- const calculatedPartSize = Math . floor ( targetPartSizeBytes / this . MAX_PARTS ) ;
443- return Math . max ( minPartSize , calculatedPartSize ) ;
444- }
445-
446- private __validateUploadPart ( dataPart : RawDataPart , partSize : number ) : void {
447- const actualPartSize = byteLength ( dataPart . data ) || 0 ;
440+ private __validateUploadPart ( dataPart : RawDataPart ) : void {
441+ const actualPartSize = byteLength ( dataPart . data ) || undefined ;
448442
449443 // Skip validation for single-part uploads (PUT operations)
450444 if ( dataPart . partNumber === 1 && dataPart . lastPart ) {
451445 return ;
452446 }
453447
454- if ( actualPartSize === 0 ) {
455- throw new Error ( `Content length is missing on the data for part number ${ dataPart . partNumber } ` ) ;
448+ if ( actualPartSize === undefined ) {
449+ throw new Error (
450+ `A dataPart was generated without a measurable data chunk size for part number ${ dataPart . partNumber } `
451+ ) ;
456452 }
457453
458454 // Validate part size (last part may be smaller)
459- if ( ! dataPart . lastPart && actualPartSize !== partSize ) {
455+ if ( ! dataPart . lastPart && actualPartSize !== this . partSize ) {
460456 throw new Error (
461- `The Part size for part number ${ dataPart . partNumber } , size ${ actualPartSize } does not match expected size ${ partSize } `
457+ `The Part size for part number ${ dataPart . partNumber } , size ${ actualPartSize } does not match expected size ${ this . partSize } `
462458 ) ;
463459 }
464460 }
0 commit comments