【发布时间】:2020-08-16 13:30:47
【问题描述】:
我目前能够通过将图像存储在 blob 中并将其上传到 firestore 来将图像上传到 firebase firestore。我将如何做同样的事情,但对于视频,我也会将它放在一个 blob 中吗?下面是我用来上传图片的代码。
openLibrary = async () => {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL)
if (status === 'granted') {
const image = await ImagePicker.launchImageLibraryAsync({allowsEditing: true})
if(!image.cancelled){
const url = await this.props.uploadPhoto(image)
this.props.updatePhoto(url)
}
}
}
export const uploadPhoto = (image) => {
return async (dispatch) => {
try {
console.log("in upload photo")
const resize = await ImageManipulator.manipulateAsync(image.uri, [], { format: 'jpeg', compress: 0.1 })
const blob = await new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.onload = () => resolve(xhr.response)
xhr.responseType = 'blob'
xhr.open('GET', resize.uri, true)
xhr.send(null)
});
const uploadTask = await firebase.storage().ref().child(uuid.v4()).put(blob)
const downloadURL = await uploadTask.ref.getDownloadURL()
return downloadURL
} catch(e) {
console.log("in upload photo error")
console.error(e)
}
}
}
【问题讨论】:
-
从 Cloud Storage 上传和下载的所有内容都以相同的方式工作 - 文件类型不会受到特殊处理。它们只是字节序列。
-
另请注意:您不是将视频上传到 Cloud Firestore(文档数据库),而是将其上传到 Cloud Storage(博客存储解决方案),然后将视频的 URL 存储在数据库。
-
Doug 和我的评论之间,问题似乎归结为:google.com/…
-
@FrankvanPuffelen 我设法上传了它,但是当我下载它以文本格式下载的文件时,如何以视频格式获取它?
标签: javascript firebase react-native google-cloud-firestore firebase-storage