Preview S3 files in the browser instead of downloading them

by 11 min read
A plain bucket link that triggers a download beside a signed link whose inline headers open the file in the browser
On this page

A link to a file in your S3 bucket downloads it, when you only wanted to view the S3 file without downloading it. It is written for beginners, self-hosters and small teams that keep images, PDFs, media or spreadsheets in an S3, R2, B2 or MinIO bucket. Two response headers decide, and you can change those on the stored file, on one link, or by previewing in Opndrive.

Key takeaways

  • A browser opens a file only when its Content-Type is a real type, like application/pdf, and its Content-Disposition is not attachment.
  • Files uploaded without a type get a generic one, and browsers save generic files on purpose, as a safety default.
  • In Opndrive, double-click a file, or choose More actions → Open with → Preview, to view spreadsheets, code, audio, video, PDFs and images.
  • Previewing still sends the file’s bytes to your browser, so it counts toward egress.

On this page

Two headers decide whether a file opens or saves

A browser shows a file only when the response says what the file is and does not ask for it to be saved. Content-Type must be a real type, like image/png or application/pdf, and Content-Disposition must be inline or missing. A generic type or attachment makes the browser save the file.

A header is a label the server sends with the file. Content-Type, also called the MIME type, names the format; Content-Disposition is inline (show it) or attachment (download it), per MDN. The generic type application/octet-stream means “Any kind of binary data”, and browsers download it “to avoid executing scripts in the serving site’s origin” (MDN[1]): a safety default, not a bug.

Plain S3 link returns stored headers and downloads; signed link with inline override returns headers that display the file
On Amazon S3, a plain link gets the stored headers, while a signed link can ask for others, and they show on a 200 response.

How files end up with a generic type

Usually the program that uploaded the file sent no type. We hit this ourselves: Opndrive’s default-mode multipart uploads sent no Content-Type, so S3 stored the files as binary/octet-stream, and a shared link to a photo downloaded it instead of showing it. Opndrive’s uploads now send the file’s type.

Backblaze B2’s native upload with the type b2/x-auto stores a file as application/octet-stream when its extension is missing or unknown (Backblaze[2]).

Two ways to change what the browser does

You can fix the type stored on the object, which fixes every link (steps further down), or override the headers on one signed link. A presigned URL is an address with a time-limited permission built in. On Amazon S3, a GetObject request can carry response-content-type and response-content-disposition, which replace those headers without touching the object. They are allowed only on signed requests, never on “an unsigned (anonymous) request”, and show only on a 200 OK response (AWS GetObject[3]).

Only AWS documents both overrides. Backblaze’s S3 Get Object reference lists response-content-type but not response-content-disposition (Backblaze[4]), Cloudflare’s R2 presigned URL page mentions neither (Cloudflare[5]), and our search found no MinIO page. We did not test these providers, so check the headers yours returns.

What you need before you preview files

Three things come first: an access key, Opndrive in a browser, and a CORS rule on the bucket.

  • An access key, the ID and secret that let a program use your bucket. Opndrive lists and downloads with GET and reads file details with HEAD (Opndrive docs), so a read-only key for one bucket is enough to view files; uploads and deletes fail with it. See the minimum IAM policy for an S3 web client (IAM sets what a key may do).
  • Opndrive at opndrive.app, or your own copy with the command below, on port 3000.
  • A CORS rule (cross-origin resource sharing), which tells browsers which websites may call the bucket. Opndrive “runs in your browser and talks to your storage directly”, so the bucket must allow its address.
Bash
docker run -d --restart unless-stopped --name opndrive -p 3000:3000 ghcr.io/opndrive/opndrive:latest

Previews need that rule too. The code and spreadsheet viewers load the file with a script, and browsers restrict requests that a script makes to another website (MDN). Images and media that are only displayed “may not require CORS”, per that page. That is our reading, not a test, but it explains photos showing while a CSV fails.

Add the CORS rule in the Amazon S3 console

These steps and the rule come from Opndrive’s CORS rules for each provider:

  1. In the Amazon S3 console, go to General purpose buckets, then pick your bucket.
  2. Open the Permissions tab.
  3. Under Cross-origin resource sharing (CORS), click Edit.
  4. Paste the rule below and click Save changes.
JSON
[
  {
    "AllowedHeaders": [
      "*"
    ],
    "AllowedMethods": [
      "GET",
      "PUT",
      "POST",
      "DELETE",
      "HEAD"
    ],
    "AllowedOrigins": [
      "https://opndrive.app"
    ],
    "ExposeHeaders": [
      "ETag"
    ],
    "MaxAgeSeconds": 3000
  }
]

If you self-host, put your own address in AllowedOrigins. GET and HEAD cover viewing; PUT, POST and DELETE are only for uploads, renames and deletes. Without a working rule, connecting fails with “Could not reach your storage”.

The rule grants no access by itself: every request is still signed with your key. A wildcard origin (*), though, lets scripts on any website use a key or link that leaks, so list your exact address.

Cloudflare R2 wants the allowed headers listed by name, Backblaze B2 takes the rule through its S3-compatible API, and MinIO Community accepts every origin by default. CORS for S3, R2, MinIO, B2 and Wasabi in one guide covers each, and you can test the CORS preflight with curl if a preview fails with a CORS error.

Use Opndrive to view an S3 file without downloading it

Once the bucket is connected, a preview is a double-click away. Each step says what you should see.

  1. Connect your bucket. On /connect, pick your provider, fill in the keys, bucket, region, endpoint and a connection name, and choose Connect to your S3 storage. You land on Home, with your folders under Suggested Folders; My Drive in the sidebar shows the whole bucket.
  2. Go to the file. Double-click folders to open them; the breadcrumbs lead back.
  3. Open the preview. Double-click the file, or choose More actions → Open with → Preview. The preview fills the screen.
  4. Move or close. Previous file and Next file (← and →) step through the folder; Close preview (Esc) returns to the list.
  5. Open it in a tab (optional). More actions → Open with → Open in new tab gives an address ending in #preview= and the file’s path.
  6. Check the stored type (optional). More actions → File information shows the Content Type; binary/octet-stream there means plain links will download the file.

Opndrive file menu with Open with, Download, Share, Rename, File information and Delete forever
Open with is the first item; its submenu holds Preview and Open in new tab.

Opndrive details panel listing a file's type, size, content type, ETag and storage class
The Content Type line is what a plain link to this file sends. Here beach.jpg is stored as application/x-www-form-urlencoded instead of image/jpeg; a generic type such as binary/octet-stream means plain links download it.

Your keys stay in your browser’s localStorage, requests go straight to your provider, not through an Opndrive server, and Clear Session removes saved connections. The step 5 tab is not a link for sharing: the path after # never reaches a server, and the page opens only where the same bucket is connected. For a link anyone can open, see share a file from S3 with a link that expires.

What happens behind the scenes

Each preview uses a presigned link that asks for inline and a Content-Type chosen from the file’s extension, so previews work even when the stored type is wrong. The link lasts one hour; an unknown extension gets application/octet-stream. If a preview looks wrong, check step 6.

File types, controls and size limits

Opndrive previews six kinds of file, each up to a size limit set in its code:

File type What you get Preview limit
Images Zoom (buttons or mouse wheel), Fit to Screen, Rotate, Actual Size, Reset 25 MB
PDF One page at a time, page buttons, zoom from 25% to 400% 50 MB
Video The browser’s own player 100 MB
Audio Opndrive’s own player: play, pause, restart, mute, volume and a seek bar 50 MB
Text and code Read-only plain text, first 1,000 lines 5 MB
Spreadsheets (CSV and Excel files) Read-only first sheet, up to 100 rows and 20 columns 10 MB

Bigger files show File Size Too Large with a Download File button; other types show File Type Not Supported. A video the browser can’t play shows “The video format may not be supported by your browser” and Retry.

A demo photo open in Opndrive's preview with zoom, fit, rotate and actual-size controls
Image previews zoom with the mouse wheel or the toolbar, which also fits, rotates, shows actual size and resets.

Previewing still transfers the file

“Without downloading” means without saving a copy. The bytes still travel to your browser, so where a provider charges for egress (data sent out of its network), a preview counts toward it just as a download does.

Gaps in Opndrive’s preview

Opndrive has no preview for Word, PowerPoint, archives or executables, and edits nothing: the viewers are read-only, with no syntax highlighting or rendered Markdown. Search matches names only, since “S3 has no built-in way to search file contents” (Opndrive docs). There are no user accounts or permissions; it is an object browser, not an admin console. For those needs, open the file in a desktop app, convert Office files to PDF before upload, or use a backend (below).

To make every link show the file, public ones included, fix the type stored on the object. In the Amazon S3 console, you change an object’s metadata by copying the object onto itself with the new metadata (AWS):

  1. From the navigation pane on the left, go to General purpose buckets, then pick your bucket.
  2. Select the check box for the object.
  3. On the Actions menu, choose Copy.
  4. Choose Browse S3, select the same folder as the source, and choose Choose destination.
  5. Under Additional copy settings, choose Specify settings, then under Metadata choose Replace all metadata.
  6. Choose Add metadata, and set Type to System-defined, Key to Content-Type and Value to the real type, for example application/pdf. Add Content-Disposition with inline the same way if you like.
  7. Choose Copy.

In Opndrive, File information should now show the new type.

The new metadata replaces the old, so add back anything you want to keep; the console route works for objects under 5 GB; and with S3 Versioning on, the old object stays as an older version.

Better, set the type at upload: PutObject takes a Content-Type (AWS PutObject[6]).

Backend, CDN or reverse proxy instead

Opndrive and presigned links send your browser straight to the bucket, which needs a CORS rule, puts keys or signed links in the viewer’s browser, and shows the bucket’s address. Switch when one of these fits better:

  • Backend API: when viewers must never hold keys, need per-user permissions, or files need scanning or converting first. The cost: your server carries every byte.
  • CDN (CloudFront, Cloudflare): for public images and video, with caching and your own domain. Visitors get what the CDN serves, not per-link overrides, so fix the stored type first; R2 presigned URLs also “cannot be used with custom domains” (Cloudflare).
  • Reverse proxy (Nginx, Caddy, Traefik): for adding TLS to a self-hosted storage server. It must pass the original Host header through, because the signature covers it, or signed links fail with SignatureDoesNotMatch; for the fix, read how to sign presigned URLs for an address the browser can reach.

A follow-up post covers signing a link in code so it opens the file, with a boto3 example, and how to check the headers your provider returns.

References

  1. MDN
  2. Backblaze
  3. AWS GetObject
  4. Backblaze
  5. Cloudflare
  6. AWS PutObject

Try Opndrive

Opndrive gives any S3-compatible bucket a Drive-like web interface you host yourself.
See it on GitHub or read the docs.

Curious about Opndrive?

Take a look at our FAQ to learn more.

Opndrive

Ready to take control of your S3 storage?

Connect your S3 bucket in minutes and start managing your files with Opndrive's beautiful, modern interface.

Build Opndrive with us

Live demos, reviews on your PRs, and a hand when you get stuck.

Be an early member
Join our Discord