https://github.com/junruchen/egg-obs
华为云 OBS,针对 Egg.js 开发
https://github.com/junruchen/egg-obs
Last synced: 8 months ago
JSON representation
华为云 OBS,针对 Egg.js 开发
- Host: GitHub
- URL: https://github.com/junruchen/egg-obs
- Owner: junruchen
- License: mit
- Created: 2019-07-09T12:24:01.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-05-06T06:16:31.000Z (almost 3 years ago)
- Last Synced: 2025-06-30T03:49:19.777Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 314 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# egg-obs
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
[npm-image]: https://img.shields.io/npm/v/egg-obs.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-obs
[travis-image]: https://img.shields.io/travis/eggjs/egg-obs.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-obs
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-obs.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-obs?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-obs.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-obs
[snyk-image]: https://snyk.io/test/npm/egg-obs/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-obs
[download-image]: https://img.shields.io/npm/dm/egg-obs.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-obs
[OBS](https://support.huaweicloud.com/obs/index.html) plugin for egg
## Install
```bash
$ npm i egg-obs --save
```
## Configuration
```js
// config/plugin.js
exports.obs = {
enable: true,
package: 'egg-obs',
};
```
```js
// config/config.default.js
exports.obs = {
access_key_id: '',
secret_access_key: '',
server: '',
bucket: ''
};
```
see [config/config.default.js](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.
```js
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](https://github.com/eggjs/egg/issues).
## License
[MIT](LICENSE)