【问题标题】:Firebase storage upload string with Cloud function具有 Cloud 功能的 Firebase 存储上传字符串
【发布时间】:2018-05-10 23:11:11
【问题描述】:

我想上传一个文本字符串并将该字符串上传到云存储。我已经用普通的 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);
      })
    }

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

【问题讨论】:

    标签: firebase google-cloud-functions firebase-storage


    【解决方案1】:

    您需要为此使用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!');
    });
    

    具体的“保存”方法是documented here

    【讨论】:

      猜你喜欢
      • 2018-12-06
      • 2020-10-04
      • 2019-06-21
      • 1970-01-01
      • 2018-04-03
      • 2018-06-30
      • 2019-07-28
      • 1970-01-01
      • 2021-04-21
      相关资源
      最近更新 更多