我正在获取文件大小并将其上传到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()
我收到以下错误消息:
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
你能给我一些建议吗?谢谢。
发布于 2019-08-12 00:22:35
您正在上传从file对象读取的数据,但是方法名表明您应该传递file对象本身:
s3_connection.upload_fileobj(ftp_file, bucket_name, s3_file_path)
发布于 2020-10-16 20:56:00
关于如何上传数据的字节表示,一个更通用的答案是使用IO包。
s3 = boto3.resource('s3')
bucket = s3.Bucket(name="your-bucket-name-here")
data = requests.get('https://www.google.de/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png').content
bucket.upload_fileobj(io.BytesIO(data), 'googleLogoTestUpload.png')
我使用的是the request package,它必须先使用pip下载。这个答案是基于jwodder的answer on another question
发布于 2020-05-11 15:44:51
我使用了这个:s3_connection.upload_fileobj(ftp_file, bucket_name, s3_file_path)
,但无法让它工作,因为它上传了一个空文件(试图以各种方式读取该文件)。
最后,我试了试下面的代码,它的效果很不错!仅供参考,我已经用paramiko
打开了FTP连接。
ftp_file = ftp_connection.file(ftp_file_path, 'r')
ftp_file_data = ftp_file.read()
s3 = boto3.resource('s3')
s3.Object(bucket_name, s3_file_path).put(Body=ftp_file_data)
https://stackoverflow.com/questions/57451656
复制相似问题