简体   繁体   English

使用Firebase Cloud功能检查存储中是否存在映像

[英]Check if image exists at storage with firebase cloud function

i need your help with one function that i createed to manipulate the images that my users send to my app. 我需要一个我创建的功能来处理用户发送到我的应用程序的图像,这需要您的帮助。

What i need is get the image that the user sent, resize and check if the image was changed to avoid the function to do all again. 我需要的是获取用户发送的图像,调整大小并检查图像是否已更改以避免该功能再次执行。 The examples that i saw change the image name and check if the beggined if the name is equals with the name set, but in my case i need keep the original name of the picture, So, how can i do that? 我看到的示例更改了图像名称,并检查了该名称是否与设置的名称相等,但是在我的情况下,我需要保留图片的原始名称,那么,该怎么办? Or exists a better way to solve this problem? 还是存在解决此问题的更好方法? My function code is: 我的功能代码是:

  import * as functions from 'firebase-functions';

  import * as Storage from '@google-cloud/storage';
  const gcs = new Storage();

  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 fileName = filePath.split('/').pop();
    const bucketDir = dirname(filePath);

    const workingDir = join(tmpdir(), 'thumbs');
    const tmpFilePath = join(workingDir, 'source.png');

    if (!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 = [64, 128, 256];

    const uploadPromises = sizes.map(async size => {
      const thumbName = `thumb@${size}_${fileName}`;
      const thumbPath = join(workingDir, thumbName);

      // Resize source image
      await sharp(tmpFilePath)
        .resize(size, size)
        .toFile(thumbPath);

      // Upload to GCS
      return bucket.upload(thumbPath, {
        destination: join(bucketDir, thumbName)
      });
    });

    // 4. Run the upload operations
    await Promise.all(uploadPromises);

    // 5. Cleanup remove the tmp/thumbs from the filesystem
    return fs.remove(workingDir);
  });

If you need to overwrite the original file, and you want to avoid an infinite loop with this function, you could attach custom metadata to the file when you upload it back to Cloud Storage. 如果您需要覆盖原始文件,并且希望避免使用此功能造成无限循环,则可以在将文件上传回Cloud Storage时将自定义元数据附加到文件。 Then, when the function is invoked again for that file, you can check the metadata on the incoming ObjectMetadata object to know when the function should bail out without making any more changes. 然后,当对该文件再次调用该函数时,可以检查传入的ObjectMetadata对象上的元数据,以了解何时应退出该函数,而无需进行任何其他更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用云删除 Firebase 存储映像 Function - Unable to delete Firebase Storage Image with Cloud Function 从可调用的 https 云 function 将图像上传到 Firebase 存储 - Upload an image into Firebase Storage from a callable https cloud function 通过云功能检索Firebase存储图像链接 - Retrieving a Firebase storage image link via Cloud Function 使用Google Cloud AutoML模型预测Firebase功能存储在Google Cloud存储中的图像 - Use Google Cloud AutoML model predict an image which is stored in Google Cloud storage in Firebase function 从Cloud功能的Firebase存储下载时出错 - Error downloading from firebase Storage in a Cloud Function 如何将映像从Expressjs更新到Google Cloud Storage / Firebase Storage - How update an image from Expressjs to Google Cloud Storage / Firebase Storage firebase 存储上上传的缺少图像(使用云 function 更新 Firestore DB) - Missing image uploaded on firebase storage (Using cloud function to update firestore DB) Cloud Storage for Firebase 访问错误“admin.storage(...).ref is not a function” - Cloud Storage for Firebase access error “admin.storage(…).ref is not a function” 谷歌云存储的 firebase 云函数 API 错误 - firebase cloud function API Error with Google Cloud Storage 如何使用 Cloud Function 从 Cloud Storage 获取 Firebase 的 object? - How to get object from Cloud Storage for Firebase using Cloud Function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM