使用Firebase Admin SDK使用云功能上传到Firebase Storage时出现问题

时间:2020-04-27 01:24:05

标签: node.js typescript firebase firebase-storage firebase-admin

因此,基本上我想做的是下载.txt文件,向其中添加一些文本,然后将其重新上传回Firebase云存储。

文件的原始内容是苹果,梨和橙子。下载文件并向其中添加新行之后。应该说苹果,梨,橙子早早退出。

当我使用默认的安全规则时,会得到

错误:{ 编码:403 消息:“权限被拒绝。无法执行此操作” }

我用于上传此文件的代码如下(其以打字稿btw编写)

使用以下代码

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
const os = require('os');
const fs = require('fs');
const path = require('path');admin.initializeApp(functions.config().dbconfig);

const fileName = 'filecopy.txt'
const destination = 'text/filecopy.txt'
const tempFilePath = path.join(os.tmpdir(), fileName)
console.log( `Writing out to ${tempFilePath}`)

const bucketRef = admin.storage().bucket(functions.config().dbconfig.storage_bucket);


bucketRef.file(`text/filecopy.txt`).download(function (error, contents) {
    console.log(error)
    console.log(contents.toString())
    console.log(contents.toString().concat("\nexiting early"))
    fs.writeFileSync(tempFilePath, contents.toString().concat("exiting early") )

    bucketRef.upload(tempFilePath, {
         destination,
         metadata: {
            contentType: 'text/plain',

        }
        })
    .then( () => fs.unlinkSync(tempFilePath) )
    .catch(err => console.error('ERROR inside upload: ', err) );
})

奇怪的是,此代码有效,但前提是我更改了默认的安全规则

 service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

对于我不愿意的任何人都允许读写的安全规则...我整天都在努力,还没有真正找到解决方案,有人可以提供帮助吗?

0 个答案:

没有答案