Javascript 复制Firebase云函数中的存储文件

Javascript 复制Firebase云函数中的存储文件,javascript,firebase,google-cloud-functions,google-cloud-storage,firebase-storage,Javascript,Firebase,Google Cloud Functions,Google Cloud Storage,Firebase Storage,我正在启动一个云函数,以便复制Firestore中的一个寄存器。其中一个字段是图像,函数首先尝试复制图像,然后复制寄存器 代码如下: export async function copyContentFunction(data: any, context: any): Promise<String> { if (!context.auth || !context.auth.token.isAdmin) { throw new functions.https.HttpsEr

我正在启动一个云函数,以便复制Firestore中的一个寄存器。其中一个字段是图像,函数首先尝试复制图像,然后复制寄存器

代码如下:

export async function copyContentFunction(data: any, context: any): Promise<String> {
  if (!context.auth || !context.auth.token.isAdmin) {
    throw new functions.https.HttpsError('unauthenticated', 'Auth error.');
  }

  const id = data.id;
  const originalImage = data.originalImage;
  const copy = data.copy;

  if (id === null || originalImage === null || copy === null) {
    throw new functions.https.HttpsError('invalid-argument', 'Missing mandatory parameters.');
  }

  console.log(`id: ${id}, original image: ${originalImage}`);

  try {
    // Copy the image
    await admin.storage().bucket('content').file(originalImage).copy(
      admin.storage().bucket('content').file(id)
    );

    // Create new content
    const ref = admin.firestore().collection('content').doc(id);
    await ref.set(copy);

    return 'ok';
  } catch {
    throw new functions.https.HttpsError('internal', 'Internal error.');
  }
}
导出异步函数copyContentFunction(数据:任意,上下文:任意):承诺{ 如果(!context.auth | |!context.auth.token.isAdmin){ 抛出新函数.https.HttpsError('unauthenticated','Auth error'); } const id=data.id; const originalImage=data.originalImage; const copy=data.copy; if(id==null | | originalImage==null | | copy==null){ 抛出新函数.https.HttpsError('无效参数','缺少必需参数'); } log(`id:${id},原始图像:${originalImage}`); 试一试{ //复制图像 等待admin.storage().bucket('content').file(originalImage)。复制( admin.storage().bucket('content').file(id) ); //创建新内容 const ref=admin.firestore().collection('content').doc(id); 等待参考集(副本); 返回“ok”; }抓住{ 抛出新函数.https.HttpsError('internal','internalerror'); } } 我尝试了多种组合,但这段代码总是失败。由于某种原因,复制图像的过程失败了,我做错了什么

谢谢。

在云函数中使用
copy()
方法应该可以正常工作。您没有分享有关错误的任何详细信息(我建议使用
catch(error)
,而不仅仅是
catch
),但我可以看到您的代码存在两个潜在问题:

  • originalImage
    对应的文件不存在
  • 云存储实例中不存在
    内容
    存储桶
第二个问题通常来自一个常见的错误,即在云存储中混淆了存储桶文件夹(或目录)的概念

实际上,谷歌云存储没有真正的“文件夹”。在云存储控制台中,bucket中的文件以文件夹的层次结构树显示(就像本地硬盘上的文件系统一样),但这只是显示文件的一种方式:bucket中没有真正的文件夹/目录。云存储控制台仅使用文件路径的不同部分,通过使用“/”分隔符来“模拟”文件夹结构

这篇关于云存储和
gsutil
的文章很好地解释和说明了这种“分层文件树的幻觉”

因此,如果要将文件从默认bucket复制到内容“文件夹”,请执行以下操作:

await admin.storage().bucket().file(`content/${originalImage}`).copy(
  admin.storage().bucket().file(`content/${id}`)
);
在云函数中使用
copy()。您没有分享有关错误的任何详细信息(我建议使用
catch(error)
,而不仅仅是
catch
),但我可以看到您的代码存在两个潜在问题:

  • originalImage
    对应的文件不存在
  • 云存储实例中不存在
    内容
    存储桶
第二个问题通常来自一个常见的错误,即在云存储中混淆了存储桶文件夹(或目录)的概念

实际上,谷歌云存储没有真正的“文件夹”。在云存储控制台中,bucket中的文件以文件夹的层次结构树显示(就像本地硬盘上的文件系统一样),但这只是显示文件的一种方式:bucket中没有真正的文件夹/目录。云存储控制台仅使用文件路径的不同部分,通过使用“/”分隔符来“模拟”文件夹结构

这篇关于云存储和
gsutil
的文章很好地解释和说明了这种“分层文件树的幻觉”

因此,如果要将文件从默认bucket复制到内容“文件夹”,请执行以下操作:

await admin.storage().bucket().file(`content/${originalImage}`).copy(
  admin.storage().bucket().file(`content/${id}`)
);

wait ref.set(复制)中的
copy
是什么?另外,你能分享你的云函数的全部代码以及你得到的错误吗?@RenaudTarnec我已经用完整的函数代码更新了这个问题“复制”是要添加到firestore的文档。能否将错误记录在catch块中<代码>捕获(错误){…}
。还有,
content
bucket是否存在?既然你提到了它,我不明白bucket是如何工作的。我已经设置了正确的bucket名称,一切正常,谢谢。
wait ref.set(copy)中的
copy
是什么?另外,你能分享你的云函数的全部代码以及你得到的错误吗?@RenaudTarnec我已经用完整的函数代码更新了这个问题“复制”是要添加到firestore的文档。能否将错误记录在catch块中<代码>捕获(错误){…}
。还有,
content
bucket是否存在?既然你提到了它,我不明白bucket是如何工作的。我已经设置了正确的bucket名称,一切正常,谢谢。