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

问题描述 投票:2回答:1

如何从具有映像名称的消防新映像中获取URL?

[我正在使用firebase_storage插件通过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
        })
    });
});

如何在抖动中获得调整大小的图像网址?我需要在数据注册中使用此URL

google-cloud-firestore flutter google-cloud-functions firebase-storage
1个回答
2
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.