Google cloud firestore 如何使用带有flatter的云函数从新图像获取URL?
如何从fire storage中的新图像中获取具有图像名称的URL 我正在使用插件上传一张带有颤振的图片 颤振代码:Google cloud firestore 如何使用带有flatter的云函数从新图像获取URL?,google-cloud-firestore,flutter,google-cloud-functions,firebase-storage,Google Cloud Firestore,Flutter,Google Cloud Functions,Firebase Storage,如何从fire storage中的新图像中获取具有图像名称的URL 我正在使用插件上传一张带有颤振的图片 颤振代码: StorageReference ref = FirebaseStorage.instance.ref().child("$stamp.jpg"); StorageUploadTask uploadTask = ref.putFile(imageFile); Uri downloadUrl = (await uploadTask.future).downl
StorageReference ref =
FirebaseStorage.instance.ref().child("$stamp.jpg");
StorageUploadTask uploadTask = ref.putFile(imageFile);
Uri downloadUrl = (await uploadTask.future).downloadUrl;
downloadableUrl = downloadUrl.toString();
但是图像大小太大了,因此我创建了一个云函数来从这个上传生成低质量的图像
云功能:
const functions = require("firebase-functions");
const gcs = require('@google-cloud/storage')();
const os = require('os');
const path = require('path');
const spawn = require('child-process-promise').spawn;
exports.onFileChange= functions.storage.object().onChange(event => {
const object = event.data;
const bucket = object.bucket;
const contentType = object.contentType;
const filePath = object.name;
console.log('File change detected, function execution started');
if (object.resourceState === 'not_exists') {
console.log('We deleted a file, exit...');
return;
}
if (path.basename(filePath).startsWith('resized-')) {
console.log('We already renamed that file!');
return;
}
const destBucket = gcs.bucket(bucket);
const tmpFilePath = path.join(os.tmpdir(), path.basename(filePath));
const metadata = { contentType: contentType };
return destBucket.file(filePath).download({
destination: tmpFilePath
}).then(() => {
return spawn('convert', [tmpFilePath, '-resize', '500x500', tmpFilePath]);
}).then(() => {
return destBucket.upload(tmpFilePath, {
destination: 'resized-' + path.basename(filePath),
metadata: metadata
})
});
});
如何在flatter中获得调整大小的图像Url?我需要在数据注册中使用此URL我很确定没有简单的方法 我将在Firebase中维护一个表,原始文件名作为键,新文件名作为值
您可以从上面的cloud函数创建条目,并使用Firebase数据库访问(实时数据库或cloud Firestore)从Flatter读取该条目。这确实是最常见的方法,使用Firebase实时数据库或cloud Firestore作为信号机制/消息总线。我最常见的做法是在数据库写入时触发云函数,然后在上传图像后从客户端代码触发。云函数读取元数据,查找文件并调整其大小,然后将响应写回预先约定的位置。现在你也可以考虑使用A。