Skip to Content
dimah-s3v2.8.1

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.

components/file-preview.tsx
"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

  1. 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 of kinds, ends here: status is ready with kind: null, and nothing is signed.
  2. Otherwise it signs an inline URL with useObjectUrl.
  3. The server keeps inline only for a type on the route's inlineTypes, and reports the stored type. An attachment has no preview. The type picks the kind.
  4. For text, it reads at most maxTextBytes (256 KiB) with one ranged GET. text.truncated says the file goes on.
KindTypes
imageimage/*
videovideo/*
audioaudio/*
pdfapplication/pdf
texttext/*, 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

PropTypeDefaultDescription
routeS3RouteNamerequiredNamed server route
objectKeystring | null—Stored object key. null, undefined, or "" is idle
contentTypestring | null—The file's type. A type without a preview signs nothing
fileNamestring | nullKey's nameGives the type when nothing else does. Not sent
kindsObjectPreviewKind[]Every kindKinds to preview. Others are ready with kind: null
maxTextBytesnumber262144Most bytes read from a text file
apiS3Api—Replaces the S3Provider API

Return

PropertyTypeDescription
status"idle" | "loading" | "ready" | "error"Where the preview is
kindObjectPreviewKind | nullHow to show the file once ready. null for no preview
urlstring | nullThe inline URL. null when nothing was signed
contentTypestring | nullThe file's type, without parameters
disposition"inline" | "attachment" | nullThe disposition url carries
expiresAtnumber | nullWhen url stops working, in ms since the epoch. null for proxy
text{ value, truncated } | nullThe start of a text file, once read
errorAPIError | nullWhy status is error
objectKeystring | nullThe key this state belongs to
refresh() => voidReads text again, or signs again

On this page