I am trying to send a CSV file to an SFTP server using a Google Cloud Function.
This is the Python script I am using -
import paramiko
import os
def hello_sftp(event, context):
myPassword = os.environ.get('SFTP_PASSWORD')
host = "HostName"
username = "TestUser"
password = myPassword
file_name = 'test.csv''
port = 22
transport = paramiko.Transport((host, port))
destination_path = "/"+file_name
local_path = "gs://testbucket/"+file_name #GCP Bucket address
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put(local_path, destination_path)
sftp.close()
transport.close()
To authenticate to the SFTP server I need to use the RSA file. In the Secret Manager I have uploaded the Secret value in the Secret Manager and Used the value as an Environment variable in the Google Cloud Function. But I think I am doing something wrong here -
myPassword = os.environ.get('SFTP_PASSWORD')
because I this line I think I am getting this error deploying message -
Deployment failure:
Function failed on loading user code. This is likely due to a bug in the user code.
Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.
In the logs I can find the following error message -
2022-01-23T23:31:59.838337ZCloud FunctionsCreateFunctioneurope-west3:[email protected] {@type: type.googleapis.com/google.cloud.audit.AuditLog, authenticationInfo: {…}, authorizationInfo: […], methodName: google.cloud.functions.v1.CloudFunctionsService.CreateFunction, request: {…}, requestMetadata: {…}, resourceLocation: {…}, resourceName: projects/testServer-test/locations/eur…
{@type: type.googleapis.com/google.cloud.audit.AuditLog, authenticationInfo: {…}, authorizationInfo: […], methodName: google.cloud.functions.v1.CloudFunctionsService.CreateFunction, request: {…}, requestMetadata: {…}, resourceLocation: {…}, resourceName: projects/testServer-test/locations/eur…
Can anyone point me, to where I am doing wrong here or the Script is wrong??