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

我正在尝试使用gcsfsftplib通过线/块将CSV从Cloud Storage传输到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

关于如何解决此问题或实现我想要的任何建议?

qq11479366 回答:将数据从Google云端存储流式传输到FTP服务器

实际上,fsspec.AbstractFileSystemGCSFileSystem的基础),尤其是其readline method,似乎与ftplib不兼容。


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

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

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


否则,您将必须使用与FTP.storlines兼容的API来实现包装类:

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

(未经测试,但应该可以告诉您)

本文链接:https://www.f2er.com/3126373.html

大家都在问