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

问题描述 投票:0回答:1
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;
node.js firebase google-cloud-functions google-cloud-storage
1个回答
0
投票

您的

onFinalize
函数仅在图像完全上传后才会触发。客户端没有直接的方法来等待您在云功能中所做的任何事情完成。

将信息从 Cloud Functions 传回客户端的常用方法是通过 Firestore 或实时数据库。

© www.soinside.com 2019 - 2024. All rights reserved.