【问题标题】:Cloud Functions: Resized images not loading云功能:调整大小的图像未加载
【发布时间】:2020-03-23 04:01:46
【问题描述】:

我正在关注tutorial,以便在上传时通过 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);
  });

非常感谢任何反馈!

【问题讨论】:

    标签: node.js google-cloud-functions firebase-storage


    【解决方案1】:

    2020 年 11 月

    关于@Somebody 的回答,我似乎在GCP Cloud Functions 中找不到ext-storage-resize-images-generateResizedImage

    更好的方法是重用原始文件的firebaseStorageDownloadTokens

    这就是我的做法

    functions
        .storage
        .object()
        .onFinalize((object) => {
    
          // some image optimization code here
          // get the original file access token
          const downloadtoken = object.metadata?.firebaseStorageDownloadTokens;
          return bucket.upload(tempLocalFile, {
            destination: file,
              metadata: {
                metadata: {
                  optimized: true, // other custom flags
                  firebaseStorageDownloadTokens: downloadtoken, // access token
                }
                
          });
        });
    

    【讨论】:

      【解决方案2】:

      我刚刚遇到了同样的问题,原因不明,Firebase 的 Resize Images 故意从调整后的图像中删除了下载令牌

      禁用删除下载访问令牌

      • 转到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 并等待它完成

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

      【讨论】:

      • 非常感谢!真的很想知道他们为什么会为此删除令牌...
      • 这是我喜欢看到的答案类型!这对我也有用,谢谢!
      • 该行已在扩展的 0.1.7 版本中删除。如果您只是更新到该版本(或更高版本),您的问题将得到解决:)
      • @RosárioPereiraFernandes 很高兴知道他们终于修好了。
      【解决方案3】:

      我自己刚开始使用扩展程序。我注意到在单击“创建访问令牌”之前,我无法从 Firebase 控制台访问图像预览 我想您必须在图像可用之前以编程方式创建此令牌。

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2019-01-03
        • 2013-02-24
        • 1970-01-01
        • 2011-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多