Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase storage upload string with Cloud function

I would like to upload a string of text and have that string uploaded to Cloud storage. I've build it in plain JS, but having issues hacking it into a cloud function.

function download(exportObj){
   var databuk =  gcs.bucket('******.appspot.com');


   // var bucket = admin.storage().bucket();
    //var tocfileloc = storageRef.child('toctest.json');
   // const name = "toctest.json";
   // const bucketdes = bucket.name;
    var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj));
    
    databuk.putString(dataStr, 'data_url').then(snapshot => {
        console.log('Uploaded a data_url string!');
        return true;
      }).catch(err=>{
          console.log("error",err);
      })
    }

I have some code above! The string is "exportObj"

like image 927
Connor Marting Avatar asked Sep 03 '25 03:09

Connor Marting


1 Answers

You'll want to use the Admin SDK for this. It'll be something along the lines of:

const admin = require('firebase-admin');
admin.initializeApp();

// ... then later, in your function
const file = admin.storage().bucket().file('path/to/your/file.txt');
return file.save('This will get stored in my storage bucket.', {
  gzip: true,
  contentType: 'text/plain'
}).then(() => {
  console.log('all done!');
});

The specific "save" method is documented here.

like image 184
Michael Bleigh Avatar answered Sep 07 '25 09:09

Michael Bleigh