直到现在,我只有一个由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]
}
每次客户端将对象上载到存储时,都会触发此功能。如果将此类对象上载到特定文件夹,则指向该资源(头像)的链接将保存到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可以工作。这就是让我认为可能存在一些限制的原因。
是否正确?通过存储上传可以触发多少个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'