# multer-cos

> Streaming multer storage engine for QCloud COS

Latest version **1.0.3** (published 2018-03-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install multer-cos
pnpm add multer-cos
yarn add multer-cos
bun add multer-cos
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2018-03-13 |
| First published | 2018-03-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 6.10.0 |
| Dependencies | 0 |
| Unpacked size | 14.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | lanbosm |
| Maintainers | lanbosm |
| Keywords | express, multer, COS |

## Links

- npm: https://www.npmjs.com/package/multer-cos
- Repository: https://github.com/lanbosm/multer-COS
- Homepage: https://github.com/lanbosm/multer-COS#readme
- Issues: https://github.com/lanbosm/multer-COS/issues
- npm.io page: https://npm.io/package/multer-cos

## Recent versions

- 1.0.3 (latest) — 2018-03-13
- 1.0.2 — 2018-03-13
- 1.0.1 — 2018-03-13
- 1.0.0 — 2018-03-13

## README

# Multer-COS
 Multer Storage Engine 腾讯云COS版

COS文档使用
https://cloud.tencent.com/document/product/436/12264#slice-upload-file


## Installation

	npm install multer-cos

## Usage

### myMulter.js
```js
let multer = require('multer');
let multerCOS = require('multer-cos');

const cosConfig={
  //id和key是必须

  //SecretId: AKIXXXXXXXXXXX,
  //SecretKey:XXXXXXXXXXXXXX,
  //Bucket:test-bucket-125XXXXXXXXX
  //Region=ap-shanghai
  // 可选参数
  FileParallelLimit: 3,    // 控制文件上传并发数
  ChunkParallelLimit: 3,   // 控制单个文件下分片上传并发数
  ChunkSize: 1024 * 1024,  // 控制分片大小，单位 B
  domain:'static.dorodoro-lab.com', //cos域名
  dir:'upload',                     //cos文件路径
  onProgress:function(progressData){//进度回调函数，回调是一个对象，包含进度信息
      //console.log(progressData);
  }

};


//定义仓库
const storage = multerCOS({
  cos:cosConfig,
  //Note:如果你传递的是一个函数，你负责创建文件夹，如果你传递的是一个字符串，multer会自动创建 如果什么都不传 系统自己会生成tmp目录
  destination: function (req, file, cb) {
      cb(null, dir);
  },
  //自己会生成个随机16字母的文件名和后缀
  filename:'auto'
});

module.exports=function(opt) {
    return  multer({
        storage: storage,
    }).array(opt);
};
```
### app.js
```js
let express= require('express');
let myMulter = require('./myMulter');

let app = express();

let server = require('http').Server(app);


//文件上传服务
app.post('/upload',  function (req, res, next)  {

    let responseData; //响应的数据

    var upload=myMulter('file',1);

    upload(req, res, function (err) {

           try {
                 if (err) throw err;
                 if(req.files.length==0) throw new  Error("不能上传空文件");

                 responseData={msg:"上传成功",code:2000};
                 responseData.url=req.files[0].url;
                 res.json(responseData);


           }
           catch (err) {
                 responseData={msg:"上传失败",code:4000};;
                 responseData.error=err.message;
                 res.status(500).json(responseData);
           }
     });
});


```
 ### dotenv
 推荐用 [dotenv](https://www.npmjs.com/package/dotenv) 保护隐私信息 如appid key bucket 等


## License

[MIT](LICENSE)
=======

---
_Source: https://npm.io/package/multer-cos · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
