anacronw / multer-s3

multer storage engine for amazon s3
MIT License
660 stars 190 forks source link

Support checksum algorithms #215

Open shapel opened 4 months ago

shapel commented 4 months ago

Hi, @anacronw πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

I need to support ChecksumAlgorithm options in my project so I used patch-package to patch multer-s3@3.0.1. https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html?icmpid=docs_amazons3_console

Ping me if you would like me to create a PR. 🀝


Here is the diff that solved my problem:

diff --git a/node_modules/multer-s3/index.js b/node_modules/multer-s3/index.js
index f64fa00..0bd907b 100644
--- a/node_modules/multer-s3/index.js
+++ b/node_modules/multer-s3/index.js
@@ -23,6 +23,7 @@ var defaultContentEncoding = staticValue(null)
 var defaultStorageClass = staticValue('STANDARD')
 var defaultSSE = staticValue(null)
 var defaultSSEKMS = staticValue(null)
+var defaultChecksumAlgorithm = staticValue(undefined)

 // Regular expression to detect svg file content, inspired by: https://github.com/sindresorhus/is-svg/blob/master/index.js
 // It is not always possible to check for an end tag if a file is very big. The firstChunk, see below, might not be the entire file.
@@ -77,7 +78,8 @@ function collect (storage, req, file, cb) {
     storage.getStorageClass.bind(storage, req, file),
     storage.getSSE.bind(storage, req, file),
     storage.getSSEKMS.bind(storage, req, file),
-    storage.getContentEncoding.bind(storage, req, file)
+    storage.getContentEncoding.bind(storage, req, file),
+    storage.getChecksumAlgorithm.bind(storage, req, file)
   ], function (err, values) {
     if (err) return cb(err)

@@ -96,7 +98,8 @@ function collect (storage, req, file, cb) {
         replacementStream: replacementStream,
         serverSideEncryption: values[7],
         sseKmsKeyId: values[8],
-        contentEncoding: values[9]
+        contentEncoding: values[9],
+        checksumAlgorithm: values[10]
       })
     })
   })
@@ -181,6 +184,12 @@ function S3Storage (opts) {
     case 'undefined': this.getSSEKMS = defaultSSEKMS; break
     default: throw new TypeError('Expected opts.sseKmsKeyId to be undefined, string, or function')
   }
+  switch (typeof opts.checksumAlgorithm) {
+    case 'function': this.getChecksumAlgorithm = opts.checksumAlgorithm; break
+    case 'string': this.getChecksumAlgorithm = staticValue(opts.checksumAlgorithm); break
+    case 'undefined': this.getChecksumAlgorithm = defaultChecksumAlgorithm; break
+    default: throw new TypeError('Expected opts.checksumAlgorithm to be undefined, string, or function')
+  }
 }

 S3Storage.prototype._handleFile = function (req, file, cb) {
@@ -201,6 +210,9 @@ S3Storage.prototype._handleFile = function (req, file, cb) {
       SSEKMSKeyId: opts.sseKmsKeyId,
       Body: (opts.replacementStream || file.stream)    
     }
+    if (opts.checksumAlgorithm) {
+      params.ChecksumAlgorithm = opts.checksumAlgorithm
+    }

     if (opts.contentDisposition) {
       params.ContentDisposition = opts.contentDisposition
@@ -235,7 +247,8 @@ S3Storage.prototype._handleFile = function (req, file, cb) {
         metadata: opts.metadata,
         location: result.Location,
         etag: result.ETag,
-        versionId: result.VersionId
+        versionId: result.VersionId,
+        ...(opts.checksumAlgorithm && { checksum: result[`Checksum${opts.checksumAlgorithm}`] })        
       })
     })
   })

This issue body was partially generated by patch-package.