[英]How to delete a Firebase Storage folder from a Firebase Cloud Function?
[英]how to make thumbnail and place it in the particular folder in Firebase storage using cloud function?
firebase提供了一個有關如何在此處創建縮略圖的示例代碼:
https://github.com/firebase/functions-samples/blob/master/quickstarts/thumbnails/functions/index.js
代碼是這樣的
// [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]
但是在嘗試了上面的代碼之后,似乎創建的縮略圖與原始圖像(配置文件圖像)位於同一文件夾中,如下圖所示
如果我希望縮略圖位於名為“ thumbnail”的另一個文件夾中,該怎么辦? 因此,如果將圖像上傳到“ profileImage”文件夾,則縮略圖將放置在“縮略圖”文件夾中
您需要修改的代碼在這里:
const thumbFileName = `thumb_${fileName}`;
const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
// Uploading the thumbnail.
return bucket.upload(tempFilePath, {
destination: thumbFilePath,
metadata: metadata,
});
它正在構建到Cloud Storage中應該在thumbFilePath
上傳文件的位置的路徑。 您會看到它正在加入filePath
目錄和文件名thumbFileName
。 filePath
定義為原始文件在開頭的位置:
const filePath = object.name; // File path in the bucket.
所有這些意味着,無論原始文件上傳到哪里,縮略圖都會被組織在其旁邊。
如果要更改最終縮略圖的位置,請使thumbFilePath
包含該位置。 它可能像這樣更改它一樣簡單:
const thumbFilePath = `thumbnail/${thumbFileName}`
或任何您需要的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.