您可以触发一个函数来响应 Cloud Storage 中上传、更新或删除文件和文件夹的操作。
本页中的示例基于一个在图片文件上传到 Cloud Storage 时触发的示例函数。此示例函数演示了如何访问事件特性,如何将文件下载到 Cloud Functions 实例,以及其他有关如何处理 Cloud Storage 事件的基础知识。
如需查看更多使用场景示例,请参阅“Cloud Functions 有哪些用途?”。
在发生 Cloud Storage 更改时触发函数
使用 functions.storage
创建用于处理 Cloud Storage 事件的函数。根据您是要将函数范围限定在特定的 Cloud Storage 存储桶还是使用默认存储桶,请使用下列选项之一:
functions.storage.object()
,用于监听默认 Cloud Storage 存储桶中对象的更改。functions.storage.bucket('bucketName').object()
,用于监听特定存储桶中对象的更改。
例如,缩略图生成器示例的范围限定至项目的默认存储桶:
exports.generateThumbnail = functions.storage.object().onFinalize(async (object) => { // ... });
Cloud Storage 支持以下事件:
onArchive
:仅在存储桶已启用对象版本控制时发送。此事件表明某个对象的当前版本已变为归档版本,原因可能是该对象已归档,或者已被新上传的同名对象覆盖。onDelete
:当某个对象被永久删除时发送。这包括系统根据存储桶的生命周期配置所覆盖或删除的对象。对于启用了对象版本控制的存储桶,如果某个对象已归档(请参阅onArchive
),则系统不会发送此事件,即使通过storage.objects.delete
方法进行归档也是如此。onFinalize
:当存储桶中成功创建了新对象(或现有对象的新实例)时发送。这包括复制或重写现有的对象。上传失败不会触发此事件。onMetadataUpdate
:当现有对象的元数据发生更改时发送。
请在 on
事件处理程序内设置事件,如上文 onFinalize
的说明中所示。
访问 Cloud Storage 对象特性
Cloud Functions 公开了多个 Cloud Storage 对象特性,例如已更新文件的 size
和 contentType
。每当对象的元数据发生变化时,metageneration 特性的值都会递增。对于新对象,metageneration
值为 1
。
const fileBucket = object.bucket; // The Storage bucket that contains the file. const filePath = object.name; // File path in the bucket. const contentType = object.contentType; // File content type. const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.
缩略图生成器示例使用了上述部分特性来检测要让函数返回的退出情况:
// Exit if this is triggered on a file that is not an image. if (!contentType.startsWith('image/')) { return functions.logger.log('This is not an image.'); } // Get the file name. const fileName = path.basename(filePath); // Exit if the image is already a thumbnail. if (fileName.startsWith('thumb_')) { return functions.logger.log('Already a Thumbnail.'); }
下载、转换和上传文件
在某些情况下,您可能不需要从 Cloud Storage 下载文件。但是,执行密集型任务(例如根据 Cloud Storage 中存储的文件生成缩略图)时,您需要将文件下载到函数实例(即运行代码的虚拟机)。
为了能轻松下载对象并将其重新上传到 Cloud Storage,请使用 npm install --save @google-cloud/storage
安装 Google Cloud Storage 软件包并将其导入。如需使用 JavaScript promise 来处理外部进程(例如示例中的缩略图处理任务),还应导入 child-process-promise
:
const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp() const spawn = require('child-process-promise').spawn; const path = require('path'); const os = require('os'); const fs = require('fs');
使用 gcs.bucket.file(filePath).download
可将文件下载到 Cloud Functions 实例的临时目录中。在此位置,您可以根据需要处理文件,然后将结果上传到 Cloud Storage。执行异步任务时,请确保在回调中返回 JavaScript promise。
示例:图片转换
Cloud Functions 提供了一个名为 ImageMagick
的图片处理程序,可用于对图片文件进行操作。以下示例说明了如何为上传的图片文件创建缩略图:
// Download file from bucket. const bucket = admin.storage().bucket(fileBucket); const tempFilePath = path.join(os.tmpdir(), fileName); const metadata = { contentType: contentType, }; await bucket.file(filePath).download({destination: tempFilePath}); functions.logger.log('Image downloaded locally to', tempFilePath); // Generate a thumbnail using ImageMagick. await spawn('convert', [tempFilePath, '-thumbnail', '200x200>', tempFilePath]); functions.logger.log('Thumbnail created at', tempFilePath); // We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail. const thumbFileName = `thumb_${fileName}`; const thumbFilePath = path.join(path.dirname(filePath), thumbFileName); // Uploading the thumbnail. await bucket.upload(tempFilePath, { destination: thumbFilePath, metadata: metadata, }); // Once the thumbnail has been uploaded delete the local file to free up disk space. return fs.unlinkSync(tempFilePath);
这段代码会执行 ImageMagick
命令行程序 convert
,为保存在临时目录中的图片创建大小为 200x200 的缩略图,然后将缩略图传回 Cloud Storage。
浏览更多示例
常见媒体转换函数的更多示例包括图片转码、内容审核和 EXIF 元数据提取。您可在 GitHub 上找到完整的示例列表。
如需了解详情,请参阅完整的 Google Cloud Storage 触发器文档。