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

uppy-next-s3-multipart

v1.0.3

Published

This is a library designed to make it simple to integrate the Uppy uploader library with AWS S3 Multipart Uploads while using a NextJS server.

Downloads

16

Readme

UppyNextS3Multipart

This is a library designed to make it simple to integrate the Uppy uploader library with AWS S3 Multipart Uploads while using a NextJS server.

Normally Uppy wants you to include their Companion server for doing this, but it's a little bit overkill for this simple use case, and it means you'd have to switch to using Express, which causes you to lose many of the benefits of the NextJS server.

Setup

First install this library with yarn add uppy-next-s3-multipart or npm install uppy-next-s3-multipart

Then, create a new API endpoint at the location of your choosing. This endpoint should redirect all of its calls to new UppyNextS3MultipartEndpoint(...).handle(req, res). In this example, I'm creating it at pages/api/uppy-aws/[endpoint].ts:

import { UppyFile } from '@uppy/core';
import { S3 } from 'aws-sdk';
import type { NextApiRequest, NextApiResponse } from 'next'
import { v4 as uuid } from 'uuid';
import { UppyNextS3MultipartEndpoint } from uppy-next-s3-multipart';

const S3_ACCESS_KEY_ID = process.env.S3_ACCESS_KEY_ID;
const S3_SECRET_ACCESS_KEY = process.env.S3_SECRET_ACCESS_KEY;
const S3_REGION = process.env.S3_REGION;
const S3_BUCKET_NAME = process.env.S3_BUCKET_NAME ?? '';

const EXPIRE_TIME_SEC = 1 * 60 * 60;

const s3 = new S3({
  credentials: {
    accessKeyId: S3_ACCESS_KEY_ID ?? '',
    secretAccessKey: S3_SECRET_ACCESS_KEY ?? '',
  },
  region: S3_REGION,
});

export type FilenameGenParams = { prefix: string };

const endpointHandler = new UppyNextS3MultipartEndpoint<FilenameGenParams>(
  s3,
  S3_BUCKET_NAME,
  EXPIRE_TIME_SEC,
  // This is used to specify how you would like the file to be named
  // In this example, I am passing a prefix from the client, then adding
  // a year-month folder, then a uuid with the filename appended at the end
  (file, params) => {
    const date = new Date();
    const month = String(date.getUTCMonth() + 1).padStart(2, '0');
    const year = date.getUTCFullYear();
    return `${params.prefix}/${year}-${month}/${uuid()}_${file.name}`;
  }
);

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse,
) {
  return endpointHandler.handle(req, res);
}

Finally, on the client side, create your Uppy instance as you normally would, but pass in the options from getUppyNextS3MultipartOptions(endpoint). For example:

import '@uppy/core/dist/style.css'
import '@uppy/dashboard/dist/style.css'

import Uppy from '@uppy/core'
import { Dashboard } from '@uppy/react'
import { AwsS3Multipart } from 'uppy'
import { getUppyNextS3MultipartOptions } from 'uppy-next-s3-multipart'
import { FilenameGenParams } from '../pages/api/uppy-aws/[endpoint]'

const uppy = new Uppy();
uppy.use(AwsS3Multipart,
  getUppyNextS3MultipartOptions<FilenameGenParams>(
    // The endpoint you saved the earlier file at. No trailing slash.
    '/api/uppy-aws',
    // This is where we pass in the params used for filename generation
    { prefix: 'music' },
  ));

export function FileUploadExample() {
  return (
    <Dashboard uppy={uppy} />
  )
}