google.storage.object.finalize不触发Firebase云功能

罗伯特·金

我有一个firebase应用程序,当用户将照片上传到存储设备时,它会触发generatethumbnail云功能。所有标准代码都运行良好,我于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 });
        });
    });
  });

在此处输入图片说明 在此处输入图片说明

弗兰克·范普菲伦

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

如果Cloud Functions在30天内或更长时间处于非活动状态,则会在系统停用之前,直到一个月左右。此行为已更改,因为对于大多数开发人员而言,这非常不直观。但是您将需要再次重新部署您的Cloud Functions以选择新行为。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google Cloud Storage上传会触发Firebase Cloud功能吗?

无法使用Firebase功能删除Google Cloud Storage文件

Google Firebase云功能+ Firestore

使用storage.objectAccessControls.insert进行Google云存储

无法使用默认服务帐户和Google云库从Google Kubernetes Engine访问Google Cloud Storage

我可以在本地运行Firebase Storage云功能吗?

Google Cloud Storage对象完成事件多次触发

Google / Firebase云功能的速率限制?

如何在Terraform 13.5中将变量传递到google_storage_bucket_object

导入@ google-cloud / storage部署Firebase功能时,p-limit lib错误

Firebase Admin SDK 上传文件到 Google Cloud Storage

在Firebase Google Cloud Storage Bucket上配置CORS

如何获取Firebase / Google Cloud Storage存储下载分析

如何在Google Spreadsheet和Firebase Storage之间进行读写?

尝试使用Superbalist / flysystem-google-cloud-storage上传到Google云存储

Google Storage使用您自己的程序而不是使用Google云库来创建Signed Urls

如何测试使用存储上传触发的Google云功能?

Google-Cloud-Scheduler因http触发云功能而失败

IBM Cloud Object Storage 凭证

Google Cloud Functions-Cloud Storage存储桶触发器延迟触发

我可以手动触发Google Cloud Storage事件触发器吗?

问题与List(); Firebase Storage的功能

Google Storage API中的死锁

Google Cloud Storage ACL混淆

Google Cloud Storage Force下载

PHP 的 Google Cloud Storage 问题

Google Cloud Storage - 权限不足

刷新Google Storage Client的令牌

Firebase云功能-@ google-cloud /存储初始化