1

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??

5
  • 2
    It's difficult to answer your question because you provide limited information: "Something is wrong here" isn't helpful. Why do you think it is wrong? Did you follow these instructions: cloud.google.com/functions/docs/configuring/secrets Please consider providing more details about what you did and what you observed to help us provide you with an answer Commented Jan 24, 2022 at 18:02
  • @DazWilkin I have followed the Instruction and also updated my questions with the error message. Thank you Commented Jan 24, 2022 at 19:07
  • Go to the logs and look for the error detail. Then add them here. Commented Jan 24, 2022 at 19:32
  • As a guess - when a cloud function is deployed, it is necessary to provide environment variables - can you show a script - how do you deploy the cloud function please? Commented Jan 24, 2022 at 19:46
  • @guillaumeblaquiere I have uploaded the error message Commented Jan 24, 2022 at 19:49

1 Answer 1

1

From your error message looks like function fails to deploy altogether after trying to parse your code so you don't even get to the other part (functions code).

Check your code for spelling mistakes, spacing aka formatting etc, make sure it runs outside of function then when deploying you will need to wrap it in a function and set whatever triggers it.

Don't forget requirements.txt to specify libraries you are importing in your code to be installed @ invocation (you can also version them within there).

After looking at your sample code for starter remove extra ' at the end of .cvs'' in file_name = 'test.csv'' line to aka file_name = 'test.csv'.

Then to check the rest of your codes behavior try to put print statements before using variables downstream those print statements will show up in your logs after you trigger your deployed function and you can see what variables looked like @ invocation.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.