Git Product home page Git Product logo

egg-obs's Introduction

egg-obs

NPM version build status Test coverage David deps Known Vulnerabilities npm download

OBS plugin for egg

Install

$ npm i egg-obs --save

Configuration

// config/plugin.js
exports.obs = {
  enable: true,
  package: 'egg-obs',
};
// config/config.default.js
exports.obs = {
  access_key_id: '',
  secret_access_key: '',
  server: '',
  bucket: ''
};

see config/config.default.js for more detail.

Usage

putObject({ key, imageProcess })

getObject(options)

options参数说明:

  • Key
  • ImageProcess
  • SaveAsStream

delObject({ key })

setBucket(name)

You can require obs instance on app or ctx.

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

module.exports = class extends Controller {
  // upload file
  async upload() {
    const { ctx } = this
    const stream = await ctx.getFileStream()
    const filename = Math.floor(Math.random() * 10000) + path.extname(stream.filename)
    
    try {
      await ctx.obs.putObject({
        key: filename,
        body: stream
      });
      ctx.body = `/obs-image/${filename}`
    } catch (error) {
      ctx.body = error;
      ctx.logger.error(new Error(error));
    }
  }

  // get file
  async getImage() {
    const { ctx } = this
    const objectname = ctx.params.objectname;
    try {
      const result = await ctx.obs.getObject({ Key: objectname })
      if (result.CommonMsg.Status < 300 && result.InterfaceResult) {
        // 读取对象内容
        const { ContentLength, Date, ETag, ContentType, Content } = result.InterfaceResult
        ctx.status = 200
        ctx.type = ContentType
        ctx.etag = ETag
        ctx.length = ContentLength
        ctx.set('Date', Date)
        ctx.body = Content
      } else {
        ctx.body = result.CommonMsg.Message
      }
    } catch (error) {
      ctx.body = error
      ctx.logger.error(new Error(error));
    }
  }
};

Questions & Suggestions

If upload blob file, ctx.getFileStream() will prompt [Invalid filename: blob] error.

Use config.multipart = { fileExtensions: [''] } to fix it.

Please open an issue here.

License

MIT

egg-obs's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

egg-obs's Issues

在vue项目引入

new Error("Cannot find module 'egg-obs'"); e.code = 'MODULE_NOT_FOUND'; throw e;
安装后无法找到模块,这个应该怎么解决

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.