简体   繁体   English

如何在不写入文件系统的情况下使用 Python 将数据从 Google Cloud Storage 传输到 SFTP

[英]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.

相关问题 使用 Python 将 zip 文件从 Google 云存储上传到 SFTP 服务器,而不加载到 memory - Upload zip file from Google cloud storage to SFTP server without loading into memory using Python 如何在不使用python写入文件的情况下将文件分块传输到Azure Blob存储 - how to transfer file to azure blob storage in chunks without writing to file using python 使用Python在Google Cloud Storage上读写JSON文件 - Reading & Writing JSON file on Google Cloud Storage using Python 如何使用 python 3.6 从 SFTP 传输文件 - How can I transfer file from SFTP using python 3.6 我们可以使用 GCP Cloud function 将数据从 Google 云存储发送到 SFTP 服务器吗? - Can we send data from Google cloud storage to SFTP server using GCP Cloud function? 获取禁止:403 访问被拒绝,当请求使用 python 将数据从谷歌云存储传输到 bigquery 时 - get Forbidden: 403 Access Denied when do request to transfer data from google cloud storage to bigquery using python 使用Python从Google云端存储下载多个文件 - Download multiple file from Google cloud storage using Python 使用Python从Google Cloud Storage下载大文件 - Download large file from Google Cloud Storage using Python 使用Python将文件从Google云端存储上传到Bigquery - Uploading a file from Google Cloud Storage to Bigquery using Python 如何从谷歌云存储在 python 上加载 .pickle 文件 - How to load a .pickle file on python from google cloud storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM