使用 Cloud Functions for Firebase 存储图像

2024-02-23

我正在尝试重构一些代码以使用 Cloud Functions for Firebase。该代码应将图像存储在 Firebase 存储中的某个路径中。大多数情况下,代码与以前完全相同,只是现在而不是

server.post('/', (req, res) => {
  // Some code 
}

我根据 Firebase 文档使用以下内容

exports.getProcessedImage = functions.https.onRequest((req, res) => {
  // Some code
});

该代码以前可以工作,但现在我无法将测试图像保存到 Firebase。不知道为什么。我检查开发人员工具中的“网络”选项卡,getProcessedImage 端点正在触发代码运行,并且它响应 200 代码,因此不确定问题是什么。预先感谢您的任何帮助!

我的完整代码如下:)

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

const request = require('request');
const crypto = require('crypto');
const storage = require('@google-cloud/storage');

// Firebase Project ID and Service Account Key.
const gcs = storage({
  projectId: 'snapshelf-aabb55',
  keyFilename: './serviceAccountKey.json'
});

const bucket = gcs.bucket('snapshelf-aabb55.appspot.com');

function saveImage(url) {

    // Generate a random HEX string using crypto (a native node module).
    const randomFileName = crypto.randomBytes(16).toString('hex');

    // Fetch image info using a HTTP HEAD request.
    // https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD
    request.head(url, (error, info) => {
        if (error) {
            return console.error(error);
    }

    // Download image from Pixelz, then save the image to Firebase
    // using the Google Cloud API and the magic of Node Streams.
    // https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-
    cloud/v0.52.0/storage/file
    // http://stackoverflow.com/questions/28355079/how-do-node-js-streams-work
    request(url)
        .pipe(
            bucket.file(`sample/images/${randomFileName}`).createWriteStream({
                metadata: {
                    contentType: info.headers['content-type']
                }
            })
        )
        .on('error', (err) => {

            // Do something if the upload fails.
            console.error(err);
        })
        .on('finish', () => {

            // Do something when everything is done.

            // Get download url for stored image
            console.log('Image successfully uploaded to Firebase Storage!')
        });
    });
}

exports.getProcessedImage = functions.https.onRequest((req, res) => {
    console.log(req.body.processedImageURL);
    /*
    if (req.body && req.body.processedImageURL) {

        // Get image from Pixelz and save it to Firebase Storage.
        saveImage(req.body.processedImageURL);

        return res.status(200).end();
    }

    res.status(400).end();
    */

    const url = 'https://www2.chemistry.msu.edu/courses/cem352/SS2017_Wulff/MichiganState.jpg'
    console.log(url);
    saveImage(url);
    console.log('Saving url');
    res.status(200).send();
});

您是否使用 Spark 计划(免费)在 Firebase 上部署 Function?

如果答案是肯定的,那么您的问题是这样的:

Spark 计划中的 Firebase 项目只能向 Google API 发出出站请求。对第三方 API 的请求失败并出现错误。有关升级项目的更多信息。

既然你正在尝试做一个外部请求,当你的函数执行时什么也没有发生:(

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Cloud Functions for Firebase 存储图像 的相关文章

随机推荐