const bucket = admin.storage().bucket(fileBucket);
const metadata = {
contentType: contentType,
};
const downloadResponse = await bucket.file(filePath).download();
const imageBuffer = downloadResponse\[0\];
functions.logger.log("Image downloaded!");
// Generate a thumbnail using sharp.
const thumbnailBuffer = await sharp(imageBuffer).resize({
width: 200,
height: 200,
withoutEnlargement: true,
}).toBuffer();
functions.logger.log("Thumbnail created");
// Upload the thumbnail with a 'thumb\_' prefix.
const thumbFileName = thumb\ _$ {
fileName
};
const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
await bucket.file(thumbFilePath).save(thumbnailBuffer, {
metadata: metadata,
});
return functions.logger.log("Thumbnail uploaded!");
});
字符串
我尝试了以下解决方案
// Download file from bucket.
const bucket = admin.storage().bucket(fileBucket);
const metadata = {
contentType: contentType,
};
const downloadResponse = await bucket.file(filePath).download();
const imageBuffer = downloadResponse\[0\];
functions.logger.log("Image downloaded!");
// Generate a thumbnail using sharp.
const thumbnailBuffer = await sharp(imageBuffer).resize({
width: 200,
height: 200,
withoutEnlargement: true,
}).toBuffer();
functions.logger.log("Thumbnail created");
// Upload the thumbnail with a 'thumb\_' prefix.
const thumbFileName = thumb\ _$ {
fileName
};
const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
await bucket.file(thumbFilePath).save(thumbnailBuffer, {
metadata: metadata,
});
return // potential bucket file image url;
型
1条答案
按热度按时间xbp102n01#
您的
onFinalize
函数仅在图像完全上传时触发。客户端没有直接的方式来等待您在该云函数中完成的任何操作。将信息从Cloud Functions传送回客户端的常见方式是通过Firestore或Realtime Database。