python error unsupported operand type(s) for + NoneType and int

by: sfaria, 1 year ago

I have a Lambda function in python which i am using to download files from SFTP server and add it to an S3 bucket.

I am getting an error "unsupported operand type(s) for +: 'NoneType' and 'int'" and I don't understand why.

Can anyone please advise on how to solve this? see code below:

import paramiko
import boto3

remote_path = str('remotepath')
host = 'HOST URL'
port = 22
username = 'username'
mykey = paramiko.RSAKey.from_private_key_file("theprivatekey")
s3 = boto3.client('s3')

def handler(event, context):
        transport = paramiko.Transport(host,port)
        transport.connect(username = username, pkey = mykey)
        sftp = paramiko.SFTPClient.from_transport(transport)
        print('Failed to connect')
        if sftp.listdir ==0:
            print("Empty directory")
            for filenames in sftp.listdir(remote_path):
                file_remote = str(remote_path + filenames)
                ftp_file = sftp.file(file_remote,'r')
                print("Read File and start saving to s3")
                s3.upload_fileobj(ftp_file, Bucket='s3buckethere', Key=str(filenames))
    except Exception as e:
        print('Failed: '+ str(e))
Exact Lambda error:

Read File and start saving to s3
Failed: unsupported operand type(s) for +: 'NoneType' and 'int'
Full trace: Had to change it up a bit to get this response Response: { "errorMessage": "'<' not supported between instances of 'int' and 'NoneType'", "errorType": "TypeError", "stackTrace": [ " File "/var/task/", line 19, in handlern sftp.get(os.environ['REMOTEFILE'], LOCALFILE)n", " File "/opt/python/paramiko/", line 802, in getn size = self.getfo(remotepath, fl, callback)n", " File "/opt/python/paramiko/", line 780, in getfon fr.prefetch(file_size)n", " File "/opt/python/paramiko/", line 471, in prefetchn while n < file_size:n" ] }

You must be logged in to post. Please login or register an account.