我需要的是将 .csv 文件从 SFTP 服务器复制到 Google Cloud Storage 存储桶,而无需在本地下载该文件。
这是迄今为止我的代码:
from google.cloud import storage
import io
import paramiko
# connection parameters
host = 'host'
port = 22
usr = 'me'
pwd = '0123'
# Connect to the SFTP server
sftp_client = paramiko.SSHClient()
sftp_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sftp_client.connect(host, port, usr, pwd)
sftp = sftp_client.open_sftp()
# Get the file
sftp_file = sftp.open('./file.csv', 'rb')
# Call the Google Cloud Storage API
storage_client = storage.Client()
bucket_name = 'the_bucket'
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(sftp_file)
# Upload to bucket
blob.upload_from_filename(sftp_file)
这是返回的错误:
ValueError: <paramiko.sftp_file.SFTPFile object at 0x0000022665279A00> could not be converted to unicode
有人可以帮我解开吗?
Blob.upload_from_file
上传您使用 SFTPClient.open
打开的类文件对象:
with sftp.open('./file.csv', 'rb') as sftp_file:
blob.upload_from_filename(sftp_file)