Better Upload

Helpers

A collection of helper functions and utilities for your project.

S3 Clients

Better Upload has built-in clients for popular S3-compatible storage services, like AWS S3 and Cloudflare R2. Suggest a new client by opening an issue.

import { aws } from '@better-upload/server/clients';

const s3 = aws({
  accessKeyId: 'your-access-key-id',
  secretAccessKey: 'your-secret-access-key',
  region: 'us-east-1',
});

You can omit the parameters and let the client get your credentials from environment variables.

Objects

Helpers for working with objects in S3-compatible storage services. Suggest new helpers by opening an issue.

Presign get object

Generate a pre-signed URL to download an object.

import { presignGetObject } from '@better-upload/server/helpers';

const url = await presignGetObject(s3, {
  bucket: 'my-bucket',
  key: 'example.png',
  expiresIn: 3600, // 1 hour
});

Get object

Get an object (including its content) from an S3 bucket.

import { getObject } from '@better-upload/server/helpers';

const object = await getObject(s3, {
  bucket: 'my-bucket',
  key: 'example.png',
});

Head object

Get metadata about an object without fetching its content.

import { headObject } from '@better-upload/server/helpers';

const object = await headObject(s3, {
  bucket: 'my-bucket',
  key: 'example.png',
});

Move object (rename)

Move an object from one key to another, within or between, S3 buckets. Also known as renaming.

import { moveObject } from '@better-upload/server/helpers';

await moveObject(s3, {
  source: {
    bucket: 'source-bucket',
    key: 'example.png',
  },
  destination: {
    bucket: 'destination-bucket',
    key: 'copy.png',
  },
});

This copies the object to the new location and then deletes the original object. It can be slow.

Copy object

Copy an object, within or between, S3 buckets.

import { copyObject } from '@better-upload/server/helpers';

await copyObject(s3, {
  source: {
    bucket: 'source-bucket',
    key: 'example.png',
  },
  destination: {
    bucket: 'destination-bucket',
    key: 'images/example.png',
  },
});

Put object

Upload an object to an S3 bucket.

import { putObject } from '@better-upload/server/helpers';

await putObject(s3, {
  bucket: 'my-bucket',
  key: 'example.txt',
  body: 'Hello, world!',
  contentType: 'text/plain',
});

Do not use this helper for large files or for client-side uploads (use the standard router instead).

Delete object

Delete an object from an S3 bucket.

import { deleteObject } from '@better-upload/server/helpers';

await deleteObject(s3, {
  bucket: 'my-bucket',
  key: 'example.png',
});