【问题标题】:Transfer the data from S3 to FTP server via stream using Python使用 Python 通过流将数据从 S3 传输到 FTP 服务器
【发布时间】:2020-08-21 06:46:19
【问题描述】:

使用 Python,我想将匹配模式 sample1 的文件从 AWS S3 直接复制到 FTP 服务器,而无需下载到本地临时位置。 我尝试了以下方法:

import s3fs
from ftplib import FTP_TLS

s3 = s3fs.S3FileSystem(anon=False)
pattern = 'sample1'
rest = [i for i in list if pattern in i]
rest

ftp = 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.csv
Traceback (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'

关于如何实现这一点的任何建议?谢谢!

【问题讨论】:

    标签: python amazon-s3 ftp ftplib python-s3fs


    【解决方案1】:

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

    【讨论】:

    • 它的作品感谢@Martin Prikryl
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 2015-05-14
    相关资源
    最近更新 更多