我正在尝试编写一个 Firebase 云函数来从 API 获取音频文件并将其保存到 Firebase 存储。我收到此错误:
Firebase Storage: User does not have permission to access 'Audio/English/United_States-OED-0/winter.mp3'. (storage/unauthorized)
Run Code Online (Sandbox Code Playgroud)
我使用模拟器和云时遇到同样的错误。存储中未写入任何文件。日志Uploaded a string!'没有出现。
我有一个 Firebase IAM 服务帐户。我知道如何将其导入 CommonJS 模块 ( require),但我的函数位于 ES 模块 ( import) 中。如何导入我的服务帐户?这不起作用,因为assert仅适用于 Node 17,而 Firebase Functions 使用 Node 16。
import serviceAccount from "./my-awesome-project-firebase-adminsdk-12345.json" assert { type: "json" };
Run Code Online (Sandbox Code Playgroud)
这是我的云功能:
Firebase Storage: User does not have permission to access 'Audio/English/United_States-OED-0/winter.mp3'. (storage/unauthorized)
Run Code Online (Sandbox Code Playgroud)
这是我的云规则:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试启动我的 Angular 项目并登录,但这没有帮助。
这是我的模拟器规则。这些是由以下人员创建的默认规则firebase init functions:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我更改false为true但这没有帮助。
有关的文档got在这里。该行不会抛出错误。
云存储错误消息文档说
storage/unauthorized
User is not authorized to perform the desired action, check your security rules to ensure they are correct.
Run Code Online (Sandbox Code Playgroud)
查看模拟器的默认值storage.rules,没有任何内容可以读取或写入。哎呀,感谢您保护我的模拟器免受俄罗斯黑客的侵害。默认应该是
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write
}
}
}
Run Code Online (Sandbox Code Playgroud)
即使有了新规则,模拟器似乎也很挑剔。有时文件会写入,但大多数时候不会写入存储。
我的云存储规则,特别是request.auth.token.trusted,是为来自我的 Angular 应用程序的调用而设置的。如果呼叫不是来自应用程序上的登录用户,则请求将停止。即,在 Firebase 控制台中测试 Cloud Function 不会写入存储。
| 归档时间: |
|
| 查看次数: |
1797 次 |
| 最近记录: |