egg-aliyun-utils
v1.0.11
Published
This is a util collection for aliyun services.
Downloads
15
Readme
egg-aliyun-utils
Introduction
This is a util collection for aliyun services.
STS
- Get Temporary Auth: It can get a temporary authorization from aliyun with the Access Key Id and Access Key Secret.
OSS
- 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