使用 Python,我想將匹配模式的文件sample1從 AWS S3 直接復(fù)制到 FTP 服務(wù)器,而無需下載到本地臨時(shí)位置。我嘗試了以下操作: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'關(guān)于如何實(shí)現(xiàn)這一目標(biāo)的任何建議?謝謝!
1 回答

紅顏莎娜
TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊
要從 S3 讀取文件,您需要使用S3FileSystem.open,而不是os.open.
在指定目標(biāo) FTP 路徑時(shí),您只需要從原始 S3 路徑中提取一個(gè)文件名。posixpath.basename應(yīng)該做。
for f in rest:
print(f)
with s3.open(f, 'r') as fu:
ftp.storbinary('STOR ' + posixpath.basename(f), fu)
添加回答
舉報(bào)
0/150
提交
取消