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

unggah

v2.1.0

Published

Express middleware to upload file to google cloud storage or S3 compatible clouds.

Downloads

14

Readme

unggah

This is a convenient wrapper around multer, google cloud storage, and AWS S3 API as a single express middleware to upload file from user to Google Cloud Storage or AWS S3 and S3 compatible providers.

It will change the field for the uploaded file with a url string like

  • https://storage.googleapis.com/bucket-name/filename for Google Cloud Storage
  • https://bucket-name.s3-ap-southeast-1.amazonaws.com/filename for AWS S3

The url now can be saved to database.

Installation

npm install unggah

Then install either aws-sdk or @google-cloud/storage, depending on what service you're going to use.

npm install aws-sdk
# or
npm install @google-cloud/storage

Usage

<!-- Don't forget the enctype="multipart/form-data" in your form. -->
<form action="/upload-single" method="post" enctype="multipart/form-data">
  <input type="file" name="file" />
</form>
const express = require('express')
const unggah = require('unggah')

const app = express()

const upload = unggah({
  limits: {
    fileSize: 1e6 // in bytes
  },
  storage: storage // storage configuration for google cloud storage or S3
})

options:

  • limits: limits of uploaded data in object form (similar to limits option in multer)
  • storage: setup for cloud storage provider that you want to use (Details in following sections).

It will return an upload object that have 3 methods: (.single(), .array(), and .fields()). You can use all of them just like how you would use multer.


Using Google Cloud Storage

Prerequisites

  1. Make sure you have a google cloud project with billing enabled.
  2. Enable Google Storage API for the project.
  3. Create a bucket to store the files.
  4. Create a service account and download the credential in JSON format.

storage configurations

  • keyFilename: file path for credential that you have downloaded before.
  • bucketName: the bucket name that will contain the uploaded file, you can create one through google cloud console.
  • rename (optional): it's a string or a function that return a string which will be used as name for files being stored. If omitted it will use the original filename prefixed with the timestamp.
const storage = unggah.gcs({
  keyFilename: '/Users/me/google-credential-keyfile.json',
  bucketName: 'my-bucket',
  rename: (req, file) => {
    return `${Date.now()}-${file.originalname}`  // this is the default
  }
})

note:

To make uploaded files available for public view, add Storage Object Viewer role for allUsers. Step by step instruction can be found here


Using AWS S3 or S3 compatible providers

Prerequisites

  1. Create a bucket to store the files.
  2. Obtain access key id and it's secret for your AWS user account.

storage configurations

  • endpoint: url endpoint for your S3 storage (example: s3.ap-southeast-1.amazonaws.com)
  • accessKeyId: Access Key ID that you get from prerequisite #2,
  • secretAccessKey: Secret Access Key that you get from prerequisite #2,
  • bucketName: the bucket name that will contain the uploaded file, you can create one through google cloud console.
  • rename (optional): it's a string or a function that return a string which will be used as name for files being stored. If omitted it will use the original filename prefixed with the timestamp.
const storage = unggah.s3({
  endpoint: 's3.ap-southeast-1.amazonaws.com',
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  bucketName: 'my-bucket',
  rename: (req, file) => {
    return `${Date.now()}-${file.originalname}`  // this is the default
  }
})

// .......

const upload = unggah({
  limits: {
    fileSize: 1e6 // in bytes
  },
  storage: storage
})

app.post('/upload-single', upload.single('file'), (req, res) => {
  console.log(req.body)
  res.end()
})

app.post('/upload-array', upload.array('files'), (req, res) => {
  console.log(req.body)
  res.end()
})

app.post('/upload-fields',
  upload.fields([{ name: 'file1' }, { name: 'file2' }]),
  (req, res) => {
    console.log(req.body)
    res.end()
  }
)

// .......

Deleting File

The storage object has .delete() method that you can use to delete a file

storage.delete('file.txt')
  .then(() => console.log('deleted'))
  .catch(err => console.log(err.message))