useObjectPreview
Decide how a stored file shows in the browser, sign its inline URL, and read the start of a text file.
useObjectPreview tells you how to show one stored file without saving it:
as an image, a video, audio, a PDF, or text, or not at all. It signs the
inline URL and reads the start of a text file. For a ready-made view, use
ObjectPreview.
"use client";
import { useObjectPreview } from "@dimah-s3/react";
export function FilePreview({ objectKey }: { objectKey: string }) {
const preview = useObjectPreview({ route: "files", objectKey });
if (preview.status === "loading") return <p>Loading…</p>;
if (preview.status === "error") return <p>Could not load the preview.</p>;
if (!preview.url) return <p>No preview</p>;
switch (preview.kind) {
case "image":
return <img src={preview.url} alt="" />;
case "video":
return <video src={preview.url} controls preload="metadata" />;
case "audio":
return <audio src={preview.url} controls preload="metadata" />;
case "pdf":
return <iframe src={preview.url} title="Preview" />;
case "text":
return <pre>{preview.text?.value}</pre>;
default:
return <p>No preview</p>;
}
}How it decides
- A type you pass in
contentType, or one the file name gives (fileName, else the key's name), that has no preview, or whose kind is left out ofkinds, ends here:statusisreadywithkind: null, and nothing is signed. - Otherwise it signs an inline URL with
useObjectUrl. - The server keeps
inlineonly for a type on the route'sinlineTypes, and reports the stored type. An attachment has no preview. The type picks the kind. - For text, it reads at most
maxTextBytes(256 KiB) with one rangedGET.text.truncatedsays the file goes on.
| Kind | Types |
|---|---|
image | image/* |
video | video/* |
audio | audio/* |
pdf | application/pdf |
text | text/*, JSON |
resolvePreviewKind({ contentType, fileName }) from @dimah-s3/react/engine
maps a type to its kind, for a Preview button you show only when it applies.
Reading text uses fetch. In presign mode, the bucket's CORS rules must
allow GET from your app, as the provider guides set up.
Refresh
The URL refreshes before it expires, as in useObjectUrl, and a text file is
not read again. refresh() tries again: it reads a text file again, and signs
any other file again. Call it when an <img> or <video> fails, such as
after the URL expired while the tab slept.
API Reference
Options
| Prop | Type | Default | Description |
|---|---|---|---|
route | S3RouteName | required | Named server route |
objectKey | string | null | — | Stored object key. null, undefined, or "" is idle |
contentType | string | null | — | The file's type. A type without a preview signs nothing |
fileName | string | null | Key's name | Gives the type when nothing else does. Not sent |
kinds | ObjectPreviewKind[] | Every kind | Kinds to preview. Others are ready with kind: null |
maxTextBytes | number | 262144 | Most bytes read from a text file |
api | S3Api | — | Replaces the S3Provider API |
Return
| Property | Type | Description |
|---|---|---|
status | "idle" | "loading" | "ready" | "error" | Where the preview is |
kind | ObjectPreviewKind | null | How to show the file once ready. null for no preview |
url | string | null | The inline URL. null when nothing was signed |
contentType | string | null | The file's type, without parameters |
disposition | "inline" | "attachment" | null | The disposition url carries |
expiresAt | number | null | When url stops working, in ms since the epoch. null for proxy |
text | { value, truncated } | null | The start of a text file, once read |
error | APIError | null | Why status is error |
objectKey | string | null | The key this state belongs to |
refresh | () => void | Reads text again, or signs again |