uppy/examples/aws-nodejs/index.js
Raúl Ibáñez 3f07d79de6
Add example for Uppy with S3 and a Node.js server (#4129)
Co-authored-by: Merlijn Vos <merlijn@soverin.net>
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
2022-10-17 16:43:00 -03:00

53 lines
1.2 KiB
JavaScript

require('dotenv').config({ path: path.join(__dirname, '..', '..', '.env') })
const express = require('express')
const app = express()
const path = require('node:path')
const port = process.env.PORT
const bodyParser = require('body-parser')
const aws = require('aws-sdk')
app.use(bodyParser.json())
app.get('/', (req, res) => {
const htmlPath = path.join(__dirname, 'public', 'index.html')
res.sendFile(htmlPath)
})
app.get('/drag', (req, res) => {
const htmlPath = path.join(__dirname, 'public', 'drag.html')
res.sendFile(htmlPath)
})
app.post('/sign-s3', (req, res) => {
const s3 = new aws.S3()
const fileName = req.body.filename
const { contentType } = req.body
const s3Params = {
Bucket: process.env.S3_BUCKET,
Key: fileName,
Expires: 60,
ContentType: contentType,
ACL: 'public-read',
}
s3.getSignedUrl('putObject', s3Params, (err, data) => {
if (err) {
console.log(err)
return res.end()
}
const returnData = {
url: data,
method: 'PUT',
}
res.write(JSON.stringify(returnData))
res.end()
})
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})