Skip to Content
dimah-s3v2.8.1
Share Links

Share Links

The share() plugin gives db() objects and folders links anyone can open, with expiry, passwords, download limits, and revocation.

@dimah-s3/share adds share links on top of db(). An owner creates a link to one file or one folder of their scope. Anyone who holds the link can open it, browse the folder, and download files, without a session, until it expires, reaches its download limit, or the owner turns it off.

const { link } = await s3Client.share.createLink({
  target: { type: "object", key },
  expiresIn: 7 * 24 * 60 * 60, // seconds, by the server's clock
  password: "correct horse",
  maxDownloads: 10,
});

link.url; // "https://app.example.com/s/k3Jx…"

How it works

  • Ownership comes from db(). Owner actions resolve the caller with your resolveScope, and your db authorize sees them as operation: "plugin". You configure scopes once.
  • The token is the credential. A link URL carries 192 random bits. Visitor calls send it instead of a session; the object key never leaves the server.
  • Every visit is checked again. Each call reloads the link and its target: a revoked or expired link, a deleted file, or a file moved out of a shared folder stops opening at once.
  • Downloads follow the route. On a route in presign mode a visitor gets a presigned URL that lives 5 minutes, capped by the route. In proxy mode they get a signed /share/file URL, as short-lived, that streams through your server.
  • Passwords never travel twice. unlockLink checks the password (PBKDF2-SHA256) once and returns an access proof for later calls. A new password ends every proof made under the old one.

Setup

examples/with-db runs this setup with a share dialog and a public page.

Install

npm i @dimah-s3/share

It needs @dimah-s3/db and @dimah-s3/server at the same version. The adapters come from @dimah-s3/db/adapters/*.

Create the table

The plugin stores links in share_link, and its schema version in private_dimah_s3_share_settings, beside the db tables.

scripts/dimah-s3-share.ts
import { drizzleAdapter } from "@dimah-s3/db/adapters/drizzle";
import { DimahS3Share } from "@dimah-s3/share";
import { runCli } from "@dimah-s3/share/cli";

await runCli(
  DimahS3Share.client(drizzleAdapter({ db: undefined, provider: "sqlite" })),
);
npx tsx scripts/dimah-s3-share.ts generate latest -o db/dimah-s3-share.ts
npx drizzle-kit push

Add the new file to schema in drizzle.config.ts.

With a PostgreSQL schema, such as DimahS3Share.names.schema("storage"), two generated files would each declare storageSchema, and drizzle-kit stops at "There's a duplicate schema name". Pass both clients to runCli from @dimah-s3/db/cli instead, which generates them into one file and declares the schema once:

scripts/dimah-s3.ts
import { DimahS3Db } from "@dimah-s3/db";
import { drizzleAdapter } from "@dimah-s3/db/adapters/drizzle";
import { runCli } from "@dimah-s3/db/cli";
import { DimahS3Share } from "@dimah-s3/share";

const adapter = drizzleAdapter({ db: undefined, provider: "postgresql" });
await runCli([
  DimahS3Db.names.schema("storage").client(adapter),
  DimahS3Share.names.schema("storage").client(adapter),
]);
npx tsx scripts/dimah-s3.ts generate latest -o db/dimah-s3.ts
npx drizzle-kit push

Then list only db/dimah-s3.ts in drizzle.config.ts, and pass its relations to drizzle() as they are.

Create the client

Use the adapter you gave db(). With Drizzle, merge the relations of both generated files into one instance:

lib/dimah-s3-db.ts
import { DimahS3Db } from "@dimah-s3/db";
import { drizzleAdapter } from "@dimah-s3/db/adapters/drizzle";
import { DimahS3Share } from "@dimah-s3/share";
import { drizzle } from "drizzle-orm/better-sqlite3";
import { relations as dbRelations } from "../db/dimah-s3";
import { relations as shareRelations } from "../db/dimah-s3-share";

export const db = drizzle({
  client: sqlite,
  relations: { ...dbRelations, ...shareRelations },
});

const adapter = drizzleAdapter({ db, provider: "sqlite" });
export const dimahS3Db = DimahS3Db.client(adapter);
export const dimahS3Share = DimahS3Share.client(adapter); 

Register the plugin

Pass share() after db(). url turns a token into the page visitors open, which you build in The shared page:

lib/s3.ts
import { db } from "@dimah-s3/db";
import { share } from "@dimah-s3/share";

export const s3 = dimahS3({
  // client, bucket, routes…
  plugins: [
    db({ client: dimahS3Db, getSession, resolveScope }),
    share({
      client: dimahS3Share,
      url: (token) => `${process.env.APP_URL}/s/${token}`,
    }),
  ],
});

A route needs download for its files to be shared.

Let visitors through your guard

Visitors have no session. If your instance guard requires one, let the public share actions through with isSharePublicAction:

lib/s3.ts
import { isSharePublicAction } from "@dimah-s3/share";

dimahS3({
  guard: async (context) => {
    if (isSharePublicAction(context)) return; 
    await requireSession(context.request);
  },
});

Rate-limit unlockLink

The password hash slows each guess, but nothing stops a script from trying many. Limit unlockLink per IP in your guard (context.plugin?.action === "unlockLink"), and refuse with throw s3Error("TOO_MANY_REQUESTS"), which useFormatError words for the visitor.

Add the client plugin

lib/s3-client.ts
"use client";

import { dbClient } from "@dimah-s3/db/client";
import { createS3Client } from "@dimah-s3/react";
import { shareClient } from "@dimah-s3/share/client";

export const s3Client = createS3Client({
  plugins: [dbClient(), shareClient()],
});

The owner actions run as the signed-in caller, on s3Client.share:

// Every link to one file, newest first.
const { links } = await s3Client.share.listLinks({
  target: { type: "object", key },
});

// New settings. `null` removes a password, an expiry, or a limit.
await s3Client.share.updateLink({ id, maxDownloads: null, password: null });

// Off for good. The row stays until pruneLinks removes it.
await s3Client.share.revokeLink({ id });

Each link reports a status: active, expired, revoked, or exhausted, plus downloadCount, viewCount, and lastAccessedAt. A link whose file or folder was deleted has target.available: false.

In React, useShareLinks keeps one target's links in state, and ShareLinkDialog is the owner's dialog that takes it. On the shared page, useShareVisit does the same for a visitor, under SharedView.

On the server, s3.share.api runs the same actions with { request }, and s3.share.links reads and revokes rows without guards. Remove dead rows from a job:

const { pruned, orphaned } = await s3.share.pruneLinks({
  olderThan: Date.now() - 30 * 24 * 60 * 60 * 1000,
});

It removes links revoked or expired before olderThan, and links created before it whose file or folder was deleted. A deleted file never comes back under the same id, so those links can never open again.

Next steps

On this page