为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Python 通过流将数据从 S3 传输到 FTP 服务器

使用 Python 通过流将数据从 S3 传输到 FTP 服务器

慕沐林林 2023-06-13 17:02:00
使用 Python,我想将匹配模式的文件sample1从 AWS S3 直接复制到 FTP 服务器,而无需下载到本地临时位置。我尝试了以下操作:import s3fsfrom ftplib import FTP_TLSs3 = s3fs.S3FileSystem(anon=False)pattern = 'sample1'rest = [i for i in list if pattern in i]restftp = FTP_TLS("ftp.test.com")ftp.login(user ='myUser', passwd = 'PassWrd')ftp.cwd("box_dest")for f in rest:    print(f)    with open(f, 'r') as fu:        ftp.storbinary('STOR ' + f, fu)我越来越:[u'test-bucket/abc/test/sample1.csv']test-bucket/abc/test/sample1.csvTraceback (most recent call last):  File "<stdin>", line 3, in <module>IOError: [Errno 2] No such file or directory: u'test-bucket/abc/test/sample1.csv'关于如何实现这一目标的任何建议?谢谢!
查看完整描述

1 回答

?
红颜莎娜

TA贡献1842条经验 获得超12个赞

要从 S3 读取文件,您需要使用S3FileSystem.open,而不是os.open.


在指定目标 FTP 路径时,您只需要从原始 S3 路径中提取一个文件名。posixpath.basename应该做。


for f in rest:

    print(f)

    with s3.open(f, 'r') as fu:

        ftp.storbinary('STOR ' + posixpath.basename(f), fu)


查看完整回答
反对 回复 2023-06-13
  • 1 回答
  • 0 关注
  • 110 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信