简体   繁体   English

如何使用云功能制作缩略图并将其放置在Firebase存储中的特定文件夹中?

[英]how to make thumbnail and place it in the particular folder in Firebase storage using cloud function?

there is a sample code from firebase about how to create thumbnail in here: firebase提供了一个有关如何在此处创建缩略图的示例代码:

https://github.com/firebase/functions-samples/blob/master/quickstarts/thumbnails/functions/index.js https://github.com/firebase/functions-samples/blob/master/quickstarts/thumbnails/functions/index.js

the code is like this 代码是这样的

// [START import]
const functions = require('firebase-functions');
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');
// [END import]

// [START generateThumbnail]
/**
 * When an image is uploaded in the Storage bucket We generate a thumbnail automatically using
 * ImageMagick.
 */
// [START generateThumbnailTrigger]
exports.generateThumbnail = functions.storage.object().onFinalize((object) => {
// [END generateThumbnailTrigger]
  // [START eventAttributes]
  const fileBucket = object.bucket; // The Storage bucket that contains the file.
  const filePath = object.name; // File path in the bucket.
  const contentType = object.contentType; // File content type.
  const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.
  // [END eventAttributes]

  // [START stopConditions]
  // 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;
  }

  // Get the file name.
  const fileName = path.basename(filePath);
  // Exit if the image is already a thumbnail.
  if (fileName.startsWith('thumb_')) {
    console.log('Already a Thumbnail.');
    return null;
  }
  // [END stopConditions]

  // [START thumbnailGeneration]
  // Download file from bucket.
  const bucket = gcs.bucket(fileBucket);
  const tempFilePath = path.join(os.tmpdir(), fileName);
  const metadata = {
    contentType: contentType,
  };
  return bucket.file(filePath).download({
    destination: tempFilePath,
  }).then(() => {
    console.log('Image downloaded locally to', tempFilePath);
    // Generate a thumbnail using ImageMagick.
    return spawn('convert', [tempFilePath, '-thumbnail', '200x200>', tempFilePath]);
  }).then(() => {
    console.log('Thumbnail created at', tempFilePath);
    // We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail.
    const thumbFileName = `thumb_${fileName}`;
    const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
    // Uploading the thumbnail.
    return bucket.upload(tempFilePath, {
      destination: thumbFilePath,
      metadata: metadata,
    });
    // Once the thumbnail has been uploaded delete the local file to free up disk space.
  }).then(() => fs.unlinkSync(tempFilePath));
  // [END thumbnailGeneration]
});
// [END generateThumbnail]

but after trying the code above, it seems the thumbnail created is located in the same folder as the original image (profile image) like the picture below 但是在尝试了上面的代码之后,似乎创建的缩略图与原始图像(配置文件图像)位于同一文件夹中,如下图所示

在此处输入图片说明

what should I do if I want the thumbnail is located in the different folder named 'thumbnail'? 如果我希望缩略图位于名为“ thumbnail”的另一个文件夹中,该怎么办? so if upload the image to 'profileImage' folder, the thumbnail will be placed to 'thumbnail' folder 因此,如果将图像上传到“ profileImage”文件夹,则缩略图将放置在“缩略图”文件夹中

在此处输入图片说明

The code you need to modify is here: 您需要修改的代码在这里:

const thumbFileName = `thumb_${fileName}`;
const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
// Uploading the thumbnail.
return bucket.upload(tempFilePath, {
  destination: thumbFilePath,
  metadata: metadata,
});

It's building a path to the location in Cloud Storage where the file should be uploaded in thumbFilePath . 它正在构建到Cloud Storage中应该在thumbFilePath上传文件的位置的路径。 You can see that it's joining the directory of filePath and file name thumbFileName . 您会看到它正在加入filePath目录和文件名thumbFileName filePath was defined as the location of the original file at the beginning: filePath定义为原始文件在开头的位置:

const filePath = object.name; // File path in the bucket.

All this means that wherever the original file was uploaded, the thumbnail will be organized right next to it. 所有这些意味着,无论原始文件上传到哪里,缩略图都会被组织在其旁边。

If you want to change the location of the final thumbnail, make thumbFilePath contain that location. 如果要更改最终缩略图的位置,请使thumbFilePath包含该位置。 It might be as simple as changing it like this: 它可能像这样更改它一样简单:

const thumbFilePath = `thumbnail/${thumbFileName}`

Or whatever you need it to be. 或任何您需要的东西。

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

相关问题 如何从 Firebase 云 Function 中删除 Firebase 存储文件夹? - How to delete a Firebase Storage folder from a Firebase Cloud Function? 如何使用Firebase云功能存储创建文件夹 - How can make folder with Firebase Cloud Functions Storage Cloud Function 存储在特定 Bucket 文件夹上的触发器 - Cloud Function storage trigger on folder of a particular Bucket 如何使用 Cloud Function 从 Cloud Storage 获取 Firebase 的 object? - How to get object from Cloud Storage for Firebase using Cloud Function? 如何使用函数从 Firebase 存储中删除文件夹? - How do I delete a folder from Firebase Storage using a function? 如何从云功能访问 firebase 存储中的特定文件夹? - How can I access a specific folder inside firebase storage from a cloud function? 如何在存储具有完全读/写管理员权限的情况下使Firebase Cloud功能正常运行? - How to make a Firebase Cloud function with full read/write admin powers on storage? 使用Firebase云功能删除用户存储文件夹 - Remove users storage folder with firebase cloud functions 如何测量云 Function 中 Firebase 中特定代码块的执行时间? - How to measure execution time of a particular block of code in Cloud Function for Firebase? 如何避免在云存储事件中对文件夹执行Firebase功能 - How to avoid performing a firebase function on folders on cloud storage events
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM