Skip to content

Latest commit

 

History

History
101 lines (82 loc) · 2.3 KB

README.md

File metadata and controls

101 lines (82 loc) · 2.3 KB

egg-cos

NPM version Test coverage David deps Known Vulnerabilities npm download

Install

$ npm i @shuang6/egg-cos --save

Configuration

// {app_root}/config/plugin.js
exports.cos = {
  enable: true,
  package: '@shuang6/egg-cos'
};
// {app_root}/config/config.default.js
exports.cos = {
  client: {
    SecretId: '',
    SecretKey: '',
    Bucket: '',
    Region: ''
  }
};

Init in egg agent, default is false:

exports.cos = {
  useAgent: true
};

Usage

You can aquire tencent cloud cos instance on app or ctx.

const path = require('path');
const Controller = require('egg').Controller;

// upload a file in controller
module.exports = class extends Controller {
  async upload() {
    const ctx = this.ctx;
    // please enable `file` mode of `egg-multipart`.
    const file = ctx.request.files[0];
    const name = 'egg-cos/' + path.basename(file.filename);
    let result;
    try {
      result = await ctx.cos.put(name, file.filepath);
    } finally {
      // need to remove the tmp files
      await ctx.cleanupRequestFiles();
    }

    if (result) {
      ctx.logger.info('cos response:\n', result);
      ctx.body = {
        url: `https://${result.Location}`
      };
    } else {
      ctx.body = 'please select a file to upload!';
    }
  }
};

Questions & Suggestions

Please open an issue here.

License

MIT