【问题标题】:Firebase cloud function running into infinite loopFirebase云功能陷入无限循环
【发布时间】:2019-03-03 20:55:34
【问题描述】:

我正在尝试制作一个云功能,该功能可以侦听上传到 Firebase 存储桶的图像并触发该功能来调整图像大小。我只是拍摄原始图像并用相同的名称编写它,从而更新相同的图像。这是我的代码,

let functions = require('firebase-functions');

let admin = require('firebase-admin');

admin.initializeApp();


const mkdirp = require('mkdirp-promise');

const gcs = require('@google-cloud/storage')();

const spawn = require('child-process-promise').spawn;
const path = require('path');
const os = require('os');
const fs = require('fs');


const THUMB_MAX_HEIGHT = 1000;
const THUMB_MAX_WIDTH = 1000;

const THUMB_PREFIX = '';


exports.generateThumbnail = functions.storage.object().onFinalize((object) => {
// File and directory paths.
const filePath = object.name;
const contentType = object.contentType; // This is the image MIME type
const fileDir = path.dirname(filePath);
const fileName = path.basename(filePath);
const thumbFilePath = path.normalize(path.join(fileDir, `${fileName}`));
const tempLocalFile = path.join(os.tmpdir(), filePath);
const tempLocalDir = path.dirname(tempLocalFile);
const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);

// Exit if this is triggered on a file that is not an image.
if (!contentType.startsWith('image/')) {
console.log('This is not an image.');
return null;
}

// Exit if the image is already a thumbnail.


// Cloud Storage files.
const bucket = gcs.bucket(object.bucket);
const file = bucket.file(filePath);
const thumbFile = bucket.file(thumbFilePath);
const metadata = {
contentType: contentType,
// To enable Client-side caching you can set the Cache-Control headers here. Uncomment below.
// 'Cache-Control': 'public,max-age=3600',
};

// Create the temp directory where the storage file will be downloaded.

return mkdirp(tempLocalDir).then(() => {
// Download file from bucket.
return file.download({destination: tempLocalFile});
}).then(() => {
console.log('The file has been downloaded to', tempLocalFile);
// Generate a thumbnail using ImageMagick.
return spawn('convert', [tempLocalFile, '-thumbnail', 
`${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile], {capture: 
['stdout', 'stderr']});
}).then(() => {
console.log('Thumbnail created at', tempLocalThumbFile);
// Uploading the Thumbnail.
return bucket.upload(tempLocalThumbFile, {destination: thumbFilePath, 
metadata: metadata});
}).then(() => {
console.log('Thumbnail uploaded to Storage at', thumbFilePath);
// Once the image has been uploaded delete the local files to free up disk space.
fs.unlinkSync(tempLocalFile);
fs.unlinkSync(tempLocalThumbFile);
// Get the Signed URLs for the thumbnail and original image.

const config = {
  action: 'read',
  expires: '03-01-2500',
};

return Promise.all([
  thumbFile.getSignedUrl(config),
  file.getSignedUrl(config),
]);

}).then((results) => {
console.log('Got Signed URLs.');
const thumbResult = results[0];
const originalResult = results[1];
const thumbFileUrl = thumbResult[0];
const fileUrl = originalResult[0];

// Add the URLs to the Database

return admin.database().ref('images').push({path: fileUrl, thumbnail: 
thumbFileUrl});
}).then(() => console.log('Thumbnail URLs saved to database.'));
});

I 函数继续执行并最终停止并显示超出配额的消息。我认为用同名书写的想法导致了问题。但这都是我想要的。有没有办法停止这个循环?

【问题讨论】:

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


    【解决方案1】:

    您必须在函数中编写一些代码,以检查您上传回存储桶的文件是否不应进一步处理,提前退出。这正是sample code 中正在发生的事情,您可能已经复制了它以获取您现在所在的位置。除非您需要一种不同的方法来确定该功能是否提前停止。也许看看文件的名称或路径。

    【讨论】:

      猜你喜欢
      • 2017-12-05
      • 2020-05-13
      • 1970-01-01
      • 2021-05-16
      • 2013-03-17
      • 2021-02-15
      相关资源
      最近更新 更多