Firestore上传并调整图像大小,并获取压缩图像下载url



我安装了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);
});

但它当然不能工作,因为我们不知道压缩图像什么时候可以准备好。显然,在得到成功响应之前进行某种延迟循环是浪费。

我在想的一件事(但不喜欢作为一种变通方法(是,既然我在成功调整大小时删除了原始图像,也许我可以以某种方式听它,当删除时,按照我上面的建议获取调整大小的图像?

那我该怎么办?

您需要通过检查上传是否为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扩展的一个警告,我发现依赖一个专用的云函数更合适,我们可以调用它来在完成时返回值。

最新更新