我试图编写一个Firebase函数,从API中获取一个音频文件,并将其保存到Firebase中。我得到了一个错误:
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。
import serviceAccount from "./my-awesome-project-firebase-adminsdk-12345.json" assert { type: "json" };
这是我的云功能:
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()
});
以下是我的云规则:
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
创建的默认规则
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if false;
}
}
}
我把false
改成了true
,但没什么用。
关于got
的文档是这里。那句话没有抛出错误。
发布于 2022-11-12 01:09:27
云存储错误消息文档说
storage/unauthorized
User is not authorized to perform the desired action, check your security rules to ensure they are correct.
看看模拟器的默认storage.rules
,没有什么东西能读或写。谢谢你保护我的仿真器免受俄罗斯黑客的攻击。默认应为
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write
}
}
}
即使有了新的规则,仿真器似乎也很挑剔。有时文件写入,大多数情况下什么都不写到存储。
我的云存储规则,特别是request.auth.token.trusted
,是为我的角度应用程序的调用设置的。如果调用不是来自应用程序上登录的用户,则请求将停止。也就是说,测试Firebase控制台中的云功能不会写入存储。
https://stackoverflow.com/questions/74381691
复制相似问题