[关闭]
@JunQiu 2018-09-20T08:37:22.000000Z 字数 2176 阅读 1588

google cloud function

tools summary_2018/09


1、日常

1、 实现 cloud function pasueAd 统一代理

2、 google cloud function 学习


2、技术

1、google cloud function

1.1、google cloud function 概述
1.2、google cloud function 函数编写
  1. ### Example 接受多种方法,只是需要对应方法处理对应的数据,比如body,query
  2. exports.helloWorld = (req, res) => {
  3. if (req.body.message === undefined) {
  4. res.status(400).send('No message defined!')
  5. } else {
  6. console.log(req.body.message)
  7. res.status(200).end()
  8. }
  9. }
  10. Tips:如果要实现多种请求方法,可以通过req中的method来实现
  1. ## pub/sub 实例
  2. exports.helloPubSub = (event) => {
  3. const pubsubMessage = event.data
  4. const name = pubsubMessage.data ? Buffer.from(pubsubMessage.data, 'base64').toString() : 'World'
  5. console.log(`Hello, ${name}!`)
  6. }
  7. Tips:文档中有一个回调函数callback用于发出信号来指示您的函数已完成运行,但实际使用中没有必要,直接打印日志即可。
  8. google cloud function中:
  9. console.log() 命令具有 INFO 日志级别。
  10. console.error() 命令具有 ERROR 日志级别。
1.3、google cloud function 函数部署和调用(以http函数为例)
  1. ### 本地
  2. # 使用gcloud 命令行工具
  3. gcloud beta functions deploy <NAME> <TRIGGER>
  4. - NAME: Cloud Functions 函数的名称
  5. - TRIGGER:触发方式
  6. // 部署(trigger为http的刚才编写的函数 helloWorld)
  7. gcloud beta functions deploy helloWorld --trigger-http
  8. 生成urlhttps://us-east1-ai-dev-216405.cloudfunctions.net/helloWorld
  9. // 调用
  10. 使用http请求即可实现请求
  11. # 仓库
  12. 格式:gcloud beta functions deploy <NAME> \
  13. --source https://source.developers.google.com/projects/<PROJECT_ID>/repos/<REPOSITORY_ID>/moveable-aliases/master/paths/<SOURCE> \
  14. <TRIGGER>
  15. (--source:源代码仓库的位置)
  16. // Example:以仓库function-pause-ad为例
  17. gcloud beta functions deploy pauseAd --source https://source.developers.google.com/projects/ai-dev-216405/repos/function-pause-ad/moveable-aliases/master/paths/ --trigger-http
  18. // 当然失败了,因为该函数是后台函数
  19. // 调用方式相同
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注