Home > Back-end >  How do you upload a new file with a JSON on Firebase Storage using Firebase Functions?
How do you upload a new file with a JSON on Firebase Storage using Firebase Functions?

Time:04-03

How do you upload a new file with a JSON on Firebase Storage using Firebase Functions? My code so far:

exports.scheduledFunctionCrontab = functions.pubsub.schedule("0 0 * * *")
    .onRun(async () => {
        try {
            const response = await axios.get("api.com");
   
            const bucket = await admin.storage().bucket();

            // What now?

        } catch (e) {
            functions.logger.error(e);
        }

        return null;
    })

CodePudding user response:

You can use save() to write the API response in a JSON file as shown below:

exports.scheduledFunctionCrontab = functions.pubsub.schedule("0 0 * * *")
  .onRun(async () => {
    try {
      const response = await axios.get("api.com");

      const bucket = await admin.storage().bucket();

      const file = bucket.file("file.json");
      const contents = JSON.stringify(data);

      await file.save(contents);
    } catch (e) {
      functions.logger.error(e);
    }

    return null;
  })
  • Related