【问题标题】:FTPS to AWS S3 with ftplib and boto3 Error: Fileobj must implement read使用 ftplib 和 boto3 到 AWS S3 的 FTPS 错误:Fileobj 必须实现读取
【发布时间】:2020-11-17 22:04:40
【问题描述】:

我正在使用 python 创建从我们的 ftps 站点到 S3 存储桶的数据传输。

for ftp_file in files_to_upload:
                ftp_file_obj = self.ftp_client.retrbinary('RETR '+ftp_file,open(ftp_file,'wb').write)
                print(ftp_file_obj)
                if self.s3_upload(
                    ftp_file_obj, ftp_file):
                    print('file uploaded to s3')
                    files_to_move.append(ftp_file)

我必须遍历 FTP 目录并将这些文件加载​​到内存中。然后,我需要将该文件放入内存并将其推送到 S3。我得到 Fileobj must implement read 错误,但我不知道为什么。

我确实有代码打印ftp_file_obj,我很奇怪这是成功传输的字符串打印输出。我一定是错误地使用了retrbinary,或者错误地存储了文件。是的,虽然我确实希望它在内存中,但在这种情况下我使用 write 来确保文件看起来符合预期。

【问题讨论】:

    标签: python amazon-s3 boto3 ftplib ftps


    【解决方案1】:

    好的,想通了。将其流式传输到 r,即 BytesIO(),其中 r.write 作为 retrbinary 上的回调。然后,使用 r.seek(0) 将光标移动到数据块的开头。然后上传。我不推荐使用 ftplib 库,除非你必须使用它。我必须这样做,因为我需要使用 FTP_TLS,而 paramiko 库不支持 TLS。我希望 Google 同事发现这很有用。

    for ftp_file in files_to_upload:
        s3_partition = self.create_s3_partition(ftp_file)
        r=BytesIO()
        self.ftp_client.retrbinary('RETR '+ftp_file, \
        r.write)
        r.seek(0)
        if self.s3_upload_file_multipart(
            r, s3_partition+ftp_file+date_long):
            print(ftp_file+' uploaded to s3')
            files_to_move.append(ftp_file)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-28
      • 2021-10-18
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多