|
| 1 | +# MD5 Checksum Fallback for AWS SDK for JavaScript v3 |
| 2 | + |
| 3 | +## Background |
| 4 | + |
| 5 | +In AWS SDK for JavaScript [v3.729.0](https://github.com/aws/aws-sdk-js-v3/releases/tag/v3.729.0), |
| 6 | +we shipped a feature that [changed default object integrity in |
| 7 | +S3](https://github.com/aws/aws-sdk-js-v3/issues/6810). The SDKs now default to using more modern |
| 8 | +checksums (like CRC32) to ensure object integrity, whereas previously MD5 checksums were being used. |
| 9 | +Some third-party S3-compatible services currently do not support these checksums. To our knowledge, |
| 10 | +this affects only the S3 `DeleteObjects` operation. |
| 11 | + |
| 12 | +If you wish to fallback to the old behavior of sending MD5 checksums, for operations like |
| 13 | +`DeleteObjectsCommand` this is how you can do it in AWS SDK for JavaScript v3: |
| 14 | + |
| 15 | +## MD5 fallback |
| 16 | + |
| 17 | +The following code provides a custom S3 client that will use MD5 checksums for DeleteObjects |
| 18 | +operations while maintaining the default behavior for all other operations. |
| 19 | + |
| 20 | +```javascript |
| 21 | +// md5ClientS3.mjs |
| 22 | +import { S3Client } from "@aws-sdk/client-s3"; |
| 23 | +import { createHash } from "crypto"; |
| 24 | + |
| 25 | +/** |
| 26 | + * Creates an S3 client that uses MD5 checksums for DeleteObjects operations |
| 27 | + */ |
| 28 | +export function createS3ClientWithMD5() { |
| 29 | + const client = new S3Client({}); |
| 30 | + const md5Hash = createHash("md5"); |
| 31 | + |
| 32 | + client.middlewareStack.add( |
| 33 | + (next, context) => async (args) => { |
| 34 | + // Check if this is a DeleteObjects command |
| 35 | + const isDeleteObjects = context.commandName === "DeleteObjectsCommand"; |
| 36 | + |
| 37 | + if (!isDeleteObjects) { |
| 38 | + return next(args); |
| 39 | + } |
| 40 | + |
| 41 | + const result = await next(args); |
| 42 | + const headers = args.request.headers; |
| 43 | + |
| 44 | + // Remove any checksum headers |
| 45 | + Object.keys(headers).forEach((header) => { |
| 46 | + if ( |
| 47 | + header.toLowerCase().startsWith("x-amz-checksum-") || |
| 48 | + header.toLowerCase().startsWith("x-amz-sdk-checksum-") |
| 49 | + ) { |
| 50 | + delete headers[header]; |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + // Add MD5 |
| 55 | + if (args.request.body) { |
| 56 | + const bodyContent = Buffer.from(args.request.body); |
| 57 | + headers["Content-MD5"] = md5Hash.update(bodyContent).digest("base64"); |
| 58 | + } |
| 59 | + |
| 60 | + return result; |
| 61 | + }, |
| 62 | + { |
| 63 | + step: "finalizeRequest", |
| 64 | + name: "addMD5Checksum", |
| 65 | + } |
| 66 | + ); |
| 67 | + |
| 68 | + return client; |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +## Usage |
| 73 | + |
| 74 | +Instead of creating a regular S3 client, use the `createS3ClientWithMD5` function: |
| 75 | + |
| 76 | +```javascript |
| 77 | +import { DeleteObjectsCommand } from "@aws-sdk/client-s3"; |
| 78 | +import { createS3ClientWithMD5 } from "./md5ClientS3.mjs"; |
| 79 | + |
| 80 | +// Create the client with MD5 support |
| 81 | +const client = createS3ClientWithMD5(); |
| 82 | + |
| 83 | +// Use it like a normal S3 client |
| 84 | +const deleteParams = { |
| 85 | + Bucket: "your-bucket", |
| 86 | + Delete: { |
| 87 | + Objects: [{ Key: "file1.txt" }, { Key: "file2.txt" }], |
| 88 | + }, |
| 89 | +}; |
| 90 | + |
| 91 | +try { |
| 92 | + const response = await client.send(new DeleteObjectsCommand(deleteParams)); |
| 93 | + console.log("Successfully deleted objects:", response); |
| 94 | +} catch (err) { |
| 95 | + console.error("Error:", err); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## How It Works |
| 100 | + |
| 101 | +The solution adds middleware to the S3 client that: |
| 102 | + |
| 103 | +1. Detects DeleteObjects operations using the command name |
| 104 | +2. Lets the SDK add its default headers |
| 105 | +3. Removes any checksum headers in the finalizeRequest step |
| 106 | +4. Calculates an MD5 hash of the request body |
| 107 | +5. Adds the MD5 hash as a Content-MD5 header |
| 108 | + |
| 109 | +This sequence ensures that we properly replace the checksums with MD5 checksum. |
| 110 | + |
| 111 | +## Usage Notes |
| 112 | + |
| 113 | +- The client can be configured with additional options as needed (region, credentials, etc.) |
| 114 | +- If your S3-compatible service supports the SDK's new checksum options or adds support in the |
| 115 | + future, you should use the standard S3 client instead. |
| 116 | + |
| 117 | +## Debugging |
| 118 | + |
| 119 | +To verify that the MD5 checksum is being correctly applied, you can add console logging to the |
| 120 | +middleware by modifying the code to include logging statements: |
| 121 | + |
| 122 | +```javascript |
| 123 | +// Inside the middleware function, add: |
| 124 | +console.log("Headers:", JSON.stringify(args.request.headers, null, 2)); |
| 125 | +``` |
0 commit comments