简体   繁体   English

如何从 Firebase 存储中获取图像的下载 url?

[英]How to get download url for a Image from Firebase Storage?

I am trying to upload an image to firebase storage after that I am trying to get the download url of image but it's not working.我试图将图像上传到 firebase 存储,之后我试图下载图像 url 但它不起作用。 Below is the code:下面是代码:

const response = await fetch(selectedImage.uri);
const file = await response.blob();

const storageRef = ref(storage, `profile/${currentUser.email}`);
uploadBytes(storageRef, file).then( (snapshot) => {
  console.log('uploaded');
  getDownloadURL(storage).then( url => console.log(url)); // tried this outside of then block as well
});

the file is uploaded and accessible through console but getDownloadURL throws error:该文件已上传并可通过控制台访问,但 getDownloadURL 抛出错误:

[Unhandled promise rejection: TypeError: ref._throwIfRoot is not a function. (In 'ref._throwIfRoot('getDownloadURL')', 'ref._throwIfRoot' is undefined)] [未处理的 promise 拒绝:TypeError:ref._throwIfRoot 不是 function。(在'ref._throwIfRoot('getDownloadURL')'中,'ref._throwIfRoot'未定义)]

Instead of passing storage to getDownloadURL() , pass snapshot.ref .不是将storage传递给getDownloadURL() ,而是传递snapshot.ref

const response = await fetch(selectedImage.uri);
const file = await response.blob();

const storageRef = ref(storage, `profile/${currentUser.email}`);
uploadBytes(storageRef, file).then( (snapshot) => {
  console.log('uploaded');
  getDownloadURL(snapshot.ref).then( url => console.log(url));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM