为什么我会收到一条错误消息,指出 Firebase 存储中的文件不存在,但当您点击链接时,该文件实际上存在?



我正在保存一个文件到firebase存储和文件退出,但我得到错误,它没有。我混淆了URL和文件,所以这里没有人可以看到它。但它确实存在。

是正确的URL。

上传代码:

async function uploadFile() {
console.log('starting UPLOAD ========');
const blob = await fetch(mediaBlobUrl).then((r) => r.blob());
var id = crypto.randomBytes(16).toString('hex');
var url = window.location.href;
console.log(` the unique id for the url is ----> ${id}`);
const path = `/recordings/${id}`;
setCopied(true);
firebase
.storage()
.ref(path)
.put(blob)
.then(function (snapshot) {
console.log('Uploaded complete');
});
await storage.ref(path).getDownloadURL().then(setURL);
}

错误:

GET https://firebasestorage.googleapis.com/v0/b/burcmce.appspot.com/o/recordings%2Fbbf9217d3a36c6 404
XhrConnection.send @ connection.ts:75
doTheRequest @ request.ts:135
(anonymous) @ backoff.ts:63
setTimeout (async)
callWithDelay @ backoff.ts:61
start @ backoff.ts:115
NetworkRequest.start_ @ request.ts:205
(anonymous) @ request.ts:102
NetworkRequest @ request.ts:99
makeRequest @ request.ts:309
StorageService._makeRequest @ service.ts:296
(anonymous) @ service.ts:324
step @ tslib.es6.js:102
(anonymous) @ tslib.es6.js:83
fulfilled @ tslib.es6.js:73
Promise.then (async)
step @ tslib.es6.js:75
(anonymous) @ tslib.es6.js:76
__awaiter @ tslib.es6.js:72
StorageService.makeRequestWithTokens @ service.ts:316
(anonymous) @ reference.ts:368
step @ tslib.es6.js:102
(anonymous) @ tslib.es6.js:83
(anonymous) @ tslib.es6.js:76
__awaiter @ tslib.es6.js:72
getDownloadURL$1 @ reference.ts:361
getDownloadURL @ api.ts:232
ReferenceCompat.getDownloadURL @ reference.ts:219
uploadFile @ ProductScreen.js:461
async function (async)
uploadFile @ ProductScreen.js:447
callCallback @ react-dom.development.js:3945
invokeGuardedCallbackDev @ react-dom.development.js:3994
invokeGuardedCallback @ react-dom.development.js:4056
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:4070
executeDispatch @ react-dom.development.js:8243
processDispatchQueueItemsInOrder @ react-dom.development.js:8275
processDispatchQueue @ react-dom.development.js:8288
dispatchEventsForPlugins @ react-dom.development.js:8299
(anonymous) @ react-dom.development.js:8508
batchedEventUpdates$1 @ react-dom.development.js:22396
batchedEventUpdates @ react-dom.development.js:3745
dispatchEventForPluginEventSystem @ react-dom.development.js:8507
attemptToDispatchEvent @ react-dom.development.js:6005
dispatchEvent @ react-dom.development.js:5924
unstable_runWithPriority @ scheduler.development.js:468
runWithPriority$1 @ react-dom.development.js:11276
discreteUpdates$1 @ react-dom.development.js:22413
discreteUpdates @ react-dom.development.js:3756
dispatchDiscreteEvent @ react-dom.development.js:5889
ProductScreen.js:462 Uncaught (in promise) FirebaseError: Firebase Storage: Object 'recordings/bbf9217d3a3aa999536c6' does not exist. (storage/object-not-found)
{
"error": {
"code": 404,
"message": "Not Found.  Could not get object",
"status": "GET_OBJECT"
}
}

有这个问题的人。解决方案如下:

firebase
.storage()
.ref()
.child(path)
.getDownloadURL()
.then(function (downloadURL) {
console.log('File available at', downloadURL);
setURL(downloadURL);
});
// storage.ref(path).getDownloadURL().then(setURL()); <<- not this.

您的代码试图在文件完全上传之前访问下载URL。因为你正在使用async/await,你可以简单地等待Firebase Storage SDK返回的承诺,然后再尝试访问它的下载。

一般情况下,您不会习惯地使用async/await。以下是更正后的版本。将对thencatch的调用与async/await混合使用不是一个好主意。在整个过程中使用async/await来获得更自然的表达式。

async function uploadFile() {
console.log('starting UPLOAD ========');
const blob = (await fetch(mediaBlobUrl)).blob()
var id = crypto.randomBytes(16).toString('hex');
var url = window.location.href;
console.log(` the unique id for the url is ----> ${id}`);
const path = `/recordings/${id}`;
setCopied(true);
const ref = firebase.storage().ref(path)
await ref.put(blob)
console.log('Uploaded complete');
const downloadUrl = await ref.getDownloadURL();
setUrl(downloadUrl);
}

解决方案如下:

firebase
.storage()
.ref()
.child(path)
.getDownloadURL()
.then(function (downloadURL) {
console.log('File available at', downloadURL);
setURL(downloadURL);
});
// storage.ref(path).getDownloadURL().then(setURL()); <<-- not this.

最新更新