使用 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條經驗 獲得超13個贊
要從 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)
添加回答
舉報
0/150
提交
取消