【发布时间】:2021-09-28 22:23:18
【问题描述】:
我创建了一个 firebase 项目和相关应用。我正在使用电子邮件/密码身份验证。我还使用 gsutil 来允许 cors,并验证我已经使用 gsutil cors get 成功设置了 cors。
我编写了以下代码来将资源下载到我的云存储。昨天它工作得很好。今天它不再工作了。这是我的代码:
const userConfig = {
email: "email@gmail.com",
password: "password",
file: "index.html",
elementId: "sample"
};
const firebaseConfig = {
apiKey: "key",
authDomain: "Domain",
projectId: "id",
storageBucket: "bucket.appspot.com",
messagingSenderId: "id",
appId: "id"
};
(async ({email, password, file, elementId}, firebaseConfig) => {
try {
const firebaseApp = firebase.initializeApp(firebaseConfig);
await firebase.auth().signInWithEmailAndPassword(email, password);
const storage = firebase.storage();
const storageRef = storage.ref(file);
const url = await storageRef.getDownloadURL();
const response = await fetch(url);
const data = await response.text()
const element = document.getElementById(elementId);
element.innerHTML += data;
} catch(error) {
alert(error.message);
}
})(userConfig, firebaseConfig);
我确信这段代码是正确的,因为它昨天在 Firefox 和 chrome 中运行没有问题。但是,由于某种原因,它停止了工作。问题似乎是:
const url = await storageRef.getDownloadURL();
似乎.getDownloadURL 函数正在向https://firebasestorage.googleapis.com/v0/b/bucket.appspot.com/o/index.html 发出ajax 请求,并且请求似乎失败了。 Firefox 出现以下错误。
跨域请求被阻止:同源策略不允许读取 远程资源在 https://firebasestorage.googleapis.com/v0/b/bucket.appspot.com/o/index.html . (原因:CORS 请求未成功)。[了解更多]
这非常不寻常,因为 firebasestorage.googleapis.com 昨天没有要求 cors。谷歌浏览器出现以下错误:
net::ERR_NAME_NOT_RESOLVED
我完全不明白。
由于我的安全规则允许在没有身份验证的情况下读取我的文件,我还使用 curl 发出了一个 http 请求。
curl -I https://firebasestorage.googleapis.com/v0/b/bucket.appspot.com/o/index.html
输出是:
curl: (6) 无法解析主机:firebasestorage.googleapis.com
发生了什么事?谁能帮我?我该怎么办?为什么.getDownloadURL 突然失效了?
【问题讨论】:
-
听起来更像是互联网/连接问题。
标签: firebase google-cloud-platform google-cloud-storage firebase-storage