提问人:Haniel Baez 提问时间:10/1/2019 最后编辑:Doug StevensonHaniel Baez 更新时间:10/23/2023 访问量:1367
在 firebase 的调整大小扩展完成后获取下载 URL
Get Download Url after firebase's resize extension completed
问:
这就是我试图实现的目标,实现 firebase 的调整大小图像扩展,上传图像,然后在调整大小完成后,将该 dowloadUrl 的拇指添加到 Cloud Firestore 文档中。这个问题对我有帮助,但仍然无法识别拇指并获取下载URL,这就是我迄今为止一直在尝试的。
注意:我将缩略图设置为根/拇指
const functions = require('firebase-functions');
const { Storage } = require('@google-cloud/storage');
const storage = new Storage();
exports.thumbsUrl = functions.storage.object().onFinalize(async object => {
const fileBucket = object.bucket;
const filePath = object.name;
const contentType = object.contentType;
if (fileBucket && filePath && contentType) {
console.log('Complete data');
if (!contentType.startsWith('thumbs/')) {
console.log('This is not a thumbnails');
return true;
}
console.log('This is a thumbnails');
} else {
console.log('Incomplete data');
return null;
}
});
答:
-1赞
Siddhant Jaiswal
12/27/2019
#1
您需要使用 filePath 来检查拇指if(filePath.startswith('thumbs/'){...}
contentType 具有文件的元数据,例如图像类型等。 FilePath 将具有完整路径。
0赞
Dr.House
8/11/2022
#2
方法1:客户端
- 创建缩略图时不要更改。
access token
- 在 gcloud 云函数控制台中编辑函数
- 通过单击转到函数代码
detailed usage stats
- 然后点击代码
- 编辑以下行
- 再次重新部署函数
// If the original image has a download token, add a
// new token to the image being resized #323
if (metadata.metadata.firebaseStorageDownloadTokens) {
// metadata.metadata.firebaseStorageDownloadTokens = uuidv4_1.uuid();
}
- 使用 getDownloadURL函数获取上传的图片
https://firebasestorage.googleapis.com/v0/b/<project_id>/o/<FolderName>%2F<Filename>.jpg?alt=media&token=xxxxxx-xxx-xxx-xxx-xxxxxxxxxxxxx
- 因为访问令牌将是相似的
https://firebasestorage.googleapis.com/v0/b/<project_id>/o/<FolderName>%2Fthumbnails%2F<Filename>_300x300.jpg?alt=media&token=xxxxxx-xxx-xxx-xxx-xxxxxxxxxxxxx
方法 2:服务器端
创建缩略图后调用此函数
var storage = firebase.storage();
var pathReference = storage.ref('users/' + userId + '/avatar.jpg');
pathReference.getDownloadURL().then(function (url) {
$("#large-avatar").attr('src', url);
}).catch(function (error) {
// Handle any errors
});
0赞
Fábio C. Souza
10/23/2023
#3
我在一些项目中遇到过类似的问题,我找到的解决方案如下:
定义文件路径。
根据扩展名配置建立调整大小的文件路径。
上传图片。
添加 6 秒延迟以允许扩展工作。
利用调整大小的图像路径获取下载 URL。
endCrop(){ const file:any = this.croppedImage.split(',')[1]; let filePath = 'shop_img/' + this.auth.User.authid + '/' + 'imgname.png'; let resizePath = 'shop_img/' + this.auth.User.authid + '/' +'imgname_600x600.png'; this.storage.uploadImage(file, filePath).then(async urlorigem =>{ await this.delay(6000); //delay of 6s this.storage.storage.ref(resizePath).getDownloadURL().subscribe(url =>{ this.croppedImage = url; //got url from resized image }) })
}
0赞
DAHAOREN
12/1/2023
#4
当 Firebase 的调整大小扩展程序创建缩略图时,它应该将其存储在特定路径中,您提到的该路径设置为 root/thumbs。您编写的函数正在尝试检查对象是否为缩略图,但 contentType.startsWith('thumbs/') 条件看起来关闭。相反,您应该检查 filePath 是否包含“thumbs”目录。类似于 if (filePath.startsWith('thumbs/'))。
评论