How can I upload a file from AngularJS application to Firebase Hosting

↘锁芯ラ 提交于 2019-12-13 03:57:49

问题


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.

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.


回答1:


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.

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

  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.

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



来源:https://stackoverflow.com/questions/45746594/how-can-i-upload-a-file-from-angularjs-application-to-firebase-hosting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!