egg-aliyun-utils

1.0.11 • Public • Published

egg-aliyun-utils

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

Introduction

This is a util collection for aliyun services.

STS
  1. Get Temporary Auth: It can get a temporary authorization from aliyun with the Access Key Id and Access Key Secret.
OSS
  1. Upload A File: Upload a file to aliyun oss.

Install

$ npm i egg-aliyun-utils --save

Usage

// {app_root}/config/plugin.js
exports.aliyunUtils = {
    enable: true,
    package: 'egg-aliyun-utils'
};

Configuration

// {app_root}/config/config.default.js
exports.aliyunUtils = {};

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

Example

// {app_root}/service/mainService.js
const { Service } = require('egg');
const { STS, OSS } = require('egg-aliyun-utils');
const toArray = require('stream-to-array');
 
class mainService extends Service {
    async getAuth() {
        const stsIns = new STS({
            accessKeyId: 'qwe',
            accessKeySecret: 'qwer',
            bucket: 'qwe',
            stsRoleId: 'acs:ram::123123123:role/keyirole',
            region: 'oss-cn-hangzhou'
        });
        const roleSession = 'BrowserCheckFile';
        const roleAction = '"oss:GetObject"';
        const data = {
            roleSession,
            roleAction
        };
        const result = await stsIns.getTemporaryAuth(data);
        return result;
    }
 
    async upload() {
        const ossIns = new OSS({
            accessKeyId: 'qwe',
            accessKeySecret: 'qwer',
            region: 'oss-cn-hangzhou'
        });
        let stream;
        try {
            stream = await ctx.getFileStream();
        } catch (e) {
            console.log(e);
        }
        const parts = await toArray(stream);
        const buffer = Buffer.concat(parts);
        const data = {
            path: 'test',
            data: buffer,
            bucket: 'qwe'
        };
        const result = await ossIns.upload(data);
        return result;
    }
}
 
module.exports = mainService;

Questions & Suggestions

Please open an issue here.

Author

Chris

License

MIT

Package Sidebar

Install

npm i egg-aliyun-utils

Weekly Downloads

2

Version

1.0.11

License

MIT

Unpacked Size

9.74 kB

Total Files

8

Last publish

Collaborators

  • xxddqq123