import tusboy from 'tusboy'
import s3store from 's3-tus-store'
import aws from 'aws-sdk'
import { Passthrough } from 'stream'
import express from 'express'
const bucket = 'my-bucket'
const client = new aws.S3({
accessKeyId: process.env.S3_ACCESS_KEY,
secretAccessKey: process.env.S3_SECRET_KEY
})
const store = s3store({ client, bucket })
const app = express()
app
.use('/:username/avatar', new express.Router({ mergeParams: true })
.get('/', (req, res, next) => {
const key = `users/${req.params.username}/avatar-resized`
const rs = store.createReadStream(key, ({ contentLength, metadata }) => {
res.set('Content-Type', metadata.contentType)
res.set('Content-Length', contentLength)
rs.pipe(res)
}).on('error', next)
})
.use('/upload', tusboy(store, {
getKey: (req) => {
const key = `users/${req.params.username}/avatar`
return key
},
beforeComplete: async (req, upload, uploadId) => {},
afterComplete: async (req, upload, completedUploadId) => {
const key = `users/${req.params.username}/avatar`
console.log(`Completed upload ${completedUploadId}`)
const rs = store.createReadStream(key)
const resizedKey = `users/${req.params.username}/avatar-resized`
const { uploadId } = await store.create(resizedKey, {
metadata: upload.metadata,
uploadLength: upload.uploadLength,
})
await store.append(uploadId, rs)
},
}))
)
app.listen(3000)