【问题标题】:How can I upload an FTP file to firebase storage using Cloud Functions for Firebase?如何使用 Cloud Functions for Firebase 将 FTP 文件上传到 Firebase 存储?
【发布时间】:2017-06-14 22:15:23
【问题描述】:

在同一个 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]

【问题讨论】:

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

标签: firebase google-cloud-functions


【解决方案1】:

我终于找到了正确的实现方法。

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)
})

等等,就像一个魅力......

【讨论】:

  • 能否请您显示整个代码以更好地解释这一点。
  • ftp 服务器地址是什么?
猜你喜欢
  • 2021-06-16
  • 2019-03-23
  • 2023-04-01
  • 2017-09-27
  • 2019-04-08
  • 1970-01-01
  • 2017-08-15
  • 2018-04-24
相关资源
最近更新 更多