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

multer-oss-new

v0.1.0

Published

upload files to oss through multer directly

Downloads

10

Readme

multer-oss

Codacy Badge Build Status license npm version NPM downloads Coverage Status Dependencies Known Vulnerabilities issues

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')
})

License

MIT