Answer a question

I am getting the file size and uploading it to S3:

def transfer_file_from_ftp_to_s3(bucket_name, ftp_file_path, s3_file_path, ftp_username, ftp_password, chunk_size):
ftp_connection = open_ftp_connection(FTP_HOST, int(FTP_PORT), ftp_username, ftp_password) 
ftp_file = ftp_connection.file(ftp_file_path, 'r')
s3_connection = boto3.client('s3')
ftp_file_size = ftp_file._get_size()

if ftp_file_size <= int(chunk_size):
    #upload file in one go
    print('Transferring complete File from FTP to S3...')
    ftp_file_data = ftp_file.read()
    s3_connection.upload_fileobj(ftp_file_data, bucket_name, s3_file_path)
    print('Successfully Transferred file from FTP to S3!')
    ftp_file.close()

I got this error message below:

Transferring complete File from FTP to S3...
Traceback (most recent call last):

  File "/Users/admin/anaconda2/lib/python2.7/site-packages/boto3/s3/inject.py", line 520, in upload_fileobj
    raise ValueError('Fileobj must implement read')

ValueError: Fileobj must implement read

Can you give me some pointers? Thank you.

Answers

You're uploading the data you read from the file object, but the method name suggests that you should pass the file object itself:

s3_connection.upload_fileobj(ftp_file, bucket_name, s3_file_path)
Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐