我正在csv嘗試SFTP使用Google Cloud Function.
這是我正在使用的 Python 腳本 -
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!")
從腳本中您可以看到我沒有任何東西Password可以連接到SFTP SERVER,而是我有一個RSA KEY.
現在我的問題是如何將 RSA 密鑰放入此腳本中?有人知道嗎??
uj5u.com熱心網友回復:
問題可能是,您使用的是 Python;使用 更容易gcloud compute scp,因為可以按需提供密鑰,并且也可以訪問存盤桶。命令的順序是:
gcloud config set ...
gcloud compute config-ssh
gcloud compute scp ...
gcloud compute ssh ...
對于示例; 而這解釋了如何管理密鑰(不是他們在所有的存盤)。
雖然我想知道為什么您不只是從該服務器上的存盤桶中獲取 CSV?這種情況總是推與拉。這可能是有道理的,但并非總是如此。選擇錯誤的環境或構建比所需更復雜的解決方案,這只會增加障礙 - 并且“將 CSV 從 Google 存盤桶發送到 SFTP 服務器”不允許確定這樣做的實際目的。
uj5u.com熱心網友回復:
除了存盤私鑰的位置之外,我建議將所有機密(例如,私鑰、密碼短語等)存盤在 Secret Manager 中。云函式應該使用 Secret Manager API 獲取所有這些值,并使用它們建立 SFTP 連接。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420384.html
標籤:
