我正在获取文件大小并将其上传到 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
你能给我一些指点吗?谢谢。
关于如何上传数据的字节表示的更一般的答案将使用 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')
我在用着请求包 https://requests.readthedocs.io/de/latest/必须首先使用 pip 下载。
这个答案是基于jwodder https://stackoverflow.com/users/744178/jwodder's 回答另一个问题 https://stackoverflow.com/a/16007452/5302210
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)