javascript - Expo firebase 7.9.0 无法获取 downloadUrl
问题描述
我的方法确实设法将图像从 expo 加载到 firebase 存储,但我似乎无法获得下载 URL。
const uploadImage = async (uri) => {
const uniqid = () => Math.random().toString(36).substr(2, 9);
const ext = uri.split('.').pop(); // Extract image extension
const filename = `${uniqid()}.${ext}`; // Generate unique name
const response = await fetch(uri);
const blob = await response.blob();
var ref = firebase
.storage()
.ref()
.child('images/' + filename);
ref.getDownloadURL().then((url) => console.log(url));
return ref.put(blob);
};
这是我得到的错误
FirebaseStorageError {
“code_”:“storage/object-not-found”,“message_”:“Firebase 存储:对象 'images/gebwu7tnh.jpg' 不存在。”,“name_”:“FirebaseError”,“serverResponse_”:“{ “错误”:{“代码”:404,“消息”:“未找到。无法获取对象”,“状态”:“GET_OBJECT”} }”
解决方案
这是我发现帮助我研究的东西。我确实重构了我的 firebase 以使用更高阶的组件。这是我的firebase方法。
uploadImageAsync: async (uri) => {
const uniqid = () => Math.random().toString(36).substr(2, 9);
const ext = uri.split('.').pop(); // Extract image extension
const filename = `${uniqid()}.${ext}`; // Generate unique name
const ref = firebase
.storage()
.ref()
.child('images/' + filename);
const blob = await new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onload = function () {
resolve(xhr.response);
};
xhr.onerror = function (e) {
console.log(e);
reject(new TypeError('Network request failed'));
};
xhr.responseType = 'blob';
xhr.open('GET', uri, true);
xhr.send(null);
});
const snapshot = await ref.put(blob);
blob.close();
const imgUrl = await snapshot.ref.getDownloadURL();
console.log(imgUrl);
return imgUrl;
}, };
这是我在组件中实现它的方式
const setImage = async (uri) => {
try {
return await firebase.uploadImageAsync(uri);
} catch (error) {
console.log(error);
}
};