【问题标题】:google.storage.object.finalize not triggering firebase cloud functiongoogle.storage.object.finalize 未触发 Firebase 云功能
【发布时间】:2019-06-30 08:35:09
【问题描述】:

我有一个 firebase 应用程序,当用户将照片上传到存储时,它会触发生成缩略图云功能。所有标准代码,运行良好,我于 2019 年 2 月 24 日部署。 现在,当我上传照片时,什么也没有发生。我查看存储,照片就在那里,但是当我查看 firebase 云函数的日志时,generateThumbnail 函数没有被调用。我该如何调试/修复这个?我正在考虑只是重新部署我的代码,或者升级我的库等以防发生重大变化?

这是我的代码:

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 });
        });
    });
  });

【问题讨论】:

    标签: firebase google-cloud-functions firebase-storage


    【解决方案1】:

    所有标准代码,运行良好,我于 2019 年 2 月 24 日部署。

    直到一个月左右之前,如果 Cloud Functions 闲置 30 天或更长时间,它们就会被系统停用。此行为已被更改,因为它对大多数开发人员来说非常不直观。但您需要再次重新部署 Cloud Functions 才能选择加入新行为。

    【讨论】:

    • 而不是重新部署,我只是“复制”(复制)相关功能并删除旧功能。谢谢:)
    猜你喜欢
    • 2021-11-27
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 2020-11-21
    相关资源
    最近更新 更多