如何使用node.js将文件存储在Firebase中 [英] How to store files in firebase using node.js
问题描述
我有一个小作业,其中将有一个指向文档或文件的URL,例如Google驱动器链接或保管箱链接.
I have a small assignment where I will have a URL to a document or a file like google drive link or dropbox link.
我必须使用此链接通过Node.js将文件或文档存储在Firebase中.我应该如何开始?
I have to use this link to store that file or doc in firebase using nodejs. How should i start?
抬起头可能会有所帮助.我应该使用什么?请帮助我被困在这里.
Little head's up might help. What should i use? Please help I'm stuck here.
推荐答案
以下代码段显示了如果有URL,如何将图像直接上传到Cloud Storage.任何公共链接都可以使用,无论它是从Dropbox共享还是在互联网上的其他地方共享.
Here's a snippet of code that shows how you could upload a image directly to Cloud Storage if you have a URL for it. Any public link works, whether it's shared from Dropbox or somewhere else on the internet.
编辑2020-06-01 :直接从URL上传的选项已在SDK的v2.0版本中删除(2018年9月4日):
Edit 2020-06-01 The option to upload directly from URL was dropped in v2.0 of the SDK (4 September 2018): https://github.com/googleapis/nodejs-storage/releases/tag/v2.0.0
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调用提供了文件上传选项.
This example is using the default bucket in your application and the opts
object provides file upload options for the API call.
-
destination
是您的文件将被上传到Google Cloud Storage中的路径 -
metadata
应该描述您要上传的文件( MIME类型您要上传的
destination
is the path that your file will be uploaded to in Google Cloud Storagemetadata
should describe the file that you're uploading (see more examples here)contentType
is the file MIME type that you are uploading
这篇关于如何使用node.js将文件存储在Firebase中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!