[英]How to transfer data from Google Cloud Storage to SFTP using Python without writing to the file system
I wrote a program using pysftp to download the file from Google Cloud Storage blob, and then upload the file from the file system.我编写了一个程序,使用 pysftp 从 Google Cloud Storage blob 下载文件,然后从文件系统上传文件。 I wondered if I could bypass the file system and upload the stream to SFTP.我想知道我是否可以绕过文件系统并将 stream 上传到 SFTP。
I am using Google Cloud Functions to run my program and the file system is read-only.我正在使用 Google Cloud Functions 运行我的程序,并且文件系统是只读的。 So I can't write to disk.所以我无法写入磁盘。 Also, it would be much faster to transfer data as it avoids the step of writing and reading from the disk.此外,传输数据会更快,因为它避免了从磁盘写入和读取的步骤。
for blob in storage_client.list_blobs(bucket, prefix=prefix):
source = blob.name
destination = local_download_dir + "/" + remove_prefix(blob.name, prefix)
blob.download_to_filename(destination)
...
with pysftp.Connection(Config.SFTP_HOST, port=Config.SFTP_PORT, username=Config.SFTP_USER, password=Config.SFTP_PWD, cnopts=cnopts) as sftp:
...
files = listdir(local_download_dir)
for f in files:
sftp.put(local_download_dir + "/" + f) # upload file to remote
Supporting the community by answering my own question.通过回答我自己的问题来支持社区。 Hope some of you find it useful.希望你们中的一些人觉得它有用。
I initially tried the following, and it worked but may lead to memory issues for big files:我最初尝试了以下方法,它有效,但可能导致大文件出现 memory 问题:
from io import BytesIO
sftp.putfo(BytesIO(blob.download_as_bytes()), destination)
Then found a better approach with blob.open:然后用 blob.open 找到了一个更好的方法:
with blob.open("rb") as f:
sftp.putfo(f, destination)
In stream mode chunk_size: 40MB default在 stream 模式下 chunk_size: 40MB 默认
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.