首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:"Firebase存储:用户没有访问权限.“

错误:"Firebase存储:用户没有访问权限.“
EN

Stack Overflow用户
提问于 2022-11-09 21:39:46
回答 1查看 71关注 0票数 0

我试图编写一个Firebase函数,从API中获取一个音频文件,并将其保存到Firebase中。我得到了一个错误:

代码语言:javascript
运行
复制
Firebase Storage: User does not have permission to access 'Audio/English/United_States-OED-0/winter.mp3'. (storage/unauthorized)

我在使用模拟器和云时也会遇到同样的错误。存储中没有写入任何文件。日志Uploaded a string!'不会出现。

我有一个Firebase IAM服务帐户。我知道如何将它导入CommonJS模块(require),但我的函数位于ES模块(import)中。如何导入我的服务帐户?这不起作用,因为assert只在Node 17中工作,而Firebase函数使用Node 16。

代码语言:javascript
运行
复制
import serviceAccount from "./my-awesome-project-firebase-adminsdk-12345.json" assert { type: "json" };

这是我的云功能:

代码语言:javascript
运行
复制
import { initializeApp } from "firebase/app";
import * as functions from "firebase-functions";
import { getStorage, ref, uploadString, connectStorageEmulator } from "firebase/storage";

const firebaseConfig = {
    apiKey: ...,
    authDomain: ...,
    databaseURL: ...,
    projectId: ...,
    storageBucket: "my-awesome-project.appspot.com",
    appId: "..."
};

const app = initializeApp(firebaseConfig);

import got from 'got';

export const Oxford_T2S = functions.firestore.document('Users/{userID}/English/OED_T2S_Request').onUpdate((change, context) => {
  const storage = getStorage(app);
  connectStorageEmulator(storage, "localhost", 9199); // comment out to use cloud
  const audioEnglishOEDWinterRef = ref(storage, 'Audio/English/United_States-OED-0/winter.mp3');

  async function getOED() {
    try {
      let file = await got('https://audio.oxforddictionaries.com/en/mp3/winter__us_2.mp3');
      uploadString(audioEnglishOEDWinterRef, file).then((snapshot) => {
          console.log('Uploaded a string!');
      });
    } catch (error) {
        console.error(error);
    }
  }

  return getOED()
});

以下是我的云规则:

代码语言:javascript
运行
复制
service firebase.storage {
  match /b/{bucket}/o {   
    // anything using request.auth.token relies on the userLogin cloud function.
    
    match /Audio/{shortLangA}/{file=**} {
        // allow writing of audio files if the user is trusted or a teacher of the language, apply recursively
        allow write: if (request.auth.token.trusted || shortLangA in request.auth.token.teaches);
    }
    
     match /Users/{file=**} {
        // allow writing of pronunciation tests
        // this rule is insecure
      // this rule needs to be improved by allowing users to only write to their own folders
      allow read, write: if true;
    }

    match /{allPaths=**} {
      // Only authenticated users can read from the bucket
      allow read: if request.auth != null;
    }
  }
}

我试着旋转我的棱角分明的项目和登录,但这没有帮助。

这是我的模拟器规则。这些是firebase init functions创建的默认规则

代码语言:javascript
运行
复制
rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if false;
    }
  }
}

我把false改成了true,但没什么用。

关于got的文档是这里。那句话没有抛出错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-12 01:09:27

云存储错误消息文档说

代码语言:javascript
运行
复制
storage/unauthorized
User is not authorized to perform the desired action, check your security rules to ensure they are correct.

看看模拟器的默认storage.rules,没有什么东西能读或写。谢谢你保护我的仿真器免受俄罗斯黑客的攻击。默认应为

代码语言:javascript
运行
复制
rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write
    }
  }
}

即使有了新的规则,仿真器似乎也很挑剔。有时文件写入,大多数情况下什么都不写到存储。

我的云存储规则,特别是request.auth.token.trusted,是为我的角度应用程序的调用设置的。如果调用不是来自应用程序上登录的用户,则请求将停止。也就是说,测试Firebase控制台中的云功能不会写入存储。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74381691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档