本简易教程演示了如何使用 Cloud Storage 触发器编写、部署和触发 Cloud Functions 事件驱动函数来响应 Cloud Storage 事件。
如果您要查找使用 Cloud Storage 本身的代码示例,请访问 Google Cloud 示例浏览器。
目标
- 编写和部署 Cloud Functions 事件驱动函数。
- 通过将文件上传到 Cloud Storage 来触发函数。
费用
在本文档中,您将使用 Google Cloud 的以下收费组件:
- Cloud Functions
- Cloud Storage
您可使用价格计算器根据您的预计使用情况来估算费用。
如需在 Cloud Shell Editor 中直接遵循有关此任务的分步指导,请点击操作演示:
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目。
-
启用 Cloud Functions, Cloud Build, Cloud Storage, and Eventarc API。
- 安装 Google Cloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
-
在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目。
-
启用 Cloud Functions, Cloud Build, Cloud Storage, and Eventarc API。
- 安装 Google Cloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
- 准备开发环境:
Node.js
Python
Go
Java
C#
Ruby
PHP
如果您已经安装 gcloud CLI,请运行以下命令进行更新:
gcloud components update
准备应用
创建一个 Cloud Storage 存储分区以向其中上传测试文件,其中,
YOUR_TRIGGER_BUCKET_NAME
是全局唯一的存储分区名称:gsutil mb gs://YOUR_TRIGGER_BUCKET_NAME
将示例应用代码库克隆到本地机器:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
或者,您也可以下载该示例的 zip 文件并将其解压缩。
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
或者,您也可以下载该示例的 zip 文件并将其解压缩。
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
或者,您也可以下载该示例的 zip 文件并将其解压缩。
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
或者,您也可以下载该示例的 zip 文件并将其解压缩。
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
或者,您也可以下载该示例的 zip 文件并将其解压缩。
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
或者,您也可以下载该示例的 zip 文件并将其解压缩。
PHP
git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
或者,您也可以下载该示例的 zip 文件并将其解压缩。
切换到包含 Cloud Functions 函数示例代码的目录:
Node.js
cd nodejs-docs-samples/functions/helloworld/
Python
cd python-docs-samples/functions/helloworld/
Go
cd golang-samples/functions/helloworld/
Java
cd java-docs-samples/functions/helloworld/hello-gcs/
C#
cd dotnet-docs-samples/functions/helloworld/HelloGcs/
Ruby
cd ruby-docs-samples/functions/helloworld/storage/
PHP
cd php-docs-samples/functions/helloworld_storage/
部署和触发函数
目前,Cloud Storage 函数基于 Cloud Storage 发出的 Pub/Sub 通知,并且支持类似事件类型:
以下部分介绍如何为每种类型的事件部署和触发函数。
完成对象创建
当 Cloud Storage 对象的“写入”成功完成后,“完成对象创建”事件会被触发。具体而言,这意味着创建新对象或覆盖现有对象会触发此事件。此触发器会忽略归档操作和元数据更新操作。
完成对象创建:部署函数
查看处理 Cloud Storage 事件的示例函数:
Node.js
Python
Go
Java
C#
Ruby
PHP
如需部署该函数,请在示例代码所在的目录中运行以下命令:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs20 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
使用 --runtime
标志可以指定支持的 Node.js 版本的运行时 ID 来运行您的函数。
Python
gcloud functions deploy hello_gcs \ --runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
使用 --runtime
标志可以指定支持的 Python 版本的运行时 ID 来运行您的函数。
Go
gcloud functions deploy HelloGCS \ --runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
Java
gcloud functions deploy java-gcs-function \ --entry-point functions.HelloGcs \ --runtime java17 \ --memory 512MB \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
使用 --runtime
标志可以指定支持的 Java 版本的运行时 ID 来运行您的函数。
C#
gcloud functions deploy csharp-gcs-function \ --entry-point HelloGcs.Function \ --runtime dotnet6 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
使用 --runtime
标志可以指定支持的 .NET 版本的运行时 ID 来运行您的函数。
Ruby
gcloud functions deploy hello_gcs --runtime ruby32 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
使用 --runtime
标志可以指定支持的 Ruby 版本的运行时 ID 来运行您的函数。
PHP
gcloud functions deploy helloGCS --runtime php82 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
使用--runtime
标志以指定支持的 PHP 版本以运行您的函数。
其中,YOUR_TRIGGER_BUCKET_NAME
是触发函数的 Cloud Storage 存储分区的名称。
完成对象创建:触发函数
要触发函数,请执行以下操作:
在示例代码所在的目录中创建一个空
gcf-test.txt
文件。将该文件上传到 Cloud Storage 以触发函数:
gsutil cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
其中,
YOUR_TRIGGER_BUCKET_NAME
是您要向其中上传测试文件的 Cloud Storage 存储分区的名称。检查日志以确保执行已完成:
gcloud functions logs read --limit 50
对象删除
对于未启用版本控制的存储分区,对象删除事件最实用。这些事件会在对象的旧版本被删除时触发。另外,当对象被覆盖时,这些事件也会触发。对象删除触发器也可以用于启用了版本控制的存储分区。当对象的某个版本被永久删除时,会触发此类触发器。
对象删除:部署函数
使用与“完成创建”示例相同的示例代码,以对象删除作为触发器事件来部署函数。在示例代码所在的目录中运行以下命令:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs20 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
使用 --runtime
标志可以指定支持的 Node.js 版本的运行时 ID 来运行您的函数。
Python
gcloud functions deploy hello_gcs \ --runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
使用 --runtime
标志可以指定支持的 Python 版本的运行时 ID 来运行您的函数。
Go
gcloud functions deploy HelloGCS \ --runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
Java
gcloud functions deploy java-gcs-function \ --entry-point functions.HelloGcs \ --runtime java17 \ --memory 512MB \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
使用 --runtime
标志可以指定支持的 Java 版本的运行时 ID 来运行您的函数。
C#
gcloud functions deploy csharp-gcs-function \ --entry-point HelloGcs.Function \ --runtime dotnet6 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
使用 --runtime
标志可以指定支持的 .NET 版本的运行时 ID 来运行您的函数。
Ruby
gcloud functions deploy hello_gcs --runtime ruby32 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
使用 --runtime
标志可以指定支持的 Ruby 版本的运行时 ID 来运行您的函数。
PHP
gcloud functions deploy helloGCS --runtime php82 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
使用--runtime
标志以指定支持的 PHP 版本以运行您的函数。
其中,YOUR_TRIGGER_BUCKET_NAME
是触发函数的 Cloud Storage 存储分区的名称。
对象删除:触发函数
要触发函数,请执行以下操作:
在示例代码所在的目录中创建一个空
gcf-test.txt
文件。确保您的存储分区未启用版本控制:
gsutil versioning set off gs://YOUR_TRIGGER_BUCKET_NAME
将该文件上传到 Cloud Storage:
gsutil cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
其中,
YOUR_TRIGGER_BUCKET_NAME
是您要向其中上传测试文件的 Cloud Storage 存储分区的名称。此时,函数应该尚未执行。删除该文件以触发函数:
gsutil rm gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt
检查日志以确保执行已完成:
gcloud functions logs read --limit 50
请注意,函数可能需要一些时间才能执行完毕。
对象归档
对象归档事件只能用于启用了版本控制的存储分区。这些事件会在对象的旧版本被归档时触发。具体而言,这意味着当对象被覆盖或删除时,归档事件会被触发。
对象归档:部署函数
使用与“完成创建”示例相同的示例代码,以对象归档作为触发器事件来部署函数。在示例代码所在的目录中运行以下命令:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs20 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
使用 --runtime
标志可以指定支持的 Node.js 版本的运行时 ID 来运行您的函数。
Python
gcloud functions deploy hello_gcs \ --runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
使用 --runtime
标志可以指定支持的 Python 版本的运行时 ID 来运行您的函数。
Go
gcloud functions deploy HelloGCS \ --runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
Java
gcloud functions deploy java-gcs-function \ --entry-point functions.HelloGcs \ --runtime java17 \ --memory 512MB \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
使用 --runtime
标志可以指定支持的 Java 版本的运行时 ID 来运行您的函数。
C#
gcloud functions deploy csharp-gcs-function \ --entry-point HelloGcs.Function \ --runtime dotnet6 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
使用 --runtime
标志可以指定支持的 .NET 版本的运行时 ID 来运行您的函数。
Ruby
gcloud functions deploy hello_gcs --runtime ruby32 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
使用 --runtime
标志可以指定支持的 Ruby 版本的运行时 ID 来运行您的函数。
PHP
gcloud functions deploy helloGCS --runtime php82 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
使用--runtime
标志以指定支持的 PHP 版本以运行您的函数。
其中,YOUR_TRIGGER_BUCKET_NAME
是触发函数的 Cloud Storage 存储分区的名称。
对象归档:触发函数
要触发函数,请执行以下操作:
在示例代码所在的目录中创建一个空
gcf-test.txt
文件。确保您的存储分区已启用版本控制:
gsutil versioning set on gs://YOUR_TRIGGER_BUCKET_NAME
将该文件上传到 Cloud Storage:
gsutil cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
其中,
YOUR_TRIGGER_BUCKET_NAME
是您要向其中上传测试文件的 Cloud Storage 存储分区的名称。此时,函数应该尚未执行。归档该文件以触发函数:
gsutil rm gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt
查看日志以确保执行已完成:
gcloud functions logs read --limit 50
对象元数据更新
当现有对象的元数据更新时,元数据更新事件会被触发。
对象元数据更新:部署函数
使用与“完成创建”示例相同的示例代码,以元数据更新作为触发器事件来部署函数。在示例代码所在的目录中运行以下命令:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs20 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
使用 --runtime
标志可以指定支持的 Node.js 版本的运行时 ID 来运行您的函数。
Python
gcloud functions deploy hello_gcs \ --runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
使用 --runtime
标志可以指定支持的 Python 版本的运行时 ID 来运行您的函数。
Go
gcloud functions deploy HelloGCS \ --runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
Java
gcloud functions deploy java-gcs-function \ --entry-point functions.HelloGcs \ --runtime java17 \ --memory 512MB \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
使用 --runtime
标志可以指定支持的 Java 版本的运行时 ID 来运行您的函数。
C#
gcloud functions deploy csharp-gcs-function \ --entry-point HelloGcs.Function \ --runtime dotnet6 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
使用 --runtime
标志可以指定支持的 .NET 版本的运行时 ID 来运行您的函数。
Ruby
gcloud functions deploy hello_gcs --runtime ruby32 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
使用 --runtime
标志可以指定支持的 Ruby 版本的运行时 ID 来运行您的函数。
PHP
gcloud functions deploy helloGCS --runtime php82 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
使用--runtime
标志以指定支持的 PHP 版本以运行您的函数。
其中,YOUR_TRIGGER_BUCKET_NAME
是触发函数的 Cloud Storage 存储分区的名称。
对象元数据更新:触发函数
要触发函数,请执行以下操作:
在示例代码所在的目录中创建一个空
gcf-test.txt
文件。确保您的存储分区未启用版本控制:
gsutil versioning set off gs://YOUR_TRIGGER_BUCKET_NAME
将该文件上传到 Cloud Storage:
gsutil cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
其中,
YOUR_TRIGGER_BUCKET_NAME
是您要向其中上传测试文件的 Cloud Storage 存储分区的名称。此时,函数应该尚未执行。更新该文件的元数据:
gsutil -m setmeta -h "Content-Type:text/plain" gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt
查看日志以确保执行已完成:
gcloud functions logs read --limit 50
清理
为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。
删除项目
为了避免产生费用,最简单的方法是删除您为本教程创建的项目。
要删除项目,请执行以下操作:
- 在 Google Cloud 控制台中,进入管理资源页面。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击关闭以删除项目。
删除 Cloud Functions 函数
删除 Cloud Functions 函数不会移除存储在 Cloud Storage 中的任何资源。
如需删除您在本教程中创建的 Cloud Functions 函数,请运行以下命令:
Node.js
gcloud functions delete helloGCS
Python
gcloud functions delete hello_gcs
Go
gcloud functions delete HelloGCS
Java
gcloud functions delete java-gcs-function
C#
gcloud functions delete csharp-gcs-function
Ruby
gcloud functions delete hello_gcs
PHP
gcloud functions delete helloGCS
您也可以通过 Google Cloud Console 删除 Cloud Functions 函数。