【问题标题】:Transfer file from SFTP to S3 using Paramiko使用 Paramiko 将文件从 SFTP 传输到 S3
【发布时间】:2021-01-19 07:24:03
【问题描述】:

我正在使用 Paramiko 访问远程 SFTP 文件夹,并且我正在尝试编写将文件从 SFTP 中的路径传输的代码(使用文件元数据检查其上次修改日期的简单逻辑)到 AWS S3 存储桶. 我已经使用 Boto3 设置了到 S3 的连接,但我似乎仍然无法编写一个工作代码来传输文件而不先将它们下载到本地目录。这是我尝试使用 Paramiko 的 getfo() 方法的一些代码。但它不起作用。

for f in files:
    # get last modified from file metadata
    last_modified = sftp.stat(remote_path + f).st_mtime
    last_modified_date = datetime.fromtimestamp(last_modified).date()
    if last_modified_date > date_limit:  # check limit
       print('getting ' + f)
       full_path = f"{folder_path}{f}"
       fo = sftp.getfo(remote_path + f,f)
       s3_conn.put_object(Body=fo,Bucket=s3_bucket, Key=full_path)

谢谢!

【问题讨论】:

    标签: python amazon-s3 boto3 sftp paramiko


    【解决方案1】:

    使用Paramiko SFTPClient.open 获取可以传递给Boto3 Client.put_object 的类文件对象:

    with sftp.open(remote_path + f, "r") as f:
        f.prefetch()
        s3_conn.put_object(Body=f)
    

    关于f.prefetch(),请参阅Reading file opened with Python Paramiko SFTPClient.open method is slow。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 2019-09-01
      • 2016-06-29
      • 2014-08-15
      • 1970-01-01
      相关资源
      最近更新 更多