簡體   English   中英

使用Cloud功能為Firebase存儲圖像

[英]Storing Image Using Cloud Functions for Firebase

我正在嘗試重構一些代碼以使用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 Plan(免費)在Firebase上部署功能?

如果答案是肯定的,那你的問題是因為:

Spark計划中的Firebase項目只能向Google API發出出站請求。 對第三方API的請求失敗並顯示錯誤。 有關升級項目的更多信息。

由於您嘗試發出外部請求 ,因此執行函數時不會發生任何事情:(

多數民眾贊成在現在! 既然你有管理員。 只是改變

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

const bucket = admin.storage().bucket();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM