如何使用node.js在Firebase中存储文件

时间:2018-12-29 12:21:09

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

我有一个小作业,其中将有一个指向文档或文件的URL,例如Google驱动器链接或保管箱链接。

我必须使用此链接通过Node.js将文件或文档存储在Firebase中。我应该如何开始?

抬起头可能会有所帮助。我应该使用什么?请帮助我被困在这里。

1 个答案:

答案 0 :(得分:2)

GCP documentation中主要介绍了使用管理SDK的文档。

下面是一段代码,显示了如果有URL,如何将图像直接上传到Cloud Storage。任何公共链接都可以使用,无论它是从Dropbox共享还是在互联网上的其他地方共享。

const fileUrl = 'https://www.dropbox.com/some/file/download/link.jpg';
const opts = {
  destination: 'path/to/file.jpg',
  metadata: {
    contentType: 'image/jpeg'
  }
};

firebase.storage().bucket().upload(fileUrl, opts);

此示例使用的是应用程序中的默认存储桶,并且opts对象为API调用提供了文件上传选项。

  • destination是文件将被上传到Google Cloud Storage的路径
  • metadata应该描述您要上传的文件(see more examples here
    • contentType是您要上传的文件MIME type