firebase - 如何使用 Cloud Functions for Firebase 将 FTP 文件上传到 firebase 存储?

标签 firebase google-cloud-functions

在同一个 firebase 项目中并使用云函数(用 node.js 编写),我首先下载一个 FTP 文件(使用 npm 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) 确保桶被正确引用。最初我只是用 我的 project_id 末尾没有“.appspot.com”。

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

2) 首先创建一个桶流,然后将流从 FTP get 调用管道传输到 bucketWriteStream。请注意,file_name 将是保存文件的名称(该文件不必事先存在)。

ftpSource.get(filePath, function(err, stream) {
  if (err) throw err
  stream.once('close', function() { ftpSource.end() })

  // This didn't work !
  //stream.pipe(fs.createWriteStream(fileName))

  // This works...
  let bucketWriteStream = bucket.file(fileName).createWriteStream()
  stream.pipe(bucketWriteStream)
})

Et voilà,就像一个魅力...

关于firebase - 如何使用 Cloud Functions for Firebase 将 FTP 文件上传到 firebase 存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44555569/

相关文章:

firebase - 如何在 Flutter 中将 onAuthStateChanged 用于 Firebase Auth?

google-cloud-functions - 如何用谷歌云监控监控云函数错误率?

Firebase - 云函数 : Always running functions

typescript - Firebase 模拟器缺少 tsconfig.json?

ios - 如果没有连接,则不会调用 Firebase withCompletionBlock

android - 有没有办法将后端(Kotlin)服务器应用程序部署到 Firebase?

javascript - 向循环 firebase 云功能中的所有电子邮件发送电子邮件

node.js - 如何从 Firebase Cloud Function 访问 firestore.Timestamp

firebase - MissingPluginException(未在 channel plugins.flutter.io/firebase_auth 上找到方法 signInWithCredential 的实现)

firebase - 无法安装 Firebase 消息包