能否从Cloud Storage触发函数返回图片URL这类值?
问题:能否从Cloud Storage触发的Cloud Functions返回图片URL?
我想知道是否可以从Cloud Storage触发的云函数中返回存储桶里的图片URL。以下是我当前的实现代码:
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!"); });
我尝试修改代码,希望能返回存储桶中的图片URL:
// 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;
解答
当然可以返回图片URL,根据文件权限需求,有两种实现方式:
1. 文件设为公开可读
如果缩略图允许公开访问,可直接构造公开URL:
// 上传完成后获取文件实例 const thumbFile = bucket.file(thumbFilePath); // 构造公开URL const publicUrl = `https://storage.googleapis.com/${bucket.name}/${thumbFile.name}`; // 返回URL return publicUrl;
注意:需要在上传时设置文件为公开可读权限:
await bucket.file(thumbFilePath).save(thumbnailBuffer, { metadata: metadata, predefinedAcl: 'publicRead' // 添加此行设置公开权限 });
2. 文件保持私有(生成签名URL)
如果文件需要私有访问,可生成带过期时间的签名URL:
const thumbFile = bucket.file(thumbFilePath); // 生成签名URL,设置1小时过期 const [signedUrl] = await thumbFile.getSignedUrl({ action: 'read', expires: '1h' // 支持自定义过期时间,如'2024-12-31' }); return signedUrl;
完整修改后的代码示例
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, predefinedAcl: 'publicRead' }); // 构造并返回公开URL const publicUrl = `https://storage.googleapis.com/${bucket.name}/${thumbFilePath}`; functions.logger.log("Thumbnail uploaded! URL:", publicUrl); return publicUrl;
内容的提问来源于stack exchange,提问作者Samuel Airadion
相关产品推荐
相关产品推荐

