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
bucket-name
.s3-ap-southeast-1.amazonaws.com/filename
for AWS S3The url now can be saved to database.
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
<!-- 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:
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.
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
s3.ap-southeast-1.amazonaws.com
)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()
}
)
// .......
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))
Version | Tag | Published |
---|---|---|
2.1.0 | latest | 2yrs ago |