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

时间:2018-07-13 13:41:40

标签: google-cloud-firestore flutter google-cloud-functions firebase-storage

如何从新存储的图片中获取带有图片名称的网址?

我正在使用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
        })
    });
});

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

1 个答案:

答案 0 :(得分:2)

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

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

您可以通过上面的云功能创建条目,然后使用Firebase数据库访问权限(实时数据库或Cloud Firestore)从Flutter中读取条目