将 paramiko.sftp_file.SFTPFile 对象转换为 unicode

问题描述 投票:0回答:1

我需要的是将 .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

有人可以帮我解开吗?

python google-cloud-platform google-cloud-storage sftp paramiko
1个回答
0
投票

使用

Blob.upload_from_file
上传您使用
SFTPClient.open
打开的类文件对象:

with sftp.open('./file.csv', 'rb') as sftp_file:
    blob.upload_from_filename(sftp_file)
© www.soinside.com 2019 - 2024. All rights reserved.