bucket.upload()时Firebase云功能连接错误

时间:2018-05-07 16:21:39

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

当我尝试将生成的PDF文件上传到存储桶时,firebase日志会在log" Init上传文件后给我回复...":

  

函数执行花了3721毫秒,完成状态:'连接   错误'

也许问题可能是Promises的顺序。但我初学云功能和Node.js重新排序。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const pdf = require('html-pdf');
const gcs = require('@google-cloud/storage')({keyFilename: './service_account.json'});
const handlebars = require('handlebars');
const path = require('path');
const os = require('os');
const fs = require('fs');
const bucket = gcs.bucket(bucketURL);

admin.initializeApp(functions.config().firebase);
var html = null;

exports.generatePdf = functions.https.onRequest((request, response) => {
    // data to apply to template file
    const user = {
        "date": new Date().toISOString(),
        "title": "Voucher",
        "body": "Voucher body"
    };
    const options = {
        "format": 'A4',
        "orientation": "portrait"
    };

    const localPDFFile = path.join(os.tmpdir(), 'localPDFFile.pdf');

    try {
        const source = fs.readFileSync(__dirname + '/voucher.html', 'utf8');
        html = handlebars.compile(source)(user);
    } catch (error) {
        console.error(error);
    }

    const phantomJsCloud = require("phantomjscloud");
    const browser = new phantomJsCloud.BrowserApi(phantomApiKey);

    var pageRequest = { content: html, renderType: "pdf" };

    // // Send our HTML to PhantomJS to convert to PDF

    return browser.requestSingle(pageRequest)
        .then(function (userResponse) {
            if (userResponse.statusCode !== 200) {
                console.log("invalid status code" + userResponse.statusCode);
            } else {
                console.log('Successfully generated PDF');

                // Save the PDF locally
                fs.writeFile(localPDFFile, userResponse.content.data, {
                    encoding: userResponse.content.encoding,
                }, function (err) {
                    console.log('Init upload of file...' + localPDFFile);
                    // Upload the file to our cloud bucket
                    return bucket.upload(localPDFFile, { 
                        destination: '/pdfs/voucher.pdf', 
                        metadata: { 
                            contentType: 'application/pdf' 
                        } 
                    }).then(() => {
                        console.log('bucket upload complete: ' + localPDFFile);
                        response.status(200).send({
                            message: 'PDF Gerado com sucesso!',
                            address: localPDFFile
                         });
                        return true;
                    }).catch(error => {

                        response.status(400).send({
                            message: 'Error on bucket upload!',
                            error: error
                         });

                        return false;
                    });
                });
                return true;
            }
            return true;
        });
})

0 个答案:

没有答案