diff --git a/packages/middleware-sdk-s3/package.json b/packages/middleware-sdk-s3/package.json index 48f1aec925515..ad7f6270b2732 100644 --- a/packages/middleware-sdk-s3/package.json +++ b/packages/middleware-sdk-s3/package.json @@ -25,6 +25,7 @@ "@aws-sdk/types": "*", "@aws-sdk/util-arn-parser": "*", "@smithy/protocol-http": "^3.0.3", + "@smithy/smithy-client": "^2.1.4", "@smithy/types": "^2.3.1", "tslib": "^2.5.0" }, diff --git a/packages/middleware-sdk-s3/src/check-content-length-header.spec.ts b/packages/middleware-sdk-s3/src/check-content-length-header.spec.ts index 71f4afb3636f9..98f5703a2b0e5 100644 --- a/packages/middleware-sdk-s3/src/check-content-length-header.spec.ts +++ b/packages/middleware-sdk-s3/src/check-content-length-header.spec.ts @@ -1,3 +1,5 @@ +import { NoOpLogger } from "@smithy/smithy-client"; + import { checkContentLengthHeader } from "./check-content-length-header"; describe("checkContentLengthHeaderMiddleware", () => { @@ -33,6 +35,28 @@ describe("checkContentLengthHeaderMiddleware", () => { ); }); + it("warns with console if logger is the default NoOpLogger", async () => { + const handler = checkContentLengthHeader()(mockNextHandler, { + logger: new NoOpLogger(), + }); + + await handler({ + request: { + method: null, + protocol: null, + hostname: null, + path: null, + query: {}, + headers: {}, + }, + input: {}, + }); + + expect(spy).toHaveBeenCalledWith( + "Are you using a Stream of unknown length as the Body of a PutObject request? Consider using Upload instead from @aws-sdk/lib-storage." + ); + }); + it("uses the context logger if available", async () => { const context = { logger: { diff --git a/packages/middleware-sdk-s3/src/check-content-length-header.ts b/packages/middleware-sdk-s3/src/check-content-length-header.ts index eddc551a2a0d9..95cdb14e0e4d8 100644 --- a/packages/middleware-sdk-s3/src/check-content-length-header.ts +++ b/packages/middleware-sdk-s3/src/check-content-length-header.ts @@ -1,4 +1,5 @@ import { HttpRequest } from "@smithy/protocol-http"; +import { NoOpLogger } from "@smithy/smithy-client"; import { FinalizeHandler, FinalizeHandlerArguments, @@ -29,7 +30,7 @@ export function checkContentLengthHeader(): FinalizeRequestMiddleware if (HttpRequest.isInstance(request)) { if (!request.headers[CONTENT_LENGTH_HEADER]) { const message = `Are you using a Stream of unknown length as the Body of a PutObject request? Consider using Upload instead from @aws-sdk/lib-storage.`; - if (typeof context?.logger?.warn === "function") { + if (typeof context?.logger?.warn === "function" && !(context.logger instanceof NoOpLogger)) { context.logger.warn(message); } else { console.warn(message);