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. -->
const express = const unggah = const app = const upload =
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
- Make sure you have a google cloud project with billing enabled.
- Enable Google Storage API for the project.
- Create a bucket to store the files.
- 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
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
- Create a bucket to store the files.
- 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
// ....... const upload = app app app // .......
Deleting File
The storage object has .delete() method that you can use to delete a file
storage