Back to Keystone Classic

File system storage adapter

lib/storage/adapters/fs/README.md

4.2.11.8 KB
Original Source

File system storage adapter

This adapter is used for storing uploaded files on your server's local filesystem.

Usage

Configure the FS adapter:

JS
var storage = new keystone.Storage({
	adapter: keystone.Storage.Adapters.FS,
	fs: {
		path: keystone.expandPath('./uploads'), // required; path where the files should be stored
  		publicPath: '/public/uploads', // path where files will be served
	}
});

Then use it as a storage provider for a File field:

JS
MyList.add({
	file: { type: Types.File, storage: myStorage },
});

Options

The adapter requires an additional fs field added to the storage options. it accepts the following values:

  • path: (string; required) Path the files will be stored at on disk
  • generateFilename: (function; default: random filename) Method to generate a filename for the uploaded file. Gets passed the file data, the attempt number and the callback to call with the filename.
    • See keystone-storage-namefunctions for additional filename generators, including content hash filename and original filename. See its source for more information on how to write your own.
  • whenExists: (string; default: 'retry') Specifies what to do when the file exists already. Can be one of 'retry', 'error' or 'overwrite'.
  • retryAttempts: (number; default: 3) If whenExists is set to 'retry', how many times keystone should try to generate a unique filename before returning an error
  • publicPath: (string) Optional path the files will served from by the webserver

Schema

The FS adapter supports all the default Keystone file schema fields. It also additionally supports and enables the filename path (required).

JS
{
	filename: String,
}