IMPORTANT: This repository has moved to @mjackson/remix-the-web
file-storage
is a key/value interface for storing File
objects in JavaScript. Similar to how localStorage
allows you to store key/value pairs of strings in the browser, file-storage
allows you to store key/value pairs of files on the server.
- Simple, intuitive key/value API (like Web Storage, but for
File
s instead of strings) - A generic
FileStorage
interface that works for various large object storage backends (can be adapted to AWS S3, Cloudflare R2, etc.) - Support streaming file content to and from storage
- Preserves all
File
metadata includingfile.name
,file.type
, andfile.lastModified
Install from npm:
npm install @mjackson/file-storage
import { LocalFileStorage } from "@mjackson/file-storage/local";
let storage = new LocalFileStorage("./user/files");
let file = new File(["hello world"], "hello.txt", { type: "text/plain" });
let key = "hello-key";
// Put the file in storage.
await storage.set(key, file);
// Then, sometime later...
let fileFromStorage = await storage.get(key);
// All of the original file's metadata is intact
fileFromStorage.name; // 'hello.txt'
fileFromStorage.type; // 'text/plain'
// To remove from storage
await storage.remove(key);
The FileStorage
interface allows you to implement your own file storage for custom storage backends:
import { type FileStorage } from "@mjackson/file-storage";
class CustomFileStorage implements FileStorage {
/**
* Returns `true` if a file with the given key exists, `false` otherwise.
*/
has(key: string): boolean | Promise<boolean> {
// ...
}
/**
* Puts a file in storage at the given key.
*/
set(key: string, file: File): void | Promise<void> {
// ...
}
/**
* Returns the file with the given key, or `null` if no such key exists.
*/
get(key: string): File | null | Promise<File | null> {
// ...
}
/**
* Removes the file with the given key from storage.
*/
remove(key: string): void | Promise<void> {
// ...
}
}
form-data-parser
- Pairs well with this library for storingFileUpload
objects received inmultipart/form-data
requestslazy-file
- The streamingFile
implementation used internally to stream files from storage
See LICENSE