简体   繁体   English

将 firebase 存储中的图像连接到 firebase 中的数据文档

[英]Connecting an image in firebase storage, to a data document in firebase

I am trying to access a single, specific image from firebase storage based on a data document.我正在尝试根据数据文档从 firebase 存储中访问单个特定图像。 I currently have a data document with 3 fields: name, price, and imageLink.我目前有一个包含 3 个字段的数据文档:名称、价格和 imageLink。 I want the image link to connect somehow to an image that I have saved in my storage so that I can display the price and name with the correct corresponding image.我希望图像链接以某种方式连接到我保存在存储中的图像,以便我可以使用正确的对应图像显示价格和名称。 Not sure what I need to put in the imageLink and how to get a single image.不确定我需要在 imageLink 中放入什么以及如何获取单个图像。

This is how I am uploading the image to firebase这就是我将图像上传到 firebase 的方式

`function uploadImage () {
if(imageUpload == null) return
const imageRef = ref(storage, `images/${imageUpload.name + v4()}`)
setImageUrl("image ref", imageRef._location.path_)
uploadBytes(imageRef, imageUpload).then(() => {
  alert("Image Uploaded")
})

}` }`

and this is what I am doing to try access the image这就是我正在做的尝试访问图像

const createWatchListing = async () => {
// uploadImage()
await addDoc(watchCollectionRef, {name: watchName, price: watchPrice, watchImageUrl: imageUrl})

} }

 <div>
          <h3>{watch.name}</h3>
          <h3>${watch.price}</h3>
          <img src={watch.watchImageUrl} /> <------
          <form onSubmit={() => {updateWatch(watch.id, updatedWatchName, updatedWatchPrice )}}>
            <input placeholder={watch.name} onChange={e => {setUpdatedWatchName(e.target.value)}}/>
            <input type="number" placeholder={watch.price} onChange={e => {setUpdatedWatchPrice(e.target.value)}}/>
            <button>Update Watch</button>
            <button onClick={() => {deleteWatchListing(watch.id)}}>Delete Listing</button>
          </form>  
        </div>

I tried putting the file url in, but it is different to the image url I get back from firebase when I use getDownloadUrl.我尝试将文件 url 放入,但它与我使用 getDownloadUrl 从 firebase 返回的图像 url 不同。 I am able to get all the images from firebase currently, just not indivual ones.我目前能够从 firebase 获取所有图像,但不是个别图像。

yes, I have also faced that issue.是的,我也遇到过这个问题。 But that has been resolved by adding these permissions.但这已通过添加这些权限得到解决。 在此处输入图像描述 OR You can follow the following steps:-或者您可以按照以下步骤操作:-

  1. https://console.cloud.google.com/storage/browser?project=YOUR_PROJECT_NAME&supportedpurview=project&cloudshell=true 2.select our project. https://console.cloud.google.com/storage/browser?project=YOUR_PROJECT_NAME&supportedpurview=project&cloudshell=true 2.select 我们的项目。
  2. click on open shell (it may take 1min to open)点击打开shell(打开可能需要1分钟)
  3. command: gsutil cors set cors.json gs://YOURPROJECTNAME.appspot.com/*命令:gsutil cors set cors.json gs://YOURPROJECTNAME.appspot.com/* 在此处输入图像描述

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

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