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

@simagic/multer-ali-oss

v1.0.1

Published

Streaming multer storage engine for Aliyun OSS.

Downloads

10

Readme

Multer Ali-OSS

Streaming multer storage engine for ali-oss.

This project is mostly an integration piece for existing code samples from Multer's storage engine documentation with a call to ali-oss as the substitution piece for file system.

Ali-OSS SDK Versions

ali-oss

Installation

npm install --save @simagic/multer-ali-oss

Usage

const express = require('express');
const multer = require('multer');
const OSS = require('ali-oss');
const { OSSStorage, AUTO_CONTENT_TYPE } = require('@simagic/multer-ali-oss')

const store = new OSS({
  region: '<oss region>',
  accessKeyId: '<Your accessKeyId>',
  accessKeySecret: '<Your accessKeySecret>',
  bucket: '<Your bucket name>'
});

const upload = multer({
  storage: OSSStorage({
    oss: store,
    bucket: '<Your bucket name>', // todo: Support different buckets instead of the default
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    key: function (req, file, cb) {
      cb(null, 'simagic-cloud/' + Date.now().toString())
    }
    // Direct parameters are also valid
    acl: 'public-read',
  })
})

// Example route for single file upload
app.post('/upload', upload.single('file'), (req, res) => {
  const file =  req.file
  // File uploaded successfully
  console.log(req.file)
  res.status(200).send('File uploaded successfully');
});

// Example route for multiple file upload
app.post('/uploads', upload.fields([
  { name: 'avatar', maxCount: 1 },
  { name: 'photo', maxCount: 2}
]), (req, res) => {
  // Access text fields via req.body
  console.log(req.body);

  // Access uploaded files via req.files
  console.log(req.files);

  // Files uploaded successfully
  res.status(200).send('Files uploaded successfully');
});

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

// ts
// import { OSSStorage, AUTO_CONTENT_TYPE, OSSStorageEngine } from '@simagic/multer-ali-oss';

File information

Each file contains the following information exposed by @simagic/multer-ali-oss:

Key | Description | Note --- | --- | --- bucket | The bucket used to store the file | OSSStorage key | The name of the file | OSSStorage acl | Access control for the file | OSSStorage contentType | The mimetype used to upload the file | OSSStorage metadata | The metadata object to be sent to ali-oss | OSSStorage location | The ali-oss url to access the file | OSSStorage etag | The etagof the uploaded file in ali-oss | OSSStorage contentDisposition | The contentDisposition used to upload the file | OSSStorage storageClass | The storageClass to be used for the uploaded file in ali-oss | OSSStorage contentEncoding | The contentEncoding used to upload the file | OSSStorage hash | The hash of the file | MD5

Also include:

  • fieldname
  • originalname
  • encoding
  • mimetype

Setting ACL

ACL values can be set by passing an optional acl parameter into the @simagic/multer-ali-oss object.

const upload = multer({
  storage: OSSStorage({
    oss: store,
    bucket: '<Your bucket name>', // todo: Support different buckets instead of the default
    acl: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    key: function (req, file, cb) {
      cb(null, 'simagic-cloud/' + Date.now().toString())
    }
  })
})

Setting Metadata

const upload = multer({
  storage: OSSStorage({
    oss: store,
    bucket: '<Your bucket name>', // todo: Support different buckets instead of the default
    acl: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    key: function (req, file, cb) {
      cb(null, 'simagic-cloud/' + Date.now().toString())
    }
  })
})

Setting Cache-Control header

const upload = multer({
  storage: OSSStorage({
    oss: store,
    bucket: '<Your bucket name>', // todo: Support different buckets instead of the default
    acl: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    cacheControl: 'max-age=31536000',
    key: function (req, file, cb) {
      cb(null, 'simagic-cloud/' + Date.now().toString())
    }
  })
})

Setting Custom Content-Type

const upload = multer({
  storage: OSSStorage({
    oss: store,
    bucket: '<Your bucket name>', // todo: Support different buckets instead of the default
    acl: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    contentType: AUTO_CONTENT_TYPE,
    cacheControl: 'max-age=31536000',
    key: function (req, file, cb) {
      cb(null, 'simagic-cloud/' + Date.now().toString())
    }
  })
})

You may also use a function as the contentType, which should be of the form function(req, file, cb).

More options

Setting StorageClass

optional storageClass

Setting Content-Disposition

optional contentDisposition

Setting Content-Encoding

optional contentEncoding

TODO: