【问题标题】:Passing a localy function to cloud function and saving on bucket将本地函数传递给云函数并保存在存储桶上
【发布时间】:2021-12-21 12:16:45
【问题描述】:

我在 nodejs localhost 上完美运行了以下函数:

// index.js
const scrape = require('website-scraper');
const PuppeteerPlugin = require('website-scraper-puppeteer');
const path = require('path');
const zipFolder = require('zip-folder')

scrape({
    // Provide the URL(s) of the website(s) that you want to clone
    // In this example, you can clone the Our Code World website
    urls: ['https://appsevenpage.com/'],
    // Specify the path where the content should be saved
    // In this case, in the current directory inside the ourcodeworld dir
    directory: path.resolve(__dirname, 'appseven'),
    // Load the Puppeteer plugin
    plugins: [
        new PuppeteerPlugin({
            launchOptions: {
                // If you set  this to true, the headless browser will show up on screen
                headless: true
            }, /* optional */
            scrollToBottom: {
                timeout: 10000,
                viewportN: 10
            } /* optional */
        })
    ]
}).then(() => {
    zipFolder('appseven', 'appseven.zip', function(err) {
        if(err) {
            console.log('oh no!', err)
        } else {
            
        }
    })
})

在这个函数中,我有一个“目录”,它是保存所有文件的文件夹(本地),如打印所示:

此文件夹“appseven”,我想在我的 firebase 存储桶存储中另存为 .zip 文件。如:“appseven.zip”。

我如何使用云函数 onRequest 来做到这一点?!

这是我使用云函数编写的代码库:

const functions = require('firebase-functions')
const { initializeApp } = require('firebase-admin/app')
const mkdirp = require('mkdirp')
const path = require('path')
const os = require('os')
const fs = require('fs')
const scrape = require('website-scraper')
const PuppeteerPlugin = require('website-scraper-puppeteer')

initializeApp()

exports.helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info('Hello mkdirp!', mkdirp)
  functions.logger.info('Hello path!', path)
  functions.logger.info('Hello os!', os)
  functions.logger.info('Hello fs!', fs)
  response.send('Hello from Firebase!')
  functions.logger.info('Hello path file! 1', tempFilePath)
  const tempFilePath = path.join(os.tmpdir(), 'teste')
  const tempLocalDir = path.dirname(tempFilePath)
  mkdirp(tempLocalDir).then(() => {
    scrape({
      // Provide the URL(s) of the website(s) that you want to clone
      // In this example, you can clone the Our Code World website
      urls: ['https://appseven.com/'],
      // Specify the path where the content should be saved
      // In this case, in the current directory inside the ourcodeworld dir
      directory: tempFilePath,
      // Load the Puppeteer plugin
      plugins: [
        new PuppeteerPlugin({
          launchOptions: {
            // If you set  this to true, the headless browser will show up on screen
            headless: true
          }, /* optional */
          scrollToBottom: {
            timeout: 10000,
            viewportN: 10
          } /* optional */
        })
      ]
    })
    bucket.upload(tempFilePath, {
      destination: thumbFilePath,
      metadata: metadata,
    });
    // Once the thumbnail has been uploaded delete the local file to free up disk space.
    return fs.unlinkSync(tempFilePath);
  })
  functions.logger.info('Hello path file! 2', tempFilePath)
  functions.logger.info('Hello scrape!', scrape)
  functions.logger.info('Hello PuppeteerPlugin!', PuppeteerPlugin)
})

似乎正在处理本地存储,但我确实需要关闭此临时文件夹,并且我还需要获取此临时文件夹并保存为 .zip 文件。

请帮助我,第一个使用云函数上的临时存储的函数(我也在使用最新版本的 firebase)。

【问题讨论】:

  • 如果我的回答有帮助或没有帮助,我将不胜感激。

标签: node.js google-cloud-functions google-cloud-storage zip fs


【解决方案1】:

我在documentation 中找到了这个页面,可以提供帮助。

import { getStorage, ref, uploadBytes } from "firebase/storage";

const storage = getStorage();
const storageRef = ref(storage, 'some-child');

// 'file' comes from the Blob or File API
uploadBytes(storageRef, file).then((snapshot) => {
  console.log('Uploaded a blob or file!');
});

我认为 zip 文件格式将被视为 blob 对象,因此您可以使用此方法将其上传到您的存储桶。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    相关资源
    最近更新 更多