Folders & prefix navigation
Object stores have no real folders — only keys. @nestjs/storage lists flat; how media turns that into one navigable level at a time (listFolderLevel / foldListing / the S3 side-car), and the dashboard folder feature (create, delete, move, copy) built on top of it.
An object store (S3, and the local disk that mimics it) has no folders. There is only a flat map of keys to bytes — photos/2024/paris/eiffel.jpg is one opaque string, not a paris directory inside a 2024 directory. The slashes are a convention.
@nestjs/storage lists exactly that: disk.list({ prefix, cursor, limit }) and disk.listAll({ prefix }) return every key under the prefix, nested included, in byte order. There is no delimiter. Yet every file browser — including this library's console — shows folders you can open. media adds the one-level view in -core.
One folder level
import { listFolderLevel, foldListing } from '@dudousxd/nestjs-media-core';
interface FolderListing {
folders: string[]; // sub-folder prefixes directly under `prefix`, each ending in '/'
files: StorageListEntry[]; // @nestjs/storage's entries directly under `prefix`
cursor?: string; // pass it back for the next page
}
// The side-car's native delimiter listing when there is one, else foldListing():
const level = await listFolderLevel(disk, 'photos/', { limit: 50 }, multipart.s3);Given these keys:
photos/logo.png
photos/2023/summer/a.jpg
photos/2023/winter/b.jpg
photos/2024/paris/eiffel.jpglisting photos/ gives folders: ['photos/2023/', 'photos/2024/'] and the one file directly there, photos/logo.png. prefix is '' (the root) or ends with /; core's folderPrefix('photos') normalizes it.
Page size
Both paths below use the same page size: limit, or DEFAULT_FOLDER_LISTING_LIMIT (50) when you pass none, capped at MAX_FOLDER_LISTING_LIMIT (1000, S3's own cap per request). A limit that is not a positive integer is a RangeError. folderListingLimit(limit) resolves it the same way. A page holds at most limit entries; while it has a cursor it can hold fewer (S3 leaves out a folder's marker object), so page on the cursor, not on the count.
A cursor that is not one a previous page returned throws InvalidListingCursorError (status 400).
On S3: the side-car asks the store
S3 can roll keys up itself (ListObjectsV2 with Delimiter: '/', returning CommonPrefixes), so browsing a huge bucket costs one request per level. S3Disk doesn't expose that; the s3Multipart() side-car does, as listFolders(prefix, { cursor, limit }), and listFolderLevel() uses it when the disk has one.
Everywhere else: folding the flat listing
foldListing(disk, prefix, { cursor, limit, pageSize, maxPages }) reads the flat listing and folds it: a key directly under the prefix is a file, a deeper one contributes its first segment as a folder. Keys come in byte order, so all the keys of a folder are contiguous: each folder is returned once, and a page that ends inside a folder carries it in its cursor so the next page skips the rest. The cursor is opaque — it wraps the disk's cursor.
The fold reads every key below the prefix, so a folder holding millions of keys costs that many keys to step over; maxPages (default 20 disk pages) bounds one call, which then returns what it has and a cursor. Fine for local and in-memory disks and moderate buckets; configure the side-car for big ones.
A zero-byte prefix/ marker object that other tools write is not returned as a file. @nestjs/storage itself can't create one: keys can't end with /.
The whole subtree
For a size total or a bulk action, you want every key, not one level: that's plain disk.listAll({ prefix: 'photos/2024/' }).
The dashboard folder feature
Because there are no folders, "create a folder" can't mean "make a directory". The console writes a hidden, zero-byte <prefix>/.keep file: the folder then has a key, so listings show it, empty. (The S3 habit of a prefix/ marker object is an invalid key under @nestjs/storage.) .keep files are hidden from file listings. MediaConsoleService owns the four folder operations:
| Method | Behavior |
|---|---|
createFolder(disk, prefix) | Writes <prefix>/.keep. Leading and trailing slashes are trimmed; an empty name or a path no disk accepts is a 400. |
deleteFolder(disk, prefix) | Deletes every key under <prefix>/ (nested included, .keep too) from a flat listAll(), in delete([...]) batches of 1000 — one DeleteObjects request each on S3. |
moveFolder(fromDisk, from, toDisk, to) | Relocates every key under <from>/ to <to>/, keeping each key's path relative to the folder. |
copyFolder(fromDisk, from, toDisk, to) | Same as move, but leaves the source in place. |
Same-disk moves and copies use the disk's own copy/move (server-side on S3, a rename on a local disk). A cross-disk transfer streams each object (get().body into put()), keeping its content type, cache control, disposition and metadata, with no size limit and nothing buffered. A move into a destination inside its own source is rejected on the same disk (it would recurse forever).
Legacy marker objects
prefix/ markers already in a bucket (from the old S3 driver or another tool) still show as folders, but @nestjs/storage can't address them: a folder delete skips them. Remove them with a lifecycle rule or the S3 CLI.
Dashboard REST folder routes
The folder operations are reachable over the console's JSON API. All are on the actions controller (present only with actions: true) and return 204 No Content. The disk in the path is the source disk; the JSON body's optional toDisk names the destination for a cross-disk transfer (omit it to stay on the same disk).
| Method + path | Body / query | Service call |
|---|---|---|
POST disks/:disk/folder | { "prefix": "invoices/2024" } | createFolder |
DELETE disks/:disk/folder?prefix=… | prefix query param | deleteFolder |
POST disks/:disk/move-folder | { "from", "to", "toDisk"? } | moveFolder |
POST disks/:disk/copy-folder | { "from", "to", "toDisk"? } | copyFolder |
# Create a folder
curl -X POST https://app.example.com/media/api/disks/s3/folder \
-H 'Content-Type: application/json' \
-d '{"prefix":"invoices/2024"}'
# Move a folder to another disk, keeping its subtree layout
curl -X POST https://app.example.com/media/api/disks/s3/move-folder \
-H 'Content-Type: application/json' \
-d '{"from":"invoices/2024","to":"archive/2024","toDisk":"cold"}'
# Recursively delete a folder and everything under it
curl -X DELETE 'https://app.example.com/media/api/disks/s3/folder?prefix=invoices/2024'From the browser, the typed console client wraps every one of these:
import { mediaConsoleClient } from '@dudousxd/nestjs-media-dashboard/client';
await mediaConsoleClient.createFolder('s3', 'invoices/2024');
await mediaConsoleClient.moveFolder('s3', 'invoices/2024', 'cold', 'archive/2024');
await mediaConsoleClient.deleteFolder('s3', 'invoices/2024');Building your own folder browser
You don't need the console — -core is enough to build navigation into your own admin:
List the current level
Start at the root ('') or wherever the user is. Render folders as clickable rows and files as leaves.
const { folders, files, cursor } = await listFolderLevel(disk, prefix, { limit: 50 }, sidecar);Descend on click
A folder is already a prefix — pass it back as is.
Page and sweep as needed
Follow cursor for more of a busy folder; use disk.listAll({ prefix }) when you need the whole subtree.