@uppy/companion: fix default getKey for non-standalone too (#3945)

And update docs.
This commit is contained in:
Mikael Finstad 2022-08-04 15:09:53 +01:00 committed by GitHub
parent 96252e5bc5
commit 1a3276431f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 7 deletions

View file

@ -2,6 +2,8 @@ const fs = require('node:fs')
const path = require('node:path')
const budo = require('budo')
const router = require('router')
const crypto = require('node:crypto')
const companion = require('../../packages/@uppy/companion')
/**
@ -32,7 +34,7 @@ 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: (req, filename) => `uploads/${filename}`,
getKey: (req, filename) => `${crypto.randomUUID()}-${filename}`,
key: process.env.COMPANION_AWS_KEY,
secret: process.env.COMPANION_AWS_SECRET,

View file

@ -2,6 +2,7 @@ const ms = require('ms')
const fs = require('node:fs')
const { isURL } = require('validator')
const logger = require('../server/logger')
const { defaultGetKey } = require('../server/helpers/utils')
const defaultOptions = {
server: {
@ -13,7 +14,7 @@ const defaultOptions = {
endpoint: 'https://{service}.{region}.amazonaws.com',
conditions: [],
useAccelerateEndpoint: false,
getKey: (req, filename) => filename,
getKey: defaultGetKey,
expires: ms('5 minutes') / 1000,
},
allowLocalUrls: false,

View file

@ -164,3 +164,5 @@ module.exports.requestStream = async (req, convertResponseToError) => {
return { stream: resp }
}
module.exports.defaultGetKey = (req, filename) => `${crypto.randomUUID()}-${filename}`

View file

@ -72,7 +72,7 @@ const getConfigFromEnv = () => {
},
s3: {
key: process.env.COMPANION_AWS_KEY,
getKey: (req, filename) => `${crypto.randomUUID()}-${filename}`,
getKey: utils.defaultGetKey,
secret: getSecret('COMPANION_AWS_SECRET'),
bucket: process.env.COMPANION_AWS_BUCKET,
endpoint: process.env.COMPANION_AWS_ENDPOINT,

View file

@ -326,7 +326,7 @@ const options = {
},
},
s3: {
getKey: (req, filename, metadata) => filename,
getKey: (req, filename, metadata) => `${crypto.randomUUID()}-${filename}`,
key: '***',
secret: '***',
bucket: 'bucket-name',
@ -453,24 +453,26 @@ Get the key name for a file. The key is the file path to which the file will be
* `filename`, the original name of the uploaded file;
* `metadata`, user-provided metadata for the file. See the [`@uppy/aws-s3`](https://uppy.io/docs/aws-s3/#metaFields) docs. The `@uppy/aws-s3-multipart` plugin unconditionally sends all metadata fields, so they all are available here.
If your bucket is public, you should include a cryptographically random token in the uploaded name for security (hence the default `crypto.randomUUID()`).
This function should return a string `key`. The `req` parameter can be used to upload to a user-specific folder in your bucket, for example:
```js
app.use(authenticationMiddleware)
app.use(companion.app({
s3: {
getKey: (req, filename, metadata) => `${req.user.id}/${filename}`,
getKey: (req, filename, metadata) => `${req.user.id}/${crypto.randomUUID()}-${filename}`,
/* auth options */
},
}))
```
The default implementation returns the `filename`, so all files will be uploaded to the root of the bucket as their original file name.
The default implementation uploads all files to the root of the bucket as their original file name, prefixed with a random UUID.
```js
app.use(companion.app({
s3: {
getKey: (req, filename, metadata) => filename,
getKey: (req, filename, metadata) => `${crypto.randomUUID()}-${filename}`,
},
}))
```