我正在尝试使用Google Cloud Function
将CSV
文件发送到SFTP
服务器。
这是我正在使用的Python脚本-
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()
为了向SFTP server
进行身份验证,我需要使用RSA file
。在Secret Manager
中,我在Secret Manager
中上传了Secret value
,并在谷歌云功能中使用该值作为Environment variable
。但我觉得我在这里做错了什么-
myPassword = os.environ.get('SFTP_PASSWORD')
因为我这条线,我想我得到了这个错误部署消息-
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.
在日志中,我可以找到以下错误消息-
2022-01-23T23:31:59.838337ZCloud FunctionsCreateFunctioneurope-west3:function-SFTPxx@xx.com {@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…
有人能告诉我,我在哪里做错了,或者剧本错了吗??
从您的错误消息来看,在尝试解析代码后,函数似乎无法完全部署,因此您甚至无法访问其他部分(函数代码(。
检查你的代码是否有拼写错误、间距(又名格式(等,确保它在函数之外运行,然后在部署时,你需要将它包装在函数中,并设置触发它的任何东西。
不要忘记requirements.txt
指定要在代码中导入的库,以便在invocation中安装(您也可以在其中对它们进行版本设置(。
查看了启动器的示例代码后,将文件名="test.csv"行中.cvs''
末尾的额外'
删除为文件名="test.csv"。
然后,为了检查代码的其余行为,请尝试在使用变量之前将print语句放在下游。在触发部署的函数后,这些print语句将显示在日志中,您可以看到哪些变量看起来像@invocation。