I am trying to send a csv
file to a SFTP
server using a Google Cloud Function
.
This is the Python script I am using -
import base64
import os
import pysftp
import re
import csv
from google.cloud import storage
from google.cloud import bigquery
def hello_sftp(event, context):
#defining credentials for the transfer
myHostName = 'HostName'
myUsername = 'TestUser'
myPassword = 'RSA' // I don't have any password, instead I need to user a RSA key
filename = 'file.csv' // This is my file Name
path = "gs://mytestbucket/" // Here is my csv file stored
copy_file_on_ftp(myHostName, myUsername, myPassword, filename, path)
def copy_file_on_ftp(myHostName, myUsername, myPassword, filename, localpath):
remotepath = '/Export/' + str(filename)
print(' ')
print(localpath)
print(' ')
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(
host=myHostName, username=myUsername, password=myPassword, cnopts=cnopts
) as sftp:
print("Connection successfully established . . . ")
print("Exporting . . . ")
print("local path and file name is : ", localpath+filename)
sftp.put(localpath =localpath+filename, remotepath=remotepath)
sftp.close()
print("export to sFTP successful!")
From the Script you can see that I don't have any Password
to be able to connect to the SFTP SERVER
, instead I have an RSA KEY
.
Now my question is how can I put the RSA key into this Script? Does anyone know??