python - 使用 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.'
解决方案
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)
推荐阅读
- python - 为 Flask 中的所有蓝图注册全局请求处理程序
- python - 从 pandas 数据框中创建字典。列上包含集合
- opencv - 在 OpenCV 中检测圆圈
- android-studio - 为什么除非强制,否则 android studio 红色灯泡不显示?
- django - 创建没有时区的日期时间字段
- python - 在 Python2 中,我可以在没有参数的情况下在 __init__ (+ 其他方法)中调用 super 以便调用超级方法 w\ 与给定方法相同的参数?
- .net - 无法正确拆分字符串
- r - 从宽格式标记字段
- ios - 如何在 swift 4.0 中将数据附加到标签的末尾
- django - 尽管我什么都没做,但 Postgres 有很多活动正在进行