首页 > 解决方案 > 使用 Python 将文件从 S3 上传到 FTP 位置

问题描述

如何使用 ftplib 和 boto3 将特定模式的文件从 S3 位置上传到 Python 中的 FTP 位置?

我能够从本地上传文件,如下所示,但不能从 S3 上传。有人可以建议一种方法来实现这一目标吗?

# Upload Files 
from ftplib import FTP_TLS
import glob

host    = 'ftp.xyz.com'
uname   = 'user_name'
pwd     = 'My_Password'
sdir    = 'location'
sfrmt   = 'sample*'

ftps = FTP_TLS(host)
ftps.login(user = uname, passwd = pwd)

ftps.cwd(sdir) 

files =  glob.glob(sfrmt)
files
['sample1.csv', 'sample2.csv', 'sample3.csv']
for f in files:
    with open(f, 'r') as fu:
            ftps.storbinary('STOR ' + f, fu)

'226 Transfer complete.'
'226 Transfer complete.'
'226 Transfer complete.'

标签: pythonamazon-s3

解决方案


 
import os    
import s3fs
from ftplib import FTP_TLS

ftp = FTP_TLS("xxxxxxx.com")
ftp.login(user = "UserName", passwd ="Password")
ftp.cwd("Ftp Location")

s3_path = 's3://myBucket/Prefix/files'
fs = s3fs.S3FileSystem(anon=False) 
obj_lst = fs.ls(s3_path)
upload_lst = [i for i in obj_lst if "FileFormat" in i] # Upload files of this pattern

for file in upload_lst:
    with fs.open(file, 'r') as fu:
            ftp.storbinary('STOR ' + os.path.basename(file), fu)

推荐阅读