1.0.1 • Published 1 year ago

@simagic/multer-ali-oss v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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:

KeyDescriptionNote
bucketThe bucket used to store the fileOSSStorage
keyThe name of the fileOSSStorage
aclAccess control for the fileOSSStorage
contentTypeThe mimetype used to upload the fileOSSStorage
metadataThe metadata object to be sent to ali-ossOSSStorage
locationThe ali-oss url to access the fileOSSStorage
etagThe etagof the uploaded file in ali-ossOSSStorage
contentDispositionThe contentDisposition used to upload the fileOSSStorage
storageClassThe storageClass to be used for the uploaded file in ali-ossOSSStorage
contentEncodingThe contentEncoding used to upload the fileOSSStorage
hashThe hash of the fileMD5

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: