I have a small @tus/node-server with the @tus/s3-store to upload files to a Tigris bucket.
I’ve followed the documentation to specify a custom path on the storage based on the userId.
The files are properly being uploaded to the right location in my bucket.
I’m using Uppy (React) with the Tus uploader.
How can I build a URL for the uploaded file and read that in the upload-success event on the client? This event gets an uploadURL which is the URL of the specific Tus server request, not the actual file URL on the destination (the Tigris bucket).
import { Server } from '@tus/server'
import { S3Store } from '@tus/s3-store'
import { consola } from "consola";
import { nanoid } from "nanoid"
import invariant from 'tiny-invariant';
const host = process.env.HOST || '127.0.0.1'
const port = process.env.PORT || 8080
invariant(process.env.S3_BUCKET, "S3_BUCKET is required")
invariant(process.env.S3_ACCESS_KEY_ID, "S3_ACCESS_KEY_ID is required")
invariant(process.env.S3_SECRET_ACCESS_KEY, "S3_SECRET_ACCESS_KEY is required")
const S3_ENDPOINT = process.env.S3_ENDPOINT || "https://fly.storage.tigris.dev"
const S3_BUCKET = process.env.S3_BUCKET
const server = new Server({
path: '/files',
datastore: new S3Store({
s3ClientConfig: {
endpoint: S3_ENDPOINT,
region: "auto",
bucket: S3_BUCKET,
credentials: {
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
}
}
}),
namingFunction(req, metadata) {
invariant(metadata?.filename, "filename is required")
invariant(metadata?.userId, "userId is required")
const id = nanoid()
const userId = metadata.userId;
const filename = metadata.filename;
const extension = filename.split('.').pop();
return `${userId}/${id}.${extension}`
},
generateUrl(req, { proto, host, path, id }) {
id = Buffer.from(id, 'utf-8').toString('base64url')
return `${proto}://${host}${path}/${id}`
},
getFileIdFromRequest(req, lastPath) {
// lastPath is everything after the last `/`
// If your custom URL is different, this might be undefined
// and you need to extract the ID yourself
return Buffer.from(lastPath ?? "", 'base64url').toString('utf-8')
},
onUploadFinish: async (req, res, upload) => {
consola.info(`Upload finished: ${upload.id}`)
// Is this where I can build the file url? If so how?
// I want to be able to build the file URL like:
// const url = `${S3_ENDPOINT}/${S3_BUCKET}/${pathToFile}`
// And get that in the `upload-success` (or any similar event) on Uppy.js
return res
}
})
consola.info(`Server is running on ${host}:${port}`)
server.listen({ host, port })