首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只有HTTP(S)协议支持Firebase函数

只有HTTP(S)协议支持Firebase函数
EN

Stack Overflow用户
提问于 2021-10-16 23:30:20
回答 1查看 377关注 0票数 1

我已经复制了所有代码,包括来自Resize Image函数扩展的依赖项。我正在Firebase模拟器中运行这个程序,并且需要对它进行调整以满足我的需要。但是,就目前而言,我已经将其设置为几乎与扩展中的源代码完全相同的代码。

Ext网址:https://firebase.google.com/products/extensions/storage-resize-images

我的本地设置:

节点: v14.17.5

国家预防机制: 7.23.0

火力基数: 9.20.0

防火墙-管理:^9.11.1

火基-功能“:^3.15.6

mkdirp:^1.0.4

uuid:^8.3.2

在扩展上的应用

firebase-管理:^8.0.0

消防基地-功能:^3.13.2

mkdirp:^1.0.4

夏普: 0.23.4

uuidv4:^6.1.0

,这是调整图像大小的代码

请注意这一行:await remoteFile.download({ destination: originalFile }); // <- Offending line of code...,因为这是它失败的行。

代码语言:javascript
复制
const resizeImage = async (object): Promise<ResizedImageResult[]> => {
  logs.start();
  const { contentType } = object; // This is the image MIME type

  const tmpFilePath = path.resolve('/', path.dirname(object.name)); // Absolute path to dirname

  if (!contentType) {
    logs.noContentType();
    return;
  }

  if (!contentType.startsWith('image/')) {
    logs.contentTypeInvalid(contentType);
    return;
  }

  if (object.contentEncoding === 'gzip') {
    logs.gzipContentEncoding();
    return;
  }

  if (!supportedContentTypes.includes(contentType)) {
    logs.unsupportedType(supportedContentTypes, contentType);
    return;
  }

  if (config.includePathList && !startsWithArray(config.includePathList, tmpFilePath)) {
    logs.imageOutsideOfPaths(config.includePathList, tmpFilePath);
    return;
  }

  if (config.excludePathList && startsWithArray(config.excludePathList, tmpFilePath)) {
    logs.imageInsideOfExcludedPaths(config.excludePathList, tmpFilePath);
    return;
  }

  if (object.metadata && object.metadata.resizedImage === 'true') {
    logs.imageAlreadyResized();
    return;
  }

  const bucket = admin.storage().bucket(object.bucket);
  const filePath = object.name; // File path in the bucket.
  const fileDir = path.dirname(filePath);
  const fileExtension = path.extname(filePath);
  const fileNameWithoutExtension = extractFileNameWithoutExtension(filePath, fileExtension);
  const objectMetadata = object;

  let originalFile;
  let remoteFile: File;
  try {
    originalFile = path.join(os.tmpdir(), filePath);
    const tempLocalDir = path.dirname(originalFile);

    // Create the temp directory where the storage file will be downloaded.
    logs.tempDirectoryCreating(tempLocalDir);
    await mkdirp(tempLocalDir);
    logs.tempDirectoryCreated(tempLocalDir);

    // Download file from bucket.
    remoteFile = bucket.file(filePath);
    logs.imageDownloading(filePath);
    logs.imageDownloading(originalFile);
    await remoteFile.download({ destination: originalFile }); // <- Offending line of code...
    logs.imageDownloaded(filePath, originalFile);

    // Get a unique list of image types
    const imageTypes = new Set(config.imageTypes);

    // Convert to a set to remove any duplicate sizes
    const imageSizes = new Set(config.imageSizes);

    const tasks: Promise<ResizedImageResult>[] = [];

    imageTypes.forEach((format) => {
      imageSizes.forEach((size) => {
        tasks.push(
          modifyImage({
            bucket,
            originalFile,
            fileDir,
            fileNameWithoutExtension,
            fileExtension,
            contentType,
            size,
            objectMetadata: objectMetadata,
            format,
          }),
        );
      });
    });

    const results = await Promise.all(tasks);

    const failed = results.some((result) => result.success === false);
    if (failed) {
      logs.failed();
      return;
    } else {
      if (config.deleteOriginalFile === deleteImage.onSuccess) {
        if (remoteFile) {
          try {
            logs.remoteFileDeleting(filePath);
            await remoteFile.delete();
            logs.remoteFileDeleted(filePath);
          } catch (err) {
            logs.info('Catch 1');
            logs.errorDeleting(err);
          }
        }
      }
      logs.complete();
    }
  } catch (err) {
    logs.info('Catch 2');
    logs.error(err);
  } finally {
    if (originalFile) {
      logs.tempOriginalFileDeleting(filePath);
      fs.unlinkSync(originalFile);
      logs.tempOriginalFileDeleted(filePath);
    }
    if (config.deleteOriginalFile === deleteImage.always) {
      // Delete the original file
      if (remoteFile) {
        try {
          logs.remoteFileDeleting(filePath);
          await remoteFile.delete();
          logs.remoteFileDeleted(filePath);
        } catch (err) {
          logs.errorDeleting(err);
        }
      }
    }
  }
};

期望行为

在执行await remoteFile.download({ destination: originalFile });时,应该下载原始文件。此代码与扩展代码完全相同。

实际行为

下列错误已注销:

代码语言:javascript
复制
{
  "severity":"ERROR","message":"Error when resizing image TypeError: Only HTTP(S) protocols are supported
    at getNodeRequestOptions (.../node_modules/teeny-request/node_modules/node-fetch/lib/index.js:1309:9)
    at .../node_modules/teeny-request/node_modules/node-fetch/lib/index.js:1410:19
    at new Promise (<anonymous>)
    at Function.fetch [as default] (.../node_modules/teeny-request/node_modules/node-fetch/lib/index.js:1407:9)
    at teenyRequest (.../node_modules/teeny-request/build/src/index.js:184:29)
    at Object.request (.../node_modules/teeny-request/build/src/index.js:241:20)
    at Timeout.makeRequest [as _onTimeout] (.../node_modules/retry-request/index.js:139:28)
    at listOnTimeout (internal/timers.js:557:17)
    at processTimers (internal/timers.js:500:7)"
}

有人注意到这个问题了吗?我不能回到一个早期版本的Firebase,因为有打破的变化。

EN

回答 1

Stack Overflow用户

发布于 2021-10-21 11:29:53

对于其他遇到此问题的人,我通过在下面的行中将验证设置为false来修正它:await remoteFile.download({ destination: originalFile, validation: false });

当不使用模拟器时,我将添加逻辑来打开验证。另外,确保扩展的所有环境变量都有默认值(请参阅扩展的源代码中的config.ts文件,并查看GCP中函数详细信息页中的变量选项卡)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69600287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档