0.1.0 • Published 5 years ago
multer-oss-new v0.1.0
multer-oss
multer upload files to aliyun oss directly.
:star: Features
- support upload files to oss
- support reduce jpeg, jpg and png size automatically
- support es7 features async and await
- support compressed by gzip by default
:tada: Install
npm install multer-oss-new
:rocket: Usage
init oss storage
const OSSStorage = require('multer-oss-new')
const zlib = require('zlib')
const storage = new OSSStorage({
oss: { // required
region: 'oss-region',
internal: false,
accessKeyId: 'access-key-id',
accessKeySecret: 'access-key-secret',
bucket: 'bucket'
},
destination: async (req, file, ossClient) => {
return '' // return destination folder path, optional, '' is default value
},
filename: async (req, file, ossClient) => {
return file.originalname // return file name, optional, file.originalname is default value
},
stream: async (req, file, ossClient) => {
return file.stream.pipe(zlib.createGzip()) // compress strem in gzip
},
options: async (req, file, ossClient) => { // read more https://www.npmjs.com/package/ali-oss#putstreamname-stream-options
return {
contentLength: file.size,
headers: {
'Content-Encoding': gzip,
'Content-Disposition': file.originalname,
'Expires': 3600000 // in ms
}
}
}
})
if you already have an oss client, you can pass it to opt directly
const OSS = require('ali-oss')
const OSSStorage = require('multer-oss-new')
const zlib = require('zlib')
const ossClient = new OSS({
region: 'oss-region',
internal: false,
accessKeyId: 'access-key-id',
accessKeySecret: 'access-key-secret',
bucket: 'bucket'
})
const storage = new OSSStorage({
client: ossClient, // using oss client that already exists
destination: async (req, file, ossClient) => {
return '' // return destination folder path, optional, '' is default value
},
filename: async (req, file, ossClient) => {
return file.originalname // return file name, optional, file.originalname is default value
},
stream: async (req, file, ossClient) => {
return file.stream.pipe(zlib.createGzip()) // compress strem in gzip
},
options: async (req, file, ossClient) => { // read more https://www.npmjs.com/package/ali-oss#putstreamname-stream-options
return {
contentLength: file.size,
headers: {
'Content-Encoding': gzip,
'Expires': 3600000 // in ms
}
}
}
})
:kissing_heart: Full Examples
Full examples for using multer-oss with express, multer
const bodyParser = require('body-parser')
const express = require('express')
const http = require('http')
const multer = require('multer')
const OSSStorage = require('multer-oss-new')
const zlib = require('zlib')
const app = express()
const server = http.createServer(app)
const storage = new OSSStorage({
oss: { // required
region: 'oss-region',
internal: false,
accessKeyId: 'access-key-id',
accessKeySecret: 'access-key-secret',
bucket: 'bucket' // you could using all oss option in ali-oss pacakge
},
destination: async (req, file, ossClient) => {
return '' // return destination folder path, optional, '' is default value
},
filename: async (req, file, ossClient) => {
return file.originalname // return file name, optional, file.originalname is default value
},
stream: async (req, file, ossClient) => {
return file.stream.pipe(zlib.createGzip()) // compress strem in gzip
},
options: async (req, file, ossClient) => {
return {
contentLength: file.size,
headers: {
'Content-Encoding': gzip,
'Expires': 3600000 // in ms
}
}
}
})
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use('/upload', multer({ storage })) // for more multer usage, you could refrence multer document
app.use((error, req, res, next) => {
res.status(500).json({isError: true, error})
})
app.use((req, res) => {
return res.status(404).json({isError: true, error: 'Router NOT FOUNDED'})
})
server.listen(8000, err => {
if(err) return console.error('app start failed')
console.info('app start at 8000')
})