generated from serverless/template-package
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support apigw base64 encode (#46)
* feat: support apigw base64 encode * docs: update upload.md * test: fix
- Loading branch information
Showing
12 changed files
with
94 additions
and
150 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
## 文件上传说明 | ||
|
||
项目中如果涉及到文件上传,需要依赖 API 网关提供的 [Base64 编码能力](https://cloud.tencent.com/document/product/628/51799),使用时只需要 `serverless.yml` 中配置 `isBase64Encoded` 为 `true`,如下: | ||
|
||
```yaml | ||
app: appDemo | ||
stage: dev | ||
component: koa | ||
name: koaDemo | ||
|
||
inputs: | ||
# 省略... | ||
apigatewayConf: | ||
isBase64Encoded: true | ||
# 省略... | ||
# 省略... | ||
``` | ||
|
||
当前 API 网关支持上传最大文件大小为 `2M`,如果文件过大,请修改为前端直传对象存储方案。 | ||
|
||
## Base64 示例 | ||
|
||
此 Github 项目的 `example` 目录下存在模板文件: | ||
|
||
- [sls.upload.js](../example/sls.upload.js) | ||
|
||
开发者可根据个人项目需要参考修改,使用时需要复制文件名为 `sls.js`。 | ||
|
||
文件中实现了文件上传接口 `POST /upload`,如果要支持文件上传,需要安装 `@koajs/multer` 和 `multer` 包。 | ||
|
||
同时需要在 `serverless.yml` 的 `apigatewayConf` 中配置 `isBase64Encoded` 为 `true`。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
org: orgDemo | ||
app: appDemo | ||
stage: dev | ||
component: koa | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,25 @@ | ||
const Koa = require('koa') | ||
const KoaRouter = require('koa-router') | ||
const KoaRouter = require('@koa/router') | ||
const sendFile = require('koa-sendfile') | ||
const path = require('path') | ||
|
||
const app = new Koa() | ||
const router = new KoaRouter() | ||
const isServerless = process.env.SERVERLESS | ||
const PORT = 3000 | ||
|
||
// Routes | ||
router.get(`/*`, async (ctx) => { | ||
router.get(`/`, async (ctx) => { | ||
await sendFile(ctx, path.join(__dirname, 'index.html')) | ||
}) | ||
|
||
app.use(router.allowedMethods()).use(router.routes()) | ||
|
||
// don't forget to export! | ||
module.exports = app | ||
if (isServerless) { | ||
module.exports = app | ||
} else { | ||
app.listen(PORT, () => { | ||
console.log(`Server start on http://localhost:${PORT}`) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const Koa = require('koa') | ||
const KoaRouter = require('@koa/router') | ||
const multer = require('@koa/multer') | ||
const sendFile = require('koa-sendfile') | ||
const path = require('path') | ||
|
||
const isServerless = process.env.SERVERLESS | ||
const app = new Koa() | ||
const router = new KoaRouter() | ||
const upload = multer({ dest: isServerless ? '/tmp/upload' : './upload' }) | ||
|
||
router.get(`/`, async (ctx) => { | ||
await sendFile(ctx, path.join(__dirname, 'index.html')) | ||
}) | ||
router.post('/upload', upload.single('file'), (ctx) => { | ||
ctx.body = { | ||
success: true, | ||
data: ctx.file | ||
} | ||
}) | ||
|
||
app.use(router.routes()).use(router.allowedMethods()) | ||
|
||
if (isServerless) { | ||
module.exports = app | ||
} else { | ||
app.listen(3000, () => { | ||
console.log(`Server start on http://localhost:3000`) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"dependencies": { | ||
"download": "^8.0.0", | ||
"tencent-component-toolkit": "^1.20.6", | ||
"tencent-component-toolkit": "1.20.10", | ||
"type": "^2.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters