云功能:调整大小的图像未加载

Mr *_*Jax 3 node.js google-cloud-functions firebase-storage

我正在按照教程在上传时通过 Cloud Functions 调整图像大小,但遇到了两个我无法弄清楚的主要问题:

1) 如果上传了 PNG,它会生成大小正确的缩略图,但它们的预览不会在 Firestorage 中加载(加载微调器无限期显示)。它只在我点击“生成新的访问令牌”后显示图像(最初生成的缩略图都没有访问令牌)。

2) 如果上传了 JPEG 或任何其他格式,则 MIME 类型显示为“application/octet-stream”。我不确定如何正确提取扩展名以放入新生成的缩略图的文件名中?

export const generateThumbs = functions.storage
.object()
.onFinalize(async object => {
  const bucket = gcs.bucket(object.bucket);
  const filePath = object.name;
  const fileName = filePath.split('/').pop();
  const bucketDir = dirname(filePath);

  const workingDir = join(tmpdir(), 'thumbs');
  const tmpFilePath = join(workingDir, 'source.png');

  if (fileName.includes('thumb@') || !object.contentType.includes('image')) {
    console.log('exiting function');
    return false;
  }

  // 1. Ensure thumbnail dir exists
  await fs.ensureDir(workingDir);

 // 2. Download Source File
 await bucket.file(filePath).download({
   destination: tmpFilePath
 });

// 3. Resize the images and define an array of upload promises
 const sizes = [64, 128, 256];

 const uploadPromises = sizes.map(async size => {
  const thumbName = `thumb@${size}_${fileName}`;
  const thumbPath = join(workingDir, thumbName);

  // Resize source image
   await sharp(tmpFilePath)
    .resize(size, size)
    .toFile(thumbPath);

  // Upload to GCS
  return bucket.upload(thumbPath, {
    destination: join(bucketDir, thumbName)
  });
});

// 4. Run the upload operations
  await Promise.all(uploadPromises);

// 5. Cleanup remove the tmp/thumbs from the filesystem
  return fs.remove(workingDir);
  });
Run Code Online (Sandbox Code Playgroud)

非常感谢任何反馈!

Som*_*ody 7

我刚刚遇到了同样的问题,不知出于什么原因,Firebase 的调整图像大小故意从调整大小的图像中删除下载令牌

禁用删除下载访问令牌

  • 转到https://console.cloud.google.com
  • 从左侧选择Cloud Functions
  • 选择ext-storage-resize-images-generateResizedImage
  • 点击编辑
  • 从内联编辑器转到文件FUNCTIONS/LIB/INDEX.JS
  • 在此行之前添加 // ( delete metadata.metadata.firebaseStorageDownloadTokens; )
  • 对该文件的同一行也进行注释FUNCTIONS/SRC/INDEX.TS
  • DEPLOY并等待它完成

注意:原始和调整大小将具有相同的令牌。

  • 效果很好,谢谢!真的很想知道为什么他们要为此删除令牌...... (2认同)
  • 该行已在扩展的 0.1.7 版本中删除。如果您只需更新到该版本(或更高版本),您的问题就会得到解决:) (2认同)