使用Python从S3将文件上载到FTP位置

2024-06-16 09:33:03 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用ftplib和boto3在Python中将特定模式的文件从S3位置上传到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.'

Tags: 文件csvimporthosts3tlsftpfiles
1条回答
网友
1楼 · 发布于 2024-06-16 09:33:03
 
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)

相关问题 更多 >