将图像Blob上传到Firebase-缺少后续步骤

时间:2019-12-19 15:48:00

标签: reactjs firebase react-native blob react-native-firebase

我在数据库中保存了一系列的本地图像。看起来像这样:

[{"_data":{"blobId":"C4B1459C-4444-436D-836A-9F9CA63B925F","name":"C6DC696D-5555-4204-B8EC-F8F071D1FC15.jpg","offset":0,"size":586074,"type":"image/jpeg"}},{"_data":{"blobId":"5EFDAD62-9ACD-2222-AEBE-DF9DDD938A7B","name":"46FE26E3-6666-4DED-9657-C471231A800D.jpg","offset":0,"size":247568,"type":"image/jpeg"}},{"_data":{"blobId":"8423EF1B-F960-1111-A503-487A448FC895","name":"E2E2456C-9999-4A3C-B3FD-56D38FE0DDAE.jpg","offset":0,"size":407174,"type":"image/jpeg"}}]

我正在将阵列保存到Firestore,并希望在用户登录并显示照片时拉下blob。我知道有时需要使用downloadableURL(),但在FB文档中没有看到任何地方指出必须将blob作为URL上传。我只知道我必须将Blob放入FB中,这已经完成。在我上传到FB之前或当用户登录/加载初始状态时,谁能指出我下一步的正确方向以获取URL?

https://firebase.google.com/docs/storage/web/upload-files

Firebase Images Table

准备数据推送到Firebase:

const imagesBlob = [];
for(ud in userData.images){
  var response = await uriToBlob(userData.images[ud]); //method turns the file:// into a blob
  imagesBlob.push(response);
}
const newBlobObject = imagesBlob.map((obj)=> {return Object.assign({}, obj)}); //I push this value to the DB

将数据下拉至状态:blob包含在doc.data()

grabUserData = async (userId) => {
var db = firebase.firestore();
var docRef = db.collection("Users").doc(userId);
return docRef.get().then(function(doc) {
  if (doc.exists) {
      console.log("Document data:", doc.data());
      return doc.data();;
  } else {
      console.log("No such document!");
  }
})
};

我正在使用Firebase,React Native和Expo

1 个答案:

答案 0 :(得分:1)

尝试使用Firebase Storage将Blob存储在正确的位置,同时获得一个downloadableUrl

const uploadFile = async = (blob) => {
    const ref = await firebase
      .storage()
      .ref('chatRoomFiles/123')
      .put(blob);

    const url = ref.getDownloadURL();

return url; // <-- Url that returns your uploaded image

}