将数据从 Google 云存储流式传输到 FTP 服务器

Streaming data from Google Cloud Storage to an FTP Server

我正在尝试使用 gcsfs and ftplib 通过 lines/chunks 将 CSV 从云存储传输到 FTP 服务器。我在 GCS 中有无法存储在内存中的大文件,因此我正在尝试以这种方式进行测试。

from ftplib import FTP
import gcsfs
from urllib import request
import io

ftp = FTP('my-ftp-server')

fs = gcsfs.GCSFileSystem(project='my-project')

with fs.open('myFile.csv') as f:
    ftp.storlines("STOR myFile.csv", f)

但我收到错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-56-d461792392dd> in <module>
      1 with fs.open('myfile') as f:
----> 2     ftp.storlines("STOR myFile.csv", f)

~\.conda\envs\py3.7\lib\ftplib.py in storlines(self, cmd, fp, callback)
    530         with self.transfercmd(cmd) as conn:
    531             while 1:
--> 532                 buf = fp.readline(self.maxline + 1)
    533                 if len(buf) > self.maxline:
    534                     raise Error("got more than %d bytes" % self.maxline)

TypeError: readline() takes 1 positional argument but 2 were given

关于如何解决这个问题或实现我想要的目标有什么建议吗?

的确,fsspec.AbstractFileSystem (on which GCSFileSystem is based), particularly its readline method,好像和ftplib不兼容。


是否需要使用FTP.storlines(文本模式)?不能用FTP.storbinary(二进制模式)吗?

with fs.open('myFile.csv') as f:
    ftp.storbinary("STOR myFile.csv", f)

FTP.storbinary 按块传输文件(由可选参数 blocksize 定义,默认值为 8192)。


如果没有,您将必须实施一个包装器 class,其 API 与 FTP.storlines:

兼容
class GCSFileSystemCompat:

    def __init__(self, f):
        self.f = f

    def readline(self, size):
        return f.readline()

with fs.open('myFile.csv') as f,
    ftp.storlines("STOR myFile.csv", GCSFileSystemCompat(f))

(未经测试,但应该能给你思路)