initCompanion(options)
The initCompanion function initializes and configures the Uppy Companion middleware, which is essential for enabling users to import files from remote sources like Unsplash, Google Drive, Instagram, or direct URLs. This function is a wrapper around the official @uppy/companion library, tailored for seamless integration within a blocklet environment.
For a practical guide on setting this up, see Integrating Remote Sources (Companion).
How It Works#
Companion acts as a server-side proxy. When a user selects a file from a remote source, the request is sent to your backend's Companion endpoint. Your server then fetches the file from the remote source and streams it back to the user's browser. Once in the browser, the file is treated like a local file and uploaded to your final destination (e.g., the endpoint handled by initLocalStorageServer).
Usage#
To use Companion, initialize it with your configuration options and then attach its handle to an Express router path. The frontend Uploader component must be configured with the same path for its companionUrl prop.
Basic Companion Setup
import express from 'express';
import { initCompanion } from '@blocklet/uploader-server';
const router = express.Router();
// Basic configuration for Companion
const companion = initCompanion({
// A temporary directory on your server for processing files
path: '/tmp/uploads',
express,
// Provider options with necessary keys and secrets
providerOptions: {
unsplash: {
key: process.env.UNSPLASH_KEY,
secret: process.env.UNSPLASH_SECRET,
},
},
// The public URL of your blocklet, required by some providers
uploadUrls: [process.env.APP_URL],
});
// Mount the companion middleware on a specific path
// This path should match the `companionUrl` prop on the frontend
router.use('/companion', companion.handle);Parameters#
The initCompanion function accepts a single options object with the following properties:
Name | Type | Description |
|---|---|---|
|
| Required. The absolute path to a temporary directory on the server where files will be stored during processing. This corresponds to the |
|
| Required. The Express application instance. This is used to create the necessary sub-app and middleware stack for Companion. |
|
| Optional. An object containing the configuration for each remote provider you want to enable. Each key is the provider name (e.g., |
|
| Any other valid option from the official Uppy Companion options can be passed here. For example, |
Return Value#
The function returns a companion instance with the following key properties:
Property | Type | Description |
|---|---|---|
|
| An Express middleware that you must mount on a route (e.g., |
|
| A method that allows you to dynamically update the |
Example: Dynamic Provider Options#
You can change the provider options at runtime, which is useful for multi-tenant applications or when secrets are loaded asynchronously.
Dynamic Provider Options
// Initialize companion without provider options initially
const companion = initCompanion({
path: '/tmp/uploads',
express,
});
// Later, perhaps after fetching secrets from a database
async function updateCompanionConfig() {
const secrets = await getSecretsFromDb();
companion.setProviderOptions({
unsplash: {
key: secrets.unsplashKey,
secret: secrets.unsplashSecret,
},
});
}Additional Features#
- Status Code Rewriting: For security and better error handling, if a remote provider returns an error with a status code of 500 or higher, this middleware automatically rewrites it to
400 Bad Request. This prevents potential server error details from leaking to the client.
With Companion set up, your uploader is now capable of handling files from a wide variety of sources. You might also need to serve static files from your blocklet or other blocklets.