2
votes

I am using the Firebase web client and would like to upload JSON to Cloud Storage. I can create a reference using firebase.storage().ref().child("/testing.json");, however I am not sure how to upload the JSON string. The docs only mention uploading files, blobs, and base64 encoded strings. For example, how do I finish the below to upload to the JSON string to cloud storage, to be stored as a JSON file with "application/json" content type?

const ref = firebase.storage().ref().child("/testing.json");
const jsonString = JSON.stringify({ hello: "world" });
ref.put() // ???
1

1 Answers

2
votes

Storage ref put can take a Blob parameter. Convert the string to a blob...

const ref = firebase.storage().ref().child("/testing.json");
const jsonString = JSON.stringify({ hello: "world" });

const blob = new Blob([jsonString], { type: 'application/json' });
ref.put(blob).then( ... )