Skip to content

Commit 92f1494

Browse files
authored
💥 Upgrade to AWS JavaScript SDK V3 (#162)
Breaking Changes: When upgrading to this version, you also need to migrate from version 2 of the AWS JavaScript SDK to version 3. Instead of passing an instance of the old `aws.S3`, use the new `S3Client` class from `@aws-sdk/client-s3`. The minimum required version of Node.js has also been increased to 12.0.0, to align with the AWS JavaScript SDK.
1 parent 2718671 commit 92f1494

4 files changed

Lines changed: 34 additions & 27 deletions

File tree

README.md

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

33
Streaming multer storage engine for AWS S3.
44

5-
This project is mostly an integration piece for existing code samples from Multer's [storage engine documentation](https://github.com/expressjs/multer/blob/master/StorageEngine.md) with a call to `s3.upload` (see the [aws-sdk docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property)) as the substitution piece for file system. Existing solutions I found required buffering the multipart uploads into the actual filesystem which is difficult to scale.
5+
This project is mostly an integration piece for existing code samples from Multer's [storage engine documentation](https://github.com/expressjs/multer/blob/master/StorageEngine.md) with a call to S3 as the substitution piece for file system. Existing solutions I found required buffering the multipart uploads into the actual filesystem which is difficult to scale.
6+
7+
## AWS SDK Versions
8+
9+
3.x.x releases of multer-s3 use AWS JavaScript SDK v3. Specifically, it uses the Upload class from [@aws-sdk/lib-storage](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_lib_storage.html) which in turn calls the modular [S3Client](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/classes/s3client.html).
10+
11+
2.x.x releases for multer-s3 use AWS JavaScript SDK v2 via a call to [s3.upload](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property).
612

713
## Installation
814

@@ -13,15 +19,16 @@ npm install --save multer-s3
1319
## Usage
1420

1521
```javascript
16-
var aws = require('aws-sdk')
17-
var express = require('express')
18-
var multer = require('multer')
19-
var multerS3 = require('multer-s3')
22+
const { S3Client } = require('@aws-sdk/client-s3')
23+
const express = require('express')
24+
const multer = require('multer')
25+
const multerS3 = require('multer-s3')
2026

21-
var app = express()
22-
var s3 = new aws.S3({ /* ... */ })
27+
const app = express()
2328

24-
var upload = multer({
29+
const s3 = new S3Client()
30+
31+
const upload = multer({
2532
storage: multerS3({
2633
s3: s3,
2734
bucket: 'some-bucket',

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var stream = require('stream')
33
var fileType = require('file-type')
44
var htmlCommentRegex = require('html-comment-regex')
55
var parallel = require('run-parallel')
6+
var Upload = require('@aws-sdk/lib-storage').Upload
7+
var util = require('util')
68

79
function staticValue (value) {
810
return function (req, file, cb) {
@@ -207,13 +209,16 @@ S3Storage.prototype._handleFile = function (req, file, cb) {
207209
params.ContentEncoding = opts.contentEncoding
208210
}
209211

210-
var upload = this.s3.upload(params)
212+
var upload = new Upload({
213+
client: this.s3,
214+
params: params
215+
})
211216

212217
upload.on('httpUploadProgress', function (ev) {
213218
if (ev.total) currentSize = ev.total
214219
})
215220

216-
upload.send(function (err, result) {
221+
util.callbackify(upload.done.bind(upload))(function (err, result) {
217222
if (err) return cb(err)
218223

219224
cb(null, {

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"scripts": {
77
"test": "standard && mocha test/basic.js"
88
},
9+
"engines": {
10+
"node": ">= 12.0.0"
11+
},
912
"repository": {
1013
"type": "git",
1114
"url": "git+https://github.com/badunk/multer-s3.git"
@@ -23,13 +26,12 @@
2326
},
2427
"homepage": "https://github.com/badunk/multer-s3#readme",
2528
"dependencies": {
29+
"@aws-sdk/lib-storage": "^3.46.0",
2630
"file-type": "^3.3.0",
2731
"html-comment-regex": "^1.1.2",
2832
"run-parallel": "^1.1.6"
2933
},
3034
"devDependencies": {
31-
"aws-sdk": "^2.2.32",
32-
"concat-stream": "^1.5.1",
3335
"express": "^4.13.1",
3436
"form-data": "^1.0.0-rc3",
3537
"mocha": "^2.2.5",

test/util/mock-s3.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
var events = require('events')
2-
var concat = require('concat-stream')
32

43
function createMockS3 () {
5-
function upload (opts) {
4+
function send (opts, cb) {
65
var ee = new events.EventEmitter()
7-
8-
ee.send = function send (cb) {
9-
opts['Body'].pipe(concat(function (body) {
10-
ee.emit('httpUploadProgress', { total: body.length })
11-
cb(null, {
12-
'Location': 'mock-location',
13-
'ETag': 'mock-etag'
14-
})
15-
}))
16-
}
17-
18-
return ee
6+
var buffer = opts['input']['Body']
7+
ee.emit('httpUploadProgress', { total: buffer.length })
8+
return Promise.resolve({
9+
Location: 'mock-location',
10+
ETag: 'mock-etag'
11+
})
1912
}
2013

21-
return { upload: upload }
14+
return { send: send }
2215
}
2316

2417
module.exports = createMockS3

0 commit comments

Comments
 (0)