firebase - 如何在本地测试 firebase 触发器的存储?

标签 firebase google-cloud-functions

我有一个以这样的触发器开始的函数:

exports.generateThumbnail = functions.storage.object().onChange(event => {

如何在本地进行测试?我已经阅读了 Firebase 的本地测试文档,但是有人能给我一个如何在本地触发此操作的示例吗?目前有点困惑。谢谢!

最佳答案

要在本地测试 Firebase Cloud Functions,您有两种选择:

  • 云函数外壳
  • Firebase CLI

后者仅适用于 HTTPS 函数,因此它实际上与您的目的无关(您正在测试存储触发的函数)。

让我们从项目的 functions 目录调用 Cloud Functions Shell:

$ npm i -g firebase-tools@latest
...

$ firebase --version
3.17.3

$ firebase experimental:functions:shell
i  functions: Preparing to emulate functions.
✔  functions: generateThumbnail
firebase >

这为您提供了一个交互式 shell,您可以在其中直接执行函数:

firebase > generateThumbnail()
'Successfully invoked function.'
...

或者任何其他 JavaScript 代码:

firebase > console.log(Math.random())
0.17387339404246105
undefined

在本地测试函数时,您不会收到真正的 Firebase 存储事件,因此,您必须使用测试数据调用函数。

来自official documentation :

For Storage, Auth, and Crashlytics functions, invoke the local function with the test data that you’d like to see inside of the function. Your test data must follow the corresponding data formats:

  • For Cloud Storage: ObjectMetadata
  • For Authentication: UserRecord
  • For Crashlytics: Issue

Specify only the fields that your code depends on, or none at all if you only want to run the function.

好的,接下来您需要创建一些符合 ObjectMetadata 的测试数据

在项目的 functions 目录中创建一个包含以下内容的文件 testData.json。请注意相应的 ObjectMetadata 字段:

{
  "bucket": "unique-name-of-your-app.appspot.com",
  "contentDisposition": "inline; filename*=utf-8''test.png",
  "contentType": "image/png",
  "crc32c": "LaOnCg==",
  "etag": "CeDut9jz5dgCFAE=",
  "generation": "1416567827822212",
  "id": "unique-name-of-your-app.appspot.com/test.png/1416567827822212",
  "kind": "storage#object",
  "md5Hash": "N2rxNWE0ZGRtZmE0Y2M2NDIyOWY5NzIeNzA4YzdizjU=",
  "mediaLink": "https: //www.googleapis.com/download/storage/v1/b/unique-name-of-your-app.appspot.com/o/test.png?generation=1416567827822212&alt=media",
  "metadata": {
    "firebaseStorageDownloadTokens": "1cbc7278-9e5b-4d05-a5de-91a3a067a4bc"
  },
  "metageneration": "1",
  "name": "test.png",
  "resourceState": "not_exists",
  "selfLink": "https: //www.googleapis.com/storage/v1/b/unique-name-of-your-app.appspot.com/o/test.png",
  "size": "168501",
  "storageClass": "STANDARD",
  "timeCreated": "2018-01-21T20: 33: 47.739Z",
  "timeDeleted": "2018-01-21T20: 33: 58.207Z",
  "timeStorageClassUpdated": "2018-01-21T20: 33: 47.739Z",
  "updated": "2018-01-21T20: 33: 47.739Z"
}

正如文档所述,您不一定需要所有这些字段,请使用您的测试实际需要的字段。

现在您可以返回 Cloud Functions Shell 并导入测试数据:

firebase > const testData = require('./testData.json')
undefined

并使用测试数据作为参数调用您的函数:

firebase > generateThumbnail(testData)
'Successfully invoked function.'
...

为了进一步自动化,您还可以创建一个包含以下内容的 testFunction.js 文件:

const testData = require('./testData.json');
generateThumbnail(testData);

现在,您无需在 Cloud Functions Shell 中手动输入所有这些内容,只需从操作系统的 shell 执行它并通过标准输入重定向 testFunction.js:

$ firebase experimental:functions:shell < ./testFunction.js 
i  functions: Preparing to emulate functions.
✔  functions: generateThumbnail
firebase > const testData = require('./testData.json');
undefined
firebase > generateThumbnail(testData);
'Successfully invoked function.'
...

希望这有帮助。

关于firebase - 如何在本地测试 firebase 触发器的存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47096736/

相关文章:

javascript - 为什么我的快照返回 null,即使 Firebase 数据库上有值

swift - Firebase 太慢而无法加载基于图 block 的游戏

node.js - 如何使用 firebase 函数刷新 google oauth2 上的 token ?

node.js - 使用 firebase 实时触发函数中的其他引用值

node.js - 从 firebase 函数 node.js 服务器的 tmp 目录上传合成语音

通过 HTTP 触发时 Firebase 云函数执行两次

firebase - Google Core IoT 设备离线事件或连接状态

javascript - 如何在 Firebase 函数中编写 Javascript GET 请求?

node.js - 如何使用 Google Cloud 函数获取 URL?要求?

node.js - 为什么这个可调用的云函数失败并返回 "app":"MISSING"?