NodeJS 是否可以从云存储触发函数返回一个值,例如图像URL

68bkxrlz  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(76)
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;

xbp102n0

xbp102n01#

您的onFinalize函数仅在图像完全上传时触发。客户端没有直接的方式来等待您在该云函数中完成的任何操作。
将信息从Cloud Functions传送回客户端的常见方式是通过Firestore或Realtime Database。

相关问题