Skip to content

Commit 6c44d87

Browse files
committed
docs(supplemental-docs): version update for JSv3
1 parent d34f5a3 commit 6c44d87

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

packages/middleware-flexible-checksums/src/middleware-md5-fallback.e2e.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe("S3 MD5 Fallback for DeleteObjects", () => {
1313
let s3: S3;
1414
let Bucket: string;
1515
const testFiles = ["md5-test-1.txt", "md5-test-2.txt"];
16+
const md5Hash = createHash("md5");
1617

1718
beforeAll(async () => {
1819
s3 = new S3({ region: "us-west-2" });
@@ -73,16 +74,15 @@ describe("S3 MD5 Fallback for DeleteObjects", () => {
7374
let crc32Removed = false;
7475

7576
md5S3Client.middlewareStack.add(
76-
(next) => async (args) => {
77+
(next, context) => async (args) => {
7778
// Check if this is a DeleteObjects command
78-
const isDeleteObjects = args.constructor?.name === "DeleteObjectsCommand" || args.input?.Delete !== undefined;
79+
const isDeleteObjects = context.commandName === "DeleteObjects";
7980

8081
if (!isDeleteObjects) {
8182
return next(args);
8283
}
8384

8485
const result = await next(args);
85-
8686
const headers = args.request.headers;
8787

8888
// Remove checksum headers
@@ -99,8 +99,7 @@ describe("S3 MD5 Fallback for DeleteObjects", () => {
9999
// Add MD5
100100
if (args.request.body) {
101101
const bodyContent = Buffer.from(args.request.body);
102-
const md5Hash = createHash("md5").update(bodyContent).digest("base64");
103-
headers["Content-MD5"] = md5Hash;
102+
headers["Content-MD5"] = md5Hash.update(bodyContent).digest("base64");
104103
md5Added = true;
105104
}
106105

supplemental-docs/MD5_FALLBACK.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
## Background
44

5-
Recently the AWS SDKs shipped a feature that [changed default object integrity in S3](https://github.com/aws/aws-sdk-js-v3/issues/6810). The SDKs now default to using more modern checksums (like CRC32) to ensure object integrity, whereas previously MD5 checksums were being used. Some third-party S3-compatible services currently do not support these checksums. To our knowledge, this affects only the S3 `DeleteObjects` operation.
5+
In AWS SDK for JavaScript [v3.729.0](https://github.com/aws/aws-sdk-js-v3/releases/tag/v3.729.0), we shipped a feature that [changed default object integrity in S3](https://github.com/aws/aws-sdk-js-v3/issues/6810). The SDKs now default to using more modern checksums (like CRC32) to ensure object integrity, whereas previously MD5 checksums were being used. Some third-party S3-compatible services currently do not support these checksums. To our knowledge, this affects only the S3 `DeleteObjects` operation.
66

7-
If you wish to fallback to the old behavior of sending MD5 checksums, for operations like `DeleteObjectsCommand` this is how you can do it in AWS SDK for JavaScript v3:
7+
If you wish to fallback to the old behavior of sending MD5 checksums, for operations like `DeleteObjectsCommand` this is how
8+
you can do it in AWS SDK for JavaScript v3:
89

910
## MD5 fallback
1011

11-
The following code provides a custom S3 client that will use MD5 checksums for DeleteObjects operations while maintaining the default behavior for all other operations.
12+
The following code provides a custom S3 client that will use MD5 checksums for DeleteObjects operations while maintaining the
13+
default behavior for all other operations.
1214

1315
```javascript
1416
// md5ClientS3.mjs
@@ -20,19 +22,18 @@ import { createHash } from "crypto";
2022
*/
2123
export function createS3ClientWithMD5() {
2224
const client = new S3Client({});
25+
const md5Hash = createHash("md5");
2326

2427
client.middlewareStack.add(
25-
(next) => async (args) => {
28+
(next, context) => async (args) => {
2629
// Check if this is a DeleteObjects command
27-
const isDeleteObjects = args.constructor?.name === "DeleteObjectsCommand" || args.input?.Delete !== undefined;
30+
const isDeleteObjects = context.commandName === "DeleteObjects";
2831

2932
if (!isDeleteObjects) {
3033
return next(args);
3134
}
3235

3336
const result = await next(args);
34-
35-
// Modify the final request headers
3637
const headers = args.request.headers;
3738

3839
// Remove any checksum headers
@@ -45,17 +46,16 @@ export function createS3ClientWithMD5() {
4546
}
4647
});
4748

48-
// Calculate and add MD5 for the request body
49+
// Add MD5
4950
if (args.request.body) {
5051
const bodyContent = Buffer.from(args.request.body);
51-
const md5Hash = createHash("md5").update(bodyContent).digest("base64");
52-
headers["Content-MD5"] = md5Hash;
52+
headers["Content-MD5"] = md5Hash.update(bodyContent).digest("base64");
5353
}
5454

5555
return result;
5656
},
5757
{
58-
step: "finalizeRequest", // Run after all other request modifications
58+
step: "finalizeRequest",
5959
name: "addMD5Checksum",
6060
}
6161
);
@@ -95,13 +95,13 @@ try {
9595

9696
The solution adds middleware to the S3 client that:
9797

98-
1. Detects DeleteObjects operations
98+
1. Detects DeleteObjects operations using the command name
9999
2. Lets the SDK add its default headers
100100
3. Removes any checksum headers in the finalizeRequest step
101101
4. Calculates an MD5 hash of the request body
102102
5. Adds the MD5 hash as a Content-MD5 header
103103

104-
This sequence ensures that we properly replace the current checksums with the MD5 checksum.
104+
This sequence ensures that we properly replace the checksums with MD5 checksum.
105105

106106
## Usage Notes
107107

@@ -110,7 +110,8 @@ This sequence ensures that we properly replace the current checksums with the MD
110110

111111
## Debugging
112112

113-
To verify that the MD5 checksum is being correctly applied, you can add console logging to the middleware by modifying the code to include logging statements:
113+
To verify that the MD5 checksum is being correctly applied, you can add console logging to the middleware by modifying the
114+
code to include logging statements:
114115

115116
```javascript
116117
// Inside the middleware function, add:

0 commit comments

Comments
 (0)