Writing to Files in Firebase Storage? - firebase

I have a html file inside my Firebase storage(say hello.html).I have an html content
<div>Hello world</div>
I need to write this content to the above hello.html in the storage.How to do this using Firebase Cloud Functions??

For you to be able to perform that, it will depend on the way that your file is uploaded on the Firebase and how you would like to edit them.
Considering that, I would recommend you to take a look at the below links, to verify the options that are available for you to edit.
Using Firebase Cloud Functions to Update Hosted File
Tutorial: How to upload files using Firebase
Read value/content of a file from Firebase Storage
Let me know if the information helped you!

Related

Can't access uploaded files through Firebase storage on web

I'm trying to access/download files that have been uploaded to Firebase storage but I can't figure out how to. Some of the files have a create new access token button under the storage location which gives me a link to the file on the web. Unfortunately, this is only a few files and seems to only be ones uploaded from localhost?
I can't find any reference to this on the documentation, this should be achievable through the Firebase dashboard?
I've tried setting the access rules to allow reads in all cases which hasn't helped.
Thanks
In general, you're supposed to use getDownloadURL(), as shown in the documentation, from within your web or mobile app to generate a download URL for use within your app. Typically, the console is only used to revoke the tokens that enable to download of each file through that URL.
If that's not specifically what you're trying to do, perhaps you want to read up on other was to make data public in Cloud Storage. The Firebase console is not really the best mechanism to manage downloadable content.

How to upload website on Firebase hosting without attaching storage.rules & firestore.rules files?

Today I have uploaded my website on Firebase hosting and in the process of doing that I have to create two rules files, one is storage.rules and another one is firestore.rules. I also have to create a firestore.indexes.json file. But I can remember previously I didn't have to create any such file while uploading another website on Firebas host.
I need to get rid of these 3 files, because every time I upload my files It changed to private rules for both storage and cloud which is unnecessary for me. I need to remain them public. Besides that I can't open these 2 rules files to edit (using Mac) just at least to see what is inside of them. How can I do it, Thanks!
It sounds like you use the Firebase CLI to initialize several products in the same project, including Cloud Storage and Firestore. If you don't want to work with these other products in your project files, you shouldn't select them during initialization.
The easiest thing to do would be to start over in a new directory and initialize only the products you want to use. It sounds like that's only Firebase Hosting.
You could also edit firebase.json and remove the products you don't want to use any more.
If you do want to work with Storage and Firestore, but you only want to deploy to Hosting, then just use firebas deploy --only hosting.

Firebase Functions - generate and host static webpage

I'm using Firebase Cloud Functions to generate an HTML file and now I'd like to host it together with related assets (js, css, fonts etc.) but without success.
I call the function, it generates the file properly and puts it in Firebase Storage together with js/css/other assets. Now I would like to return a URL of the index.html file so that the user can access it in the browser and the .html page will have access to the assets. Unfortunately the generated URL enforces download but I'm pretty sure that even if I managed it somehow, it won't be able to access asset files.
I know it's possible on AWS (S3 bucket) but can I do it on Firebase? Firebase Hosting doesn't seem to be the right solution in that case, does it?
Don't save it to Storage, that's a bad use case for this scenario. Instead, save it to Hosting:
https://firebase.google.com/docs/hosting/
Also, you can consider serving the content directly from the cloud function, probably there's no need to create a static version first.

Firebase Storage: safe way to get relative path of a file? (Storing HTML file)

Hello StackOverflow and Firebase Community!
I am trying to achieve something that is probably not intended with Firebase: I want to store HTML folders (banner ads in my case) in Firebase Storage.
My question is: How to keep the relative path from my HTML file to the related JS and image files.
So this files could easily find each-other:
/path/to/my/folder/index.html
/path/to/my/folder/main.js
/path/to/my/folder/picture.jpg
<html>
...
<script src="main.js">
...
</html>
document.onload = function() {
myImage.src = 'picture.jpg'
}
These relative URLs are not working if I store all the files on Firebase Storage, and access them with their DownloadURL, with looks like https://firebasestorage.googleapis.com/v0/b/my-project.appspot.com/o/my_folder_300x250%2Findex.html?alt=media&token=12a123a1-1234-1a1a-1234-a1ab123456a1,
I can make the files reachable without token by opening Firebase's security rules, but the relative path is still broken because of the URL parameters, and the folder path being escaped.
I also managed to make it work on an other project by making the Google Cloud Storage bucket public. I can access the files and keep the relative path using an URL like: https://my-project.appspot.com.storage.googleapis.com/path/to/my/folder/my_folder_300x250/index.html. This works great, but has major security flaws that I want to prevent, like listing all the files in the bucket 😱
I tried to save my banners as zip files, unzip them on the front end side, and recreate the folder structure in browser with the FileSystem API. But I'm limited to Chrome.
I could also try to replace all the URLs in my HTML, JS and CSS files, but this doesn't feel like a good solution.
I found a pretty simple solution:
In Google Cloud Console, I created a new role that has get access, but no list access, and applied it to allUsers, with the following permissions.
resourcemanager.projects.get
storage.objects.get
This will solve my issues for now, and I'll keep accessing data using links like https://my-project.appspot.com.storage.googleapis.com/path/to/my/folder/my_folder_300x250/index.html instead of firebase's downloadURL().
Firebase Storage is built around Cloud Storage, which doesn't actually have directories, but a flat namespace. Directories are emulated via the storage object naming convention: names can contain the / character and placing a "directory" path at the beginning of the object name makes that object appear to be in that "directory". See
How Subdirectories Work.
So your only solution (if you want to store the files independently) is to process all references, make the "paths" absolute and name the files accordingly. As you observed, you probably need to take into account all the URL query parameters as well.

Using Firebase Cloud Functions to Update Hosted File

I am building a site that is using Angular 4, Firebase, and Firebase Cloud Functions. What I am looking to do is when certain data is added to the database I want to add, remove, or update something in an RSS feed, sitemap, and JSON feed which are files hosted with Firebase Hosting.
Any suggestions on how to accomplish this or an alternative if it is not possible without manual updates and redeploys of the project?
There is currently no way to publish a new (or update a) file to Firebase Hosting based on a database (or other dynamic) trigger.
What is possible is to map a Cloud Function to a path on your site (e.g. /sitemap or /feed), generate the HTML in that Cloud Function, and then set caching headers on the result.

Resources