Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用firebase的云功能将FTP文件上载到firebase存储?_Firebase_Google Cloud Functions - Fatal编程技术网

如何使用firebase的云功能将FTP文件上载到firebase存储?

如何使用firebase的云功能将FTP文件上载到firebase存储?,firebase,google-cloud-functions,Firebase,Google Cloud Functions,在同一个firebase项目中,使用node.js中编写的云函数,我首先使用npm FTP模块下载一个FTP文件,然后尝试将其上载到firebase存储中 到目前为止,每一次尝试都失败了,文档也没有帮助……如果有任何专家建议/提示,我们将不胜感激 以下代码使用两种不同的方法:fs.createWriteStream和bucket.file.createWriteStream。两者都失败,但原因不同,请在代码中查看错误消息 'use strict' // [START import] let a

在同一个firebase项目中,使用node.js中编写的云函数,我首先使用npm FTP模块下载一个FTP文件,然后尝试将其上载到firebase存储中

到目前为止,每一次尝试都失败了,文档也没有帮助……如果有任何专家建议/提示,我们将不胜感激

以下代码使用两种不同的方法:fs.createWriteStream和bucket.file.createWriteStream。两者都失败,但原因不同,请在代码中查看错误消息

'use strict'

// [START import]
let admin = require('firebase-admin')
let functions = require('firebase-functions')
const gcpStorage = require('@google-cloud/storage')()
admin.initializeApp(functions.config().firebase)    
var FtpClient = require('ftp')
var fs = require('fs')
// [END import]

// [START Configs]
// Firebase Storage is configured with the following rules and grants read write access to everyone
/*
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}
*/
// Replace this with your project id, will be use by: const bucket = gcpStorage.bucket(firebaseProjectID)
const firebaseProjectID = 'your_project_id'
// Public FTP server, uploaded files are removed after 48 hours ! Upload new ones when needed for testing
const CONFIG = {
  test_ftp: {
    source_path: '/48_hour',
    ftp: {
      host: 'ftp.uconn.edu'
    }
  }
}
const SOURCE_FTP  = CONFIG.test_ftp
// [END Configs]

// [START saveFTPFileWithFSCreateWriteStream]
function saveFTPFileWithFSCreateWriteStream(file_name) {
  const ftpSource = new FtpClient()
  ftpSource.on('ready', function() {
    ftpSource.get(SOURCE_FTP.source_path + '/' + file_name, function(err, stream) {
      if (err) throw err
      stream.once('close', function() { ftpSource.end() })
      stream.pipe(fs.createWriteStream(file_name))
      console.log('File downloaded: ', file_name)
    })
  })
  ftpSource.connect(SOURCE_FTP.ftp)
}
// This fails with the following error in firebase console:
// Error: EROFS: read-only file system, open '20170601.tar.gz' at Error (native)
// [END saveFTPFileWithFSCreateWriteStream]

// [START saveFTPFileWithBucketUpload]    
function saveFTPFileWithBucketUpload(file_name) {
  const bucket = gcpStorage.bucket(firebaseProjectID)
  const file = bucket.file(file_name)
  const ftpSource = new FtpClient()
  ftpSource.on('ready', function() {
    ftpSource.get(SOURCE_FTP.source_path + '/' + file_name, function(err, stream) {
      if (err) throw err
      stream.once('close', function() { ftpSource.end() })
      stream.pipe(file.createWriteStream())
      console.log('File downloaded: ', file_name)
    })
  })
  ftpSource.connect(SOURCE_FTP.ftp)
}    
// [END saveFTPFileWithBucketUpload]

// [START database triggers]
// Listens for new triggers added to /ftp_fs_triggers/:pushId and calls the saveFTPFileWithFSCreateWriteStream
// function to save the file in the default project storage bucket
exports.dbTriggersFSCreateWriteStream = functions.database
  .ref('/ftp_fs_triggers/{pushId}')
  .onWrite(event => {
    const trigger = event.data.val()
    const fileName = trigger.file_name // i.e. : trigger.file_name = '20170601.tar.gz'
    return saveFTPFileWithFSCreateWriteStream(trigger.file_name)
    // This fails with the following error in firebase console:
    // Error: EROFS: read-only file system, open '20170601.tar.gz' at Error (native)
  })
// Listens for new triggers added to /ftp_bucket_triggers/:pushId and calls the saveFTPFileWithBucketUpload
// function to save the file in the default project storage bucket
exports.dbTriggersBucketUpload = functions.database
  .ref('/ftp_bucket_triggers/{pushId}')
  .onWrite(event => {
    const trigger = event.data.val()
    const fileName = trigger.file_name // i.e. : trigger.file_name = '20170601.tar.gz'
    return saveFTPFileWithBucketUpload(trigger.file_name)
    // This fails with the following error in firebase console:
    /*
    Error: Uncaught, unspecified "error" event. ([object Object])
    at Pumpify.emit (events.js:163:17)
    at Pumpify.onerror (_stream_readable.js:579:12)
    at emitOne (events.js:96:13)
    at Pumpify.emit (events.js:188:7)
    at Pumpify.Duplexify._destroy (/user_code/node_modules/@google-cloud/storage/node_modules/duplexify/index.js:184:15)
    at /user_code/node_modules/@google-cloud/storage/node_modules/duplexify/index.js:175:10
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickDomainCallback (internal/process/next_tick.js:122:9)
    */
  })
// [END database triggers]

我终于找到了实现这一点的正确方法

1确保铲斗正确参考。起初我只是用 我的项目id结尾没有“.appspot.com”

const bucket = gsc.bucket('<project_id>.appspot.com')

Et voilá,像一个符咒一样工作…

我终于找到了实现这一点的正确方法

1确保铲斗正确参考。起初我只是用 我的项目id结尾没有“.appspot.com”

const bucket = gsc.bucket('<project_id>.appspot.com')

Et voilá,工作起来很有魅力…

请编辑您的问题,以包含函数的相关代码。抱歉,我添加了代码,因为我测试了两种不同的方法,但没有成功。请编辑您的问题,以包含函数的相关代码。抱歉,我添加了代码,因为我测试了两种不同的方法,但没有成功。