本简易教程演示了如何使用 Cloud Storage 触发器编写、部署和触发 Cloud Run functions 事件驱动函数来响应 Cloud Storage 事件。
如果您要查找使用 Cloud Storage 本身的代码示例,请访问 Google Cloud 示例浏览器。
准备应用
创建一个 Cloud Storage 存储分区以向其中上传测试文件,其中,
YOUR_TRIGGER_BUCKET_NAME
是全局唯一的存储分区名称:gcloud storage buckets create 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 文件并将其解压缩。
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
或者,您也可以下载该示例的 zip 文件并将其解压缩。
切换到包含 Cloud Run 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/
Ruby
cd ruby-docs-samples/functions/helloworld/storage/
部署和触发函数
Cloud Storage 函数基于 Cloud Storage 发出的 Pub/Sub 通知,并且支持类似事件类型:
以下部分介绍如何为每种类型的事件部署和触发函数。
完成对象创建
当 Cloud Storage 对象的“写入”成功完成后,“完成对象创建”事件会被触发。具体而言,这意味着创建新对象或覆盖现有对象会触发此事件。此触发器会忽略归档操作和元数据更新操作。
完成对象创建:部署函数
查看处理 Cloud Storage 事件的示例函数:
Node.js
Python
Go
Java
Ruby
如需部署该函数,请在示例代码所在的目录中运行以下命令:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs22 \
--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 来运行您的函数。
Ruby
gcloud functions deploy hello_gcs --runtime ruby33 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
使用 --runtime
标志可以指定支持的 Ruby 版本的运行时 ID 来运行您的函数。
其中,YOUR_TRIGGER_BUCKET_NAME
是触发函数的 Cloud Storage 存储分区的名称。
完成对象创建:触发函数
要触发函数,请执行以下操作:
在示例代码所在的目录中创建一个空
gcf-test.txt
文件。将该文件上传到 Cloud Storage 以触发函数:
gcloud storage 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 nodejs22 \
--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 来运行您的函数。
Ruby
gcloud functions deploy hello_gcs --runtime ruby33 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
使用 --runtime
标志可以指定支持的 Ruby 版本的运行时 ID 来运行您的函数。
其中,YOUR_TRIGGER_BUCKET_NAME
是触发函数的 Cloud Storage 存储分区的名称。
对象删除:触发函数
要触发函数,请执行以下操作:
在示例代码所在的目录中创建一个空
gcf-test.txt
文件。确保您的存储分区未启用版本控制:
gcloud storage buckets update gs://YOUR_TRIGGER_BUCKET_NAME --no-versioning
将该文件上传到 Cloud Storage:
gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
其中,
YOUR_TRIGGER_BUCKET_NAME
是您要向其中上传测试文件的 Cloud Storage 存储桶的名称。此时,函数应该尚未执行。删除该文件以触发函数:
gcloud storage rm gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt
检查日志以确保执行已完成:
gcloud functions logs read --limit 50
请注意,函数可能需要一些时间才能执行完毕。
对象归档
当对象的当前版本变为非当前版本时,系统会触发对象归档事件。如果在启用了对象版本控制的存储桶中覆盖或删除对象,则会发生这种情况。
对象归档:部署函数
使用与“完成创建”示例相同的示例代码,以对象归档作为触发器事件来部署函数。在示例代码所在的目录中运行以下命令:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs22 \
--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 来运行您的函数。
Ruby
gcloud functions deploy hello_gcs --runtime ruby33 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
使用 --runtime
标志可以指定支持的 Ruby 版本的运行时 ID 来运行您的函数。
其中,YOUR_TRIGGER_BUCKET_NAME
是触发函数的 Cloud Storage 存储分区的名称。
对象归档:触发函数
要触发函数,请执行以下操作:
在示例代码所在的目录中创建一个空
gcf-test.txt
文件。确保您的存储分区已启用版本控制:
gcloud storage buckets update gs://YOUR_TRIGGER_BUCKET_NAME --versioning
将该文件上传到 Cloud Storage:
gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
其中,
YOUR_TRIGGER_BUCKET_NAME
是您要向其中上传测试文件的 Cloud Storage 存储桶的名称。此时,函数应该尚未执行。归档该文件以触发函数:
gcloud storage rm gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt
查看日志以确保执行已完成:
gcloud functions logs read --limit 50
对象元数据更新
当现有对象的元数据更新时,元数据更新事件会被触发。
对象元数据更新:部署函数
使用与“完成创建”示例相同的示例代码,以元数据更新作为触发器事件来部署函数。在示例代码所在的目录中运行以下命令:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs22 \
--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 来运行您的函数。
Ruby
gcloud functions deploy hello_gcs --runtime ruby33 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
使用 --runtime
标志可以指定支持的 Ruby 版本的运行时 ID 来运行您的函数。
其中,YOUR_TRIGGER_BUCKET_NAME
是触发函数的 Cloud Storage 存储分区的名称。
对象元数据更新:触发函数
要触发函数,请执行以下操作:
在示例代码所在的目录中创建一个空
gcf-test.txt
文件。确保您的存储分区未启用版本控制:
gcloud storage buckets update gs://YOUR_TRIGGER_BUCKET_NAME --no-versioning
将该文件上传到 Cloud Storage:
gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
其中,
YOUR_TRIGGER_BUCKET_NAME
是您要向其中上传测试文件的 Cloud Storage 存储桶的名称。此时,函数应该尚未执行。更新该文件的元数据:
gcloud storage objects update gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt --content-type=text/plain
查看日志以确保执行已完成:
gcloud functions logs read --limit 50