I have a requirement where I need to copy all CSV files from the S3 bucket to SFTP location.
I am able to connect and connection established but I was not sure how to move files using Paramiko.
Following code:
print('Establishing SFTP connection with ' + hostname + ' at port: ' + port)
filebjects = s3_client.list_objects_v2(Bucket=bucket_name)
# print(filebjects)
try:
print("port")
# return
bucket = s3_rs.Bucket(str(keybucket))
for obj in bucket.objects.all():
key = obj.key
body = obj.get()['Body'].read()
decodedKey = body.decode('utf-8')
key = paramiko.RSAKey.from_private_key(StringIO(decodedKey))
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
transport = paramiko.Transport((hostname,int(port)))
transport.connect(username=username,pkey=key)
sftp = paramiko.SFTPClient.from_transport(transport)
print("sftp")
client.put(localpath="fileName",remotepath="SFTP_REMO_FOLDER",)
sftp.chdir(path=remote_directory)
print(sftp.listdir(path='.'))
# for object in filebjects['Contents']:
# obj=client.get_object(Bucket=bucket_name,Key=fileName)
# file_data = obj['Body'].read()
# print(file_data)
# return
transport.close()
I am not sure what should be in the file name here I am getting all objects and file name separate. Any suggestion?