@@ -27,7 +27,8 @@ import { extendedEncodeURIComponent } from "@smithy/smithy-client";
2727import type { AbortController as IAbortController , AbortSignal as IAbortSignal , Endpoint } from "@smithy/types" ;
2828import { EventEmitter } from "events" ;
2929
30- import { byteLength } from "./bytelength" ;
30+ import { byteLength } from "./byteLength" ;
31+ import { BYTE_LENGTH_SOURCE , byteLengthSource } from "./byteLengthSource" ;
3132import { getChunk } from "./chunker" ;
3233import { BodyDataTypes , Options , Progress } from "./types" ;
3334
@@ -60,6 +61,7 @@ export class Upload extends EventEmitter {
6061
6162 // used for reporting progress.
6263 private totalBytes ?: number ;
64+ private readonly totalBytesSource ?: BYTE_LENGTH_SOURCE ;
6365 private bytesUploadedSoFar : number ;
6466
6567 // used in the upload.
@@ -98,6 +100,7 @@ export class Upload extends EventEmitter {
98100
99101 // set progress defaults
100102 this . totalBytes = this . params . ContentLength ?? byteLength ( this . params . Body ) ;
103+ this . totalBytesSource = byteLengthSource ( this . params . Body , this . params . ContentLength ) ;
101104 this . bytesUploadedSoFar = 0 ;
102105 this . abortController = options . abortController ?? new AbortController ( ) ;
103106
@@ -381,9 +384,14 @@ export class Upload extends EventEmitter {
381384
382385 let result ;
383386 if ( this . isMultiPart ) {
384- const { expectedPartsCount, uploadedParts } = this ;
385- if ( expectedPartsCount !== undefined && uploadedParts . length !== expectedPartsCount ) {
386- throw new Error ( `Expected ${ expectedPartsCount } part(s) but uploaded ${ uploadedParts . length } part(s).` ) ;
387+ const { expectedPartsCount, uploadedParts, totalBytes, totalBytesSource } = this ;
388+ if ( totalBytes !== undefined && expectedPartsCount !== undefined && uploadedParts . length !== expectedPartsCount ) {
389+ throw new Error ( `Expected ${ expectedPartsCount } part(s) but uploaded ${ uploadedParts . length } part(s).
390+ The expected part count is based on the byte-count of the input.params.Body,
391+ which was read from ${ totalBytesSource } and is ${ totalBytes } .
392+ If this is not correct, provide an override value by setting a number
393+ to input.params.ContentLength in bytes.
394+ ` ) ;
387395 }
388396
389397 this . uploadedParts . sort ( ( a , b ) => a . PartNumber ! - b . PartNumber ! ) ;
@@ -478,7 +486,7 @@ export class Upload extends EventEmitter {
478486
479487 if ( this . partSize < Upload . MIN_PART_SIZE ) {
480488 throw new Error (
481- `EntityTooSmall: Your proposed upload partsize [${ this . partSize } ] is smaller than the minimum allowed size [${ Upload . MIN_PART_SIZE } ] (5MB)`
489+ `EntityTooSmall: Your proposed upload part size [${ this . partSize } ] is smaller than the minimum allowed size [${ Upload . MIN_PART_SIZE } ] (5MB)`
482490 ) ;
483491 }
484492
0 commit comments