我安装了Resize Images扩展,它正在工作。在我的应用中,我有:
const storageRef = firebase.storage().ref();
const imagesRef = storageRef.child(`users/${user?.id}/images`);
const imageRef = imagesRef.child(`${timestamp}.jpg`);
imageRef.put(blob).then((snapshot) => {
snapshot.ref
.getDownloadURL()
.then((image_url) => {
返回的image_url
是上传的原始图片,而不是调整大小的图片。
如何获取调整大小的图像的下载url?
我尝试将以下内容添加到响应中:
imagesRef
.child(`${timestamp}_1000x1000.jpg`)
.getDownloadURL()
.then((resized_image_url) => {
console.log('resized_image_url', resized_image_url);
});
但它当然不能工作,因为我们不知道压缩图像什么时候会准备好。做一些延迟循环直到我得到一个成功的响应显然是浪费的。
我在想的一件事(但不是解决办法)是,既然我在成功调整大小时删除了原始图像,也许我可以以某种方式收听它,当删除时,获取我上面建议的调整大小的图像?
那我该怎么办呢?
发布于 2021-05-06 00:28:16
您需要通过检查是否超时( exists
)来测试上传是否已经完成,这可以在从存储中获取文档引用的循环或超时中完成。
const storageFile = bucket.file('path/to/compressed/image.jpg');
storageFile
.exists()
.then((exists) => {
if (exists[0]) {
console.log("File exists");
} else {
console.log("File does not exist");
}
})
这是firebase扩展的一个警告,我发现依赖一个专用的云函数更合适,我们可以在完成时调用它来返回值。
https://stackoverflow.com/questions/67409210
复制