gpt4 book ai didi

google-cloud-firestore - 如何使用带有 Flutter 的 Cloud Function 从新图像获取 URL?

转载 作者:IT王子 更新时间:2023-10-29 06:41:32 24 4
gpt4 key购买 nike

如何使用图像名称从 fire-storage 中的新图像获取 URL?

我正在使用 firebase_storage 使用 Flutter 上传图片插件

flutter 中的代码:

StorageReference ref =
FirebaseStorage.instance.ref().child("$stamp.jpg");
StorageUploadTask uploadTask = ref.putFile(imageFile);
Uri downloadUrl = (await uploadTask.future).downloadUrl;
downloadableUrl = downloadUrl.toString();

但是图像尺寸太大,因此我创建了一个 Cloud Function 来从上传的图像中生成质量较低的图像。

云函数:

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
})
});
});

如何在 flutter 中获取调整大小图像的网址?我需要在数据注册中使用这个URL

最佳答案

我很确定没有简单的方法。

我会在 Firebase 中维护一个表,其中原始文件名作为键,新文件名作为值。

您从上面的云函数创建条目,并使用 Firebase 数据库访问(实时数据库或 Cloud Firestore)从 Flutter 中读取它

关于google-cloud-firestore - 如何使用带有 Flutter 的 Cloud Function 从新图像获取 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51326552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com