uppy/examples/digitalocean-spaces/server.cjs
Mikael Finstad 151da8bdfe
coalesce options bucket and getKey (#5169)
* coalesce options `bucket` and `getKey`

this is a breaking change!
also:
- remove `req` because it's an internal, unstable api that might change any time and should not be used directly
- use an params object for getKey/bucket functions instead of positional arguments, to make it more clean, and easier to add more data in the future
- add metadata to `bucket` whenever we are aware of it

* fix lint

* Apply suggestions from code review

Co-authored-by: Antoine du Hamel <antoine@transloadit.com>

* add back `Request` object

also add filename to `bucket` for consistency

* add migration steps

* fix lint

* Update docs/guides/migration-guides.md

Co-authored-by: Antoine du Hamel <antoine@transloadit.com>

---------

Co-authored-by: Antoine du Hamel <antoine@transloadit.com>
2024-06-13 23:32:38 +02:00

60 lines
2.2 KiB
JavaScript

const fs = require('node:fs')
const path = require('node:path')
const crypto = require('node:crypto')
require('dotenv').config({ path: path.join(__dirname, '..', '..', '.env') })
const app = require('express')()
const companion = require('../../packages/@uppy/companion')
/**
* Environment variables:
*
* - COMPANION_AWS_REGION - Your space region, eg "ams3"
* - COMPANION_AWS_KEY - Your access key ID
* - COMPANION_AWS_SECRET - Your secret access key
* - COMPANION_AWS_BUCKET - Your space's name.
*/
if (!process.env.COMPANION_AWS_REGION) throw new Error('Missing Space region, please set the COMPANION_AWS_REGION environment variable (eg. "COMPANION_AWS_REGION=ams3")')
if (!process.env.COMPANION_AWS_KEY) throw new Error('Missing access key, please set the COMPANION_AWS_KEY environment variable')
if (!process.env.COMPANION_AWS_SECRET) throw new Error('Missing secret key, please set the COMPANION_AWS_SECRET environment variable')
if (!process.env.COMPANION_AWS_BUCKET) throw new Error('Missing Space name, please set the COMPANION_AWS_BUCKET environment variable')
// Prepare the server.
const PORT = process.env.PORT || 3452
const host = `localhost:${PORT}`
const DATA_DIR = path.join(__dirname, 'tmp')
fs.mkdirSync(DATA_DIR, { recursive: true })
// Set up the /params endpoint that will create signed URLs for us.
app.use(require('cors')())
app.use(require('body-parser').json())
const { app: companionApp } = companion.app({
s3: {
// This is the crucial part; set an endpoint template for the service you want to use.
endpoint: 'https://{region}.digitaloceanspaces.com',
getKey: ({ filename }) => `${crypto.randomUUID()}-${filename}`,
key: process.env.COMPANION_AWS_KEY,
secret: process.env.COMPANION_AWS_SECRET,
bucket: process.env.COMPANION_AWS_BUCKET,
region: process.env.COMPANION_AWS_REGION,
},
server: { host },
filePath: DATA_DIR,
secret: 'blah blah',
debug: true,
})
app.use('/companion', companionApp)
require('vite').createServer({ clearScreen: false, server:{ middlewareMode: true } }).then(({ middlewares }) => {
app.use(middlewares)
app.listen(PORT, () => {
console.log(`Listening on http://localhost:${PORT}/...`)
})
})