npm.io
0.0.5 • Published 3 years ago

tcos-multer-storage

Licence
MIT
Version
0.0.5
Deps
1
Size
12 kB
Vulns
0
Weekly
0

TCOS-Multer-Storage

Introduction

A Customer Multer Storage that store the upload file to the TecentCloud Cloud Object Storage.

Usage

Need the TencentCloud permanent key

Get the SecretId and SecretKey on the Manage API Key page in the CAM console.

import express from "express";
import cors from "cors";
import multer from 'multer';
import tcosMulterStorageEngine from 'tcos-multer-storage';

const PORT = process.env.PORT;
const HOST = process.env.HOST;

// SecretId and SecretKey also could be seted at process.env as COS_SECRET_ID and COS_SECRET_KEY
const tcStorage = tcosMulterStorageEngine({
  bucket: process.env.COS_BUCKET,
  region: process.env.COS_REGION,
  // SecretId: process.env.COS_SECRET_ID,
  // SecretKey: process.env.COS_SECRET_KEY,
});

const upload = multer({
  storage: tcStorage,
});

const app = express();

// refer file item attr in the sample-upload
// https://www.tencentcloud.com/document/product/436/43871#uploading-object-by-using-simple-upload 
app.post('/file', upload.any(), (req, res) => {
  res.setHeader("Content-Type", "application/json");
  res.write(JSON.stringify({ body: req.body, files: req.files }));
  res.end();
});

app.listen(PORT, HOST, () => {
  console.log(`Running on http://${HOST}:${PORT}`);
});

app.use(cors());
app.use(express.json());

See more on the example and the __test__

Todo

Support be required as CommonJS
Support be authorizated by using temporary key
Upload large file by using multipart operations

Reference