如何将文件从AngularJS应用程序上传到Firebase Hosting [英] How can I upload a file from AngularJS application to Firebase Hosting

查看:93
本文介绍了如何将文件从AngularJS应用程序上传到Firebase Hosting的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase Hosting托管我的AngularJS应用程序.我正在使用ng-file-upload从应用程序中已有的表单中抓取用户上传的图像.我想将用户上传的图像存储在Firebase主机上的文件夹中.我无法使用Firebase Storage,因为我需要预先确定的直接链接到平台各部分中的图像资源.我以为也许可以使用Firebase Functions上传文件并将其存储在Firebase Hosting中.我一直遇到跨域错误.

I am using Firebase Hosting to host my AngularJS application. I am using ng-file-upload to grab the user-uploaded image from a form I already have in my application. I want to store the user-uploaded image in a folder on my Firebase Hosting. I cannot use Firebase Storage because I need a pre-determined, direct link to the image resource in various parts of my platform. I thought that maybe I could use Firebase Functions to upload the file and store it in my Firebase Hosting. I keep running into cross-origin errors.

有人可以解释如何构造Firebase函数,以便将图像上传到Firebase主机吗?据我了解,Firebase Function与其他Firebase Hosting内容不在同一位置托管,但是我不确定这是否有所不同.

Could someone explain how to structure the Firebase Function so I can upload the image to my Firebase Hosting? From what I understand, the Firebase Function is not hosted at the same location as the rest of the Firebase Hosting content, but I am not sure if this makes a difference.

推荐答案

Firebase托管专门用于版本化的已部署静态内容.用于Firebase的Cloud Storage绝对是适合您的产品.当您说您需要预定的直接链接"时,是否意味着您需要在上载完成之前知道该URL?如果没有,您可以只使用 getDownloadURL() 在Firebase Cloud Storage SDK中.

Firebase Hosting is exclusively for versioned, deployed static content. Cloud Storage for Firebase is definitely the right product for you. When you say you need a "pre-determined direct link" do you mean you need to be able to know the URL before the upload completes? If not, you can just use getDownloadURL() in the Firebase Cloud Storage SDK.

如果您确实需要提前知道URL,则可以执行以下操作:

If you do need to know the URL in advance, you could do the following:

  1. 使用Firebase SDK将文件上传到Cloud Storage.
  2. 创建一个可监听上传内容的云功能.
  3. li>
  4. 在该函数中,使用
  1. Upload the file to Cloud Storage using the Firebase SDK.
  2. Create a Cloud Function that listens for uploads.
  3. In the function, use the GCS Node API to make the object public.

完成上述操作后,即可直接通过https://<bucket>.storage.googleapis.com/<path_to_object>公开访问文件.

Once you do the above, your file will be directly and publicly accessible at https://<bucket>.storage.googleapis.com/<path_to_object>.

这篇关于如何将文件从AngularJS应用程序上传到Firebase Hosting的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆