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):
    try:
        print('Connecting')
        transport = paramiko.Transport(host,port)
        transport.connect(username = username, pkey = mykey)
        sftp = paramiko.SFTPClient.from_transport(transport)
        print('Connected')
    except:
        print('Failed to connect')
    try:
        if sftp.listdir ==0:
            print("Empty directory")
        else:
            for filenames in sftp.listdir(remote_path):
                file_remote = str(remote_path + filenames)
                print(file_remote)
                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))
    sftp.close()
Exact Lambda error:

Connecting
Connected
/remotepath/testing.txt
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/SFTPS3.py", line 19, in handlern sftp.get(os.environ['REMOTEFILE'], LOCALFILE)n", " File "/opt/python/paramiko/sftp_client.py", line 802, in getn size = self.getfo(remotepath, fl, callback)n", " File "/opt/python/paramiko/sftp_client.py", line 780, in getfon fr.prefetch(file_size)n", " File "/opt/python/paramiko/sftp_file.py", line 471, in prefetchn while n < file_size:n" ] }





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