带有云功能的 Firebase 存储上传字符串

Con*_*ing 2 firebase google-cloud-functions firebase-storage

我想上传一串文本并将该字符串上传到云存储。我已经用普通的 JS 构建了它,但是在将它入侵到云函数时遇到了问题。

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);
      })
    }
Run Code Online (Sandbox Code Playgroud)

我上面有一些代码!字符串是“exportObj”

Mic*_*igh 8

为此,您需要使用Admin SDK。这将是类似的东西:

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!');
});
Run Code Online (Sandbox Code Playgroud)

此处记录了特定的“保存”方法。