【发布时间】:2021-10-22 12:21:25
【问题描述】:
我想实现以下目标,但我目前陷入困境,因为 API 文档太难了。我需要一些指导。
创建一个缩略图,将图像大小调整为 100 像素的宽度。
以下是应该做什么的详细信息。
- 每当在 Firestore 数据库中创建新文档时,都应创建缩略图。
- 该文档包含存储在 Firebase Cloud Storage 中的图像的永久 URL。 (这个url是通过flutter cloud storage package.中的
getDownloadUrl()方法生成的) - 生成的缩略图应与原始图像存储在同一目录中。
- 缩略图的 URL 应更新为 Firestore 数据库文档字段。
我目前正在使用 TS,但用 JS 回复也可以。
这是显示我当前进度的代码。
const firestore = admin.firestore();
export const generateThumbnailOnCreate = functions
.firestore.document("missions/{missionId}")
.onCreate(async (snap, context) => {
const doc = snap.data();
const missionImgUrl = doc.mission_img_url;
/*
* TODO
* use the mission img url to access the cloud storage
* create thumbnail at the storage directory
* get the permanent thumbnail url update firestore document
*/
return snap.ref
.update({ thumbnail_img_url: thumbnailImgUrl })
.then((docRef) => console.log("Success"))
.catch((err) => console.error("Error"));
});
【问题讨论】:
-
"该文档包含存储在 Firebase 云存储中的图像的 url。" => 哪个确切的 URL?以
gs://(文件位置)或签名 URL 开头的那个?这个 URL 是如何生成的? -
@Renaud Tarnec 该url是由flutter firebase sdk中的getDownloadUrl()方法生成的。 firebase.flutter.dev/docs/storage/usage
-
拥有一个下载地址来管理比云存储路径更难。我建议您也存储路径,并使用该路径从云函数中获取文件。
标签: firebase google-cloud-firestore google-cloud-functions google-cloud-storage