typescript - firebase 中存储触发的云功能是否有最大数量?
问题描述
到目前为止,我只有一个由 Firebase 存储上传触发的 CF:
export const onAvatarUpload_v1 = functions.storage.object().onFinalize(async (object) => {
// [... something that works, the actual code is at the end of the question]
}
每次客户端将对象上传到 Storage 时都会触发此函数。如果将此类对象上传到特定文件夹,则指向该资源(头像)的链接将保存到 firestore 集合中的文档中。
在我定义了一个也由存储上传触发的新 CF 之前,这工作得很好而且很花哨:
export const onUserPofilePictureUpdate_v1 = functions.storage.object().onFinalize(async (object, context) => {
// something that presumably works, but that I have not been able to try, the actual code is at the end of the question
}
现在,部署了两个 CF,这些 CF 在执行时生成的日志如下所示:
12:52:31.753 p. m.
assetsManagement-onAvatarUpload_v1
Function execution started
12:52:31.764 p. m.
assetsManagement-onAvatarUpload_v1
undefined
12:52:31.768 p. m.
assetsManagement-onAvatarUpload_v1
Function execution took 17 ms, finished with status: 'error'
我还没有测试过第二个 CF。但我确信在部署第二个之前,第一个 CF 有效。这就是让我认为可能存在一些限制的原因。
是否正确,通过存储上传可以触发多少个 CF 是否有限制?firebase中可能有错误吗?没有console.log
只能是 的语句undefined
,因为我总是将 CF 的名称添加到console.log
语句中。这表明我的代码甚至无法运行。
编辑:我正在添加两个云函数的代码。
//This is the one that worked until I deployed the other one.
export const onAvatarUpload_v1 = functions.storage.object().onFinalize(async (object) => {
try {
const filePath = object.name
if (!filePath) {
return Promise.reject(`onAvatarUpload_v1: no filePath`)
}
const baseFileName = path.basename(filePath, path.extname(filePath))
const fileDir = path.dirname(filePath)
if (fileDir !== 'assets/avatars') {
return Promise.reject('onAvatarUpload_v1: The file was uploaded to another folder')
} else {
console.log(`onAvatarUpload_v1: A file has been uploaded to the /assets/avatars folder`)
}
if (!object.contentType?.startsWith('image/')) {
console.log('onAvatarUpload_v1: Object is not an image. Deleting uploaded resource...')
const [response] = await admin.storage().bucket().delete(object)
const http2XXRegExp = /(2)\d{2}/
if (http2XXRegExp.test(response.statusCode.toString())) {
console.log(
`onAvatarUpload_v1: ${response.statusCode} Successfully deleted object. ${response.statusMessage}`
)
} else {
console.log(
`onAvatarUpload_v1: ${response.statusCode} Failed to delete object. ${response.statusMessage}`
)
}
}
if (!object.mediaLink) {
return Promise.reject(`onAvatarUpload_v1: object lacks mediaLink`)
}
const repository: IDataRepository = new FirestoreRepository()
const avatarWasCorrectlySaved: boolean = await repository.addAvatar(object.mediaLink)
if (avatarWasCorrectlySaved) {
return Promise.resolve(
`onAvatarUpload_v1: Avatar ${baseFileName} added to assets collection in Firestore`
)
} else {
return Promise.reject(
`onAvatarUpload_v1: Failed to add ${baseFileName} avatar to assets collection in Firestore`
)
}
} catch (err) {
console.error(err)
return Promise.reject(err)
}
})
//This is the one that I have not been able to try
export const onUserPofilePictureUpdate_v1 = functions.storage
.object()
.onFinalize(async (object, context) => {
try {
const userId = context.auth?.uid
if (!userId) {
return Promise.reject('onUserPofilePictureUpdate_v1: No auth provided')
}
const filePath = object.name
if (!filePath) {
return Promise.reject(`onUserPofilePictureUpdate_v1: no filePath`)
}
const baseFileName = path.basename(filePath, path.extname(filePath))
const fileDir = path.dirname(filePath)
if (fileDir !== 'assets/profile') {
return Promise.reject(
'onUserPofilePictureUpdate_v1: The file was uploaded to another folder'
)
} else {
console.log(`onUserPofilePictureUpdate_v1: new file uploaded to ${fileDir}`)
}
if (!object.contentType?.startsWith('image/') || baseFileName !== userId) {
console.log(
'onUserPofilePictureUpdate_v1: This is not an image or is incorrectly named. Deleting object...'
)
const [response] = await admin.storage().bucket().delete(object)
const http2XXRegExp = /(2)\d{2}/
if (http2XXRegExp.test(response.statusCode.toString())) {
console.log(
`onAvatarUpload_v1: ${response.statusCode} Successfully deleted object. ${response.statusMessage}`
)
} else {
console.log(
`onAvatarUpload_v1: ${response.statusCode} Failed to delete object. ${response.statusMessage}`
)
}
}
if (!object.mediaLink) {
return Promise.reject(`onUserPofilePictureUpdate_v1: object lacks mediaLink`)
}
const repository: IDataRepository = new FirestoreRepository()
const userProfilePicWasUpdated = await repository.updateUserProfilePicture(
userId,
object.mediaLink
)
if (userProfilePicWasUpdated) {
return Promise.resolve(
`onUserPofilePictureUpdate_v1: User's profile picture successfuly updated`
)
} else {
return Promise.reject(
`onUserPofilePictureUpdate_v1: Could not update the user's profile picture`
)
}
} catch (err) {
console.error(`onUserPofilePictureUpdate_v1: : ${err}`)
return Promise.reject(err)
}
})
由于我部署了这两个函数,我在这两个函数的日志中看到的唯一输出是:
8:45:29.305 a. m.
userManagement-onUserPofilePictureUpdate_v1
Function execution started
8:45:29.806 a. m.
userManagement-onUserPofilePictureUpdate_v1
undefined
8:45:29.810 a. m.
userManagement-onUserPofilePictureUpdate_v1
Function execution took 508 ms, finished with status: 'error'
和
8:49:34.067 a. m.
assetsManagement-onAvatarUpload_v1
Function execution started
8:49:34.085 a. m.
assetsManagement-onAvatarUpload_v1
undefined
8:49:34.086 a. m.
assetsManagement-onAvatarUpload_v1
Function execution took 21 ms, finished with status: 'error'
解决方案
推荐阅读
- c# - Xamarin.Mac - 通过从下拉列表中选择选项对 TableView 进行排序
- android - 具有列表和嵌套对象的 ViewModel 对象
- php - PHP:这个库如何拦截 curl 函数?
- bash - 为大文件优化 awk 脚本
- php - 通过 PHP 中的电子邮件正文发送带有变量的 url
- google-app-engine - 如何使用 Google Cloud Build 将 Spring Boot 项目部署到 Google App Engine?
- html - 响应列高度相同,标题、段落、按钮高度相同
- android - 从文档中读取 Firestore 值不起作用
- flask - 无法从 Kubernetes Engine 连接到 CloudSQL(无法连接到“localhost”上的 MySQL 服务器)
- mysql - 如果一个表中不存在 SQL 从另一个表中选择/插入一行