google.storage.object.finalize未触发firebase云功能
我有一个firebase应用程序,当用户将照片上传到存储时,它会触发generatethumbnail云功能。所有标准代码,运行良好,我于2019年2月24日部署。 现在当我上传一张照片时,什么也没发生。我查看了存储,照片就在那里,但是当我查看firebase云函数的日志时,generateThumbnail函数还没有被调用。如何调试/修复此问题?我在考虑重新部署我的代码,或者升级我的库等,以防出现突破性的变化 这是我的密码:google.storage.object.finalize未触发firebase云功能,firebase,google-cloud-functions,firebase-storage,Firebase,Google Cloud Functions,Firebase Storage,我有一个firebase应用程序,当用户将照片上传到存储时,它会触发generatethumbnail云功能。所有标准代码,运行良好,我于2019年2月24日部署。 现在当我上传一张照片时,什么也没发生。我查看了存储,照片就在那里,但是当我查看firebase云函数的日志时,generateThumbnail函数还没有被调用。如何调试/修复此问题?我在考虑重新部署我的代码,或者升级我的库等,以防出现突破性的变化 这是我的密码: import * as functions from 'fireba
import * as functions from 'firebase-functions';
// import * as Storage from '@google-cloud/storage';
// const gcs = new Storage();
import * as admin from 'firebase-admin';
const gcs = admin.storage()
const firestore = admin.firestore();
import { tmpdir } from 'os';
import { join, dirname } from 'path';
import * as sharp from 'sharp';
import * as fs from 'fs-extra';
export const generateThumbs = functions.storage
.object()
.onFinalize(async object => {
const bucket = gcs.bucket(object.bucket);
const filePath = object.name;
const parts = filePath.split('/');
const fileName = parts.pop();
const propertyID = parts.pop();
// console.log(`got property id ${propertyID}`)
const bucketDir = dirname(filePath);
const workingDir = join(tmpdir(), 'thumbs');
const tmpFilePath = join(workingDir, fileName);
if (fileName.includes('thumb@') || !object.contentType.includes('image')) {
console.log('exiting function');
return false;
}
// 1. Ensure thumbnail dir exists
await fs.ensureDir(workingDir);
// 2. Download Source File
await bucket.file(filePath).download({
destination: tmpFilePath
});
// 3. Resize the images and define an array of upload promises
const sizes = [256];
let thumbLocation = '';
const uploadPromises = sizes.map(async size => {
const thumbName = `thumb@${size}_${fileName}`;
const thumbPath = join(workingDir, thumbName);
// Resize source image
await sharp(tmpFilePath)
.resize(256, 171)
.toFile(thumbPath);
thumbLocation = join(bucketDir, thumbName);
// Upload to GCS
return bucket.upload(thumbPath, {
destination: thumbLocation
});
});
// 4. Run the upload operations
await Promise.all(uploadPromises);
// 5. Cleanup remove the tmp/thumbs from the filesystem
await fs.remove(workingDir);
let photoURL = ''
const hour = 1000 * 60 * 60;
const year = hour * 24 * 365;
const EXP = Date.now() + year * 10;
await bucket.file(filePath).getSignedUrl({
action: 'read',
expires: EXP
}).then(signedUrls => {
photoURL = signedUrls[0];
});
let thumbURL = '';
await bucket.file(thumbLocation).getSignedUrl({
action: 'read',
expires: EXP
}).then(signedUrls => {
thumbURL = signedUrls[0];
});
if (!(photoURL && thumbURL)) {
return Promise.resolve('Error no thumbs');
}
const propertyRef = firestore.collection('properties').doc(propertyID);
return firestore.runTransaction(t => {
return t.get(propertyRef)
.then(doc => {
if (!doc.exists) {
console.log(`doc does not exist ${propertyID}`)
return;
}
let photos = doc.data().photos;
photos = photos || [];
photos.push({
big: photoURL,
small: thumbURL,
});
t.update(propertyRef, { photos: photos });
});
});
});
所有标准代码,运行良好,我于2019年2月24日部署
直到大约一个月前,如果云功能在30天或更长时间内处于非活动状态,则系统会将其停用。这种行为后来被改变了,因为它对大多数开发人员来说非常不直观。但您需要再次重新部署云功能以选择新的行为。我没有重新部署,而是“复制”(复制)了相关功能并删除了旧功能。谢谢:)