使用 HTTP 下载一个巨大的文件并上传到 FTP 服务器而不存储它

Download a giant file with HTTP and upload to FTP server without storing it

我有一个项目,我应该使用 HTTP 下载一些大文件并将它们上传到 FTP 服务器。最简单的方法是先下载文件再上传到FTP;将其视为两个独立的阶段。

但是我们可以在下载文件的同时使用流来上传文件吗?这种方式似乎更有效率。欢迎任何特别在 python 中的示例。

使用requests module to obtain a file-like object representing the HTTP download and use it with ftplib FTP.storbinary:

from ftplib import FTP
import requests 

url = "https://www.example.com/file.zip"
r = requests.get(url, stream=True)
ftp = FTP(host, user, passwd)
ftp.storbinary("STOR /ftp/path/file.zip", r.raw)