簡體   English   中英

使用 Python 從 S3 將文件上傳到 FTP 位置

[英]Upload Files to FTP location from S3 using Python

如何使用 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM