如何对Firebase存储映像URL进行软编码 [英] How to softcode firebase storage image url's

查看:64
本文介绍了如何对Firebase存储映像URL进行软编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase Storage创建图像URL并将其发送到Firebase数据库.当我同时对'currentUser'和Firebase Storage子引用进行硬编码时,我的工作效果很好.但是,如何从Firebase数据库中获取实际的currentUser并在子引用中添加一个易记的名称?我已经在Java中看到了一些可能的答案.但不是Javascript.这是我的代码:

I am using Firebase Storage to create and send image url's to Firebase Database. I have this working well when I have both a 'currentUser' and a Firebase Storage child reference hardcoded. But, how do I grab the actual currentUser from Firebase Database and put a fluid name to the child reference/references? I've seen some possible answers to this in Java. But not Javascript. Here's my code:

// openImage() is the function that grabs a photo from the camera roll
// I'm using react-native btw, so some of this code is handling the 
// blob. 

openImage() {
this.setState({ loading: true });
const Blob = RNFetchBlob.polyfill.Blob;
const fs = RNFetchBlob.fs;
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
window.Blob = Blob

// if I use this code, it won't return the urls: const {currentUser}  
// = firebase.auth()

// So, I've got the 'currentUser' hardcoded in as "12345"

const { currentUser } = "12345"

// ImagePicker.openPicker() is just a library to access the camera roll

ImagePicker.openPicker({
  width: 400,
  height: 300,
  cropping: true,
  mediaType: 'photo',
}).then(image => {
  const imagePath = image.path;

  let uploadBlob = null;

  const storage = firebase.storage();
  const storageRef = storage.ref(currentUser);

  //I also have the .child reference name hardcoded as 'dp.jpg'       

  const imageRef = storageRef.child('dp.jpg');
  const mime = 'image/jpg';
  fs.readFile(imagePath, 'base64')
    .then((data) => {

      return Blob.build(data, { type: `${mime};BASE64` });
  })
  .then((blob) => {
      uploadBlob = blob;
      return imageRef.put(blob, { contentType: mime });
    })
    .then(() => {
      uploadBlob.close();
      return imageRef.getDownloadURL();
    })
    .then((url) => {
      const { image } = this.props;

      this.props.entryUpdate({ prop: 'image', value: url })


  });
});

}

然后将图像URL传递到Firebase数据库,并将其设置为"image"的键和"dp.jpg"的值.使用硬编码,这可以很好地工作,但是当然仅适用于一个图像和一个用户(或Firebase Storage中的文件夹).我知道Firebase Realtime Database如何将自己的ID号分配给某项,如下所示:

The image url is then passed to Firebase Database and sets it as a key of 'image' and a value of 'dp.jpg'. With the hardcoding, this works fine, but for only one image and one user (or folder in Firebase Storage) of course. I'm aware of how Firebase Realtime Database will assign it's own id number to an item as in:

const { currentUser } = firebase.auth();
const item = firebase.database().ref(`/users/${currentUser.uid}/items`)
.push();
return () => {
item.set({ make, model, year, uid: item.key, image })

其中item.key由Firebase生成,因此不必进行硬编码.是否可以在Firebase Storage中针对图像子名称和"currentUser"实现此目的?而且由于移动应用程序仅从数据库中获取网址,我是否真的需要将每个图像分配给Firebase存储中的"currentUser"文件夹?

where item.key is generated by Firebase, thus it doesn't have to be hardcoded in. Is it possible to achieve this in Firebase Storage for both the image child name and the 'currentUser'? And do I really need to assign each image to a 'currentUser' folder in Firebase storage since the mobile app only is fetching the url from the database?

推荐答案

@parohy感谢您抽出宝贵的时间在Firebase文档中进行搜索.我认为.once()仅适用于Firebase实时数据库.而且我正在Firebase Storage中寻找唯一的名称.但是,您的回答帮助我提出了解决方案.我试图利用Firebase数据库已经创建完美唯一ID的事实.但是,我认为我无法访问Firebase Storage中的那些.所以我就这样做了:

@parohy Thanks for taking the time to search through the firebase docs. I think .once() only works with Firebase Realtime Database. And I'm looking for a unique name in Firebase Storage. However, your answer helped me come to my solution. I was trying to tap into the fact that Firebase Database already creates perfectly unique ids. However, I'm don't think I can access those in Firebase Storage. So I just did:

firebase.storage().ref().child(unique + '.jpg') 

const unique = Date.now() + Math.random(); 

不是很独特,但是足够接近.

not perfectly unique, but close enough.

这篇关于如何对Firebase存储映像URL进行软编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆