Code Monkey home page Code Monkey logo

egg-oss's Introduction

egg-oss

NPM version Node.js CI Test coverage npm download

OSS plugin for egg

Install

$ npm i egg-oss

Configration

To enable oss plugin, you should change ${baseDir}/config/plugin.js

// config/plugin.js
exports.oss = {
  enable: true,
  package: 'egg-oss',
};

Then fill in nessary information like OSS's bucket, accessKeyId, accessKeySecret in ${baseDir}/config/config.{env}.js

Mention, egg-oss support normal oss client and oss cluster client, based on oss-client:

// normal oss bucket
exports.oss = {
  client: {
    accessKeyId: 'your access key',
    accessKeySecret: 'your access secret',
    bucket: 'your bucket name',
    endpoint: '{https or http}://{your endpoint name}.aliyun.com',
    timeout: '60s',
  },
};

// cluster oss bucket
// need to config all bucket information under cluster
exports.oss = {
  client: {
    cluster: [{
      endpoint: '{https or http}://{your endpoint name}.aliyun.com',
      accessKeyId: 'id1',
      accessKeySecret: 'secret1',
    }, {
      endpoint: '{https or http}://{your endpoint name}.aliyun.com',
      accessKeyId: 'id2',
      accessKeySecret: 'secret2',
    }],
    schedule: 'masterSlave', //default is `roundRobin`
    timeout: '60s',
  },
};

// if config.sts == true, oss will create STS client
exports.oss = {
  client: {
    sts: true,
    accessKeyId: 'your access key',
    accessKeySecret: 'your access secret',
  },
};

Init in egg agent, default is false:

exports.oss = {
  useAgent: true,
};

Usage

You can aquire oss instance on app or ctx.

The example below will upload file to oss using the file mode of egg-multipart.

const path = require('path');
const Controller = require('egg').Controller;
const fs = require('mz/fs');

// upload a file in controller
module.exports = class extends Controller {
  async upload() {
    const ctx = this.ctx;

    const file = ctx.request.files[0];
    const name = 'egg-oss-demo/' + path.basename(file.filename);
    let result;
    try {
      result = await ctx.oss.put(name, file.filepath);
    } finally {
      await fs.unlink(file.filepath);
    }

    if (result) {
      console.log('get oss object: %j', object);
      ctx.unsafeRedirect(result.url);
    } else {
      ctx.body = 'please select a file to upload!';
    }
  }
};

To learn OSS client API, please check oss document

Create one more OSS buckets

Some application need to access more than one oss bucket, then you need to configure oss.clients, and you can create new oss instance dynamicly by app.oss.createInstance(config).

  • ${appdir}/config/config.default.js
exports.oss = {
  clients: {
    bucket1: {
      bucket: 'bucket1',
    },
    bucket2: {
      bucket: 'bucket2',
    },
  },
  // shared by client, clients and createInstance
  default: {
    endpoint: '{https or http}://{your endpoint name}.aliyun.com',
    accessKeyId: '',
    accessKeySecret: '',
  },
};

exports.bucket3 = {
  bucket: 'bucket3',
};
  • ${appdir}/config/plugin.js
exports.oss = true;
  • ${appdir}/app.js
module.exports = app => {
  const bucket1 = app.oss.get('bucket1');
  const bucket2 = app.oss.get('bucket2');
  // it will merge app.config.bucket3 and app.config.oss.default
  const bucket3 = app.oss.createInstance(app.config.bucket3);
}

Development

Create .env file for environment that testcase need.

ALI_SDK_OSS_REGION=oss-cn-hangzhou
ALI_SDK_OSS_ENDPOINT=https://oss-cn-hangzhou.aliyuncs.com
ALI_SDK_OSS_ID=
ALI_SDK_OSS_SECRET=
ALI_SDK_OSS_BUCKET=
ALI_SDK_STS_ID=
ALI_SDK_STS_SECRET=
ALI_SDK_STS_BUCKET=
ALI_SDK_STS_ROLE=

And run npm test.

The key is saved in link (ask @popomore), you can change the key by run scripts/gen_env.sh.

Questions & Suggestions

Please open an issue here.

License

MIT

egg-oss's People

Contributors

atian25 avatar dead-horse avatar fengmk2 avatar jeff-tian avatar jtyjty99999 avatar kotot avatar popomore avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.