npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sicamois/use-upload-to-s3

v0.5.1

Published

A simple react hook `useUploadToS3` that levrage React Server Component to securely upload files to a private S3 bucket from the client while keeping the secret keys on the server.

Downloads

35

Readme

useUploadToS3

A simple react hook useUploadToS3 that levrage React Server Component to securely upload files to a private S3 bucket from the client while keeping the secret keys on the server.

[!WARNING]

This hook uses React 19 Server Actions and thus will only work with Next.js 15.

Installation

pnpm add @sicamois/use-upload-to-s3

Usage

Configure S3 Bucket and IAM User

  • Create a private S3 bucket.
  • Create a new IAM user with the following policy:
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:PutObject", "s3:GetObject"],
      "Resource": ["arn:aws:s3:::YOUR_BUCKET_NAME/*"]
    },
    {
      "Effect": "Allow",
      "Action": ["s3:GetBucketCORS", "s3:PutBucketCORS"],
      "Resource": ["arn:aws:s3:::YOUR_BUCKET_NAME"]
    }
  ]
}
  • Save the Access Key ID and Secret Access Key.

Add Environment Variables

  • Create a .env.local file in the root of your project, with the following environment variables:
AWS_ACCESS_KEY_ID=<YOUR_ACCESS_KEY_ID>
AWS_SECRET_ACCESS_KEY=<YOUR_SECRET_ACCESS_KEY>
AWS_REGION=<YOUR_REGION>

Quickstart

'use client';

import { useUploadToS3 } from '@sicamois/use-upload-to-s3';

export default function UploadFile() {
  const [handleInputChange, s3key, isPending, error] =
    useUploadToS3('YOUR_BUCKET_NAME');

  return (
    <div>
      <input type='file' onChange={handleInputChange} />
      <p>s3key: {s3key}</p>
      {isPending ? <p>Uploading...</p> : null}
      {error ? <p>Error: {error.message}</p> : null}
    </div>
  );
}

[!NOTE]

  • handleInputChange is a convenience function that makes all the magic happen. It triggers a call to a Server Action to create a secured URL to upload the file to S3.
  • s3key is the key of the file in the S3 bucket (in a state for convinience). A uuid is added to avoid overwrites.
  • isPending is a state that indicates if the file is being uploaded. As a state, it triggers a re-render when it changes.
  • error is a state that contains the error message if something goes wrong. It is also a state to be more convient to handle.

useUploadToS3

Signature

function useUploadToS3(
  bucket: string,
  options: {
    accept?: string;
    sizeLimit?: string;
    onUploadComplete?: (s3key: string, file: File) => void;
  }
);

Options

accept: string

The file types to accept, defaults to all files.

[!NOTE]

It accepts a string with the file MIME types (separated by a comma)

sizeLimit: string

The maximum file size in bytes, defaults to 1MB.

[!NOTE]

It accepts a string with the size in bytes, or a string with the size in KB, MB, GB, etc.

[!WARNING] Server Actions have a default size limit of 1MB.
To change that you have to set it in the next.config.js (or next.config.mjs) file.
see https://nextjs.org/docs/app/api-reference/next-config-js/serverActions#bodysizelimit

onUploadComplete: (s3key: string) => void

A callback function to be executed when the upload completes.

[!NOTE]

It is called with the S3 key of the uploaded file and the file itself.

💡 You can call a server action to do something with the key on the server, like adding it to a database.

Example with options

'use client';
...
import { useUploadToS3 } from '@sicamois/use-upload-to-s3';
import { addFileInfosToDatabase } from '../server/addFileToDatabase';
import { useRouter } from 'next/navigation';
...

export default function UploadFile() {
  const router = useRouter();
  const [handleInputChange, s3key, isPending, error] = useUploadToS3(
    'YOUR_BUCKET_NAME',
    {
      accept: 'image/*',
      sizeLimit: '5MB',
      onUploadComplete: async (s3key, file) => {
        const success = await addFileInfosToDatabase(
          s3key,
          file.name,
          file.size
        );
        if (success) {
          router.refresh();
        }
      },
    }
  );

  return (
    <div>
      // Some code here to diplay the images uploaded to S3
    </div>
    <div>
      <input type='file' onChange={handleInputChange} />
      <p>s3key: {s3key}</p>
      {isPending ? <p>Uploading...</p> : null}
      {error ? <p>Error: {error.message}</p> : null}
    </div>
  );
}

Motivations

  • To provide a simple, efficient & secure way to upload files to a private S3 from the client.
  • Makes everything that needs to be done on the server (using secrets, etc) to be done... on the server side !
  • The upload is done from the client, so the file never touches the server.
    • Circumvent the limitation on Vercel serverless functions to 4.5MB upload size.
  • The hook is built with React Server Components, so it's generate a very small overhead in the bundle size on the client (~3KB).
  • It removes the need to make your S3 bucket public, and thus remove the need for a trade-off between security and convenience.