简体   繁体   English

如何直接从python中的谷歌存储向客户端发送文件?

[英]How to directly send file to client from google storage in python?

I want to send any file to client on request, from google storage , but its downloading locally on server.我想根据请求将任何文件从 google storage 发送到客户端,但它在服务器上本地下载。 I don't to download locally rather if any way I can send file to client directly.我不会在本地下载,而是以任何方式直接将文件发送到客户端。

Currently I am doing this way for download目前我正在这样做下载

def download_file(self, bucket, key, path_to_download):
        bucket = gc_storage.storage_client.bucket(bucket)
        blob = bucket.blob(key)
        blob.download_to_filename(path_to_download)

I don't think there is an API method to load data from GCS to a generic third location, although some data transfer options exist for certain specific use cases.我不认为有一种 API 方法可以将数据从 GCS 加载到通用的第三个位置,尽管某些特定用例存在一些数据传输选项。

As mentioned in the comments, smart-open could be an option here, provided you're willing to at least stream the data through your server.正如评论中提到的, smart-open可能是这里的一个选项,前提是您愿意至少通过您的服务器流式传输数据。 Perhaps something like this:也许是这样的:

from dotenv import load_dotenv
from google.cloud.storage import Client
from os import getenv
from smart_open import open


# load environment variables from a file
load_dotenv("<path/to/.env")

# get the path to a service account credentials file from an environment variable
service_account_path = getenv("GOOGLE_APPLICATION_CREDENTIALS")

# create a client using the service account credentials for authentication
gcs_client = Client.from_service_account_json(service_account_path)

# use this client to authenticate your transfer
transport = {"client": gcs_client}


with open("gs://my_bucket/my_file.txt", transport_params=transport) as f_in:
    with open("gs://other_bucket/my_file.txt", "wb", transport_params=transport) as f_out:
        for line in f_in:
            f_out.write(line)

Here I've written out the full machinery for doing this with a service account, on the assumption that you aren't authenticated by default.在这里,我已经写出了使用服务帐户执行此操作的完整机制,前提是您默认情况下未通过身份验证。 My understanding is that you may be able to remove most of that if your computer is already set up to connect to GCS with some default credentials:我的理解是,如果您的计算机已设置为使用某些默认凭据连接到 GCS,则您可以删除其中的大部分内容:

from smart_open import open

with open("gs://my_bucket/my_file.txt") as f_in:
    with open("gs://other_bucket/my_file.txt", "wb") as f_out:
        for line in f_in:
            f_out.write(line)

Note also that I've written this as if you were transferring the file from one GCS bucket to another one - but this is actually one of those cases where there is a built-in API method to achieve the goal!另请注意,我已经将文件从一个 GCS 存储桶传输到另一个存储桶 - 但这实际上使用内置 API 方法来实现目标的情况之一!

# ... [obtain gcs_client as before] ...

my_bucket = gcs_client.get_bucket("my-bucket")
other_bucket = gcs_client.get_bucket("other-bucket")

my_file = my_bucket.get_blob("my-file.txt")

my_bucket.copy_blob(my_file, other_bucket)

It sounds like what you actually want to do is pass on the data to a third party, so the inner with statement will need replacing with whatever implementation you are actually using.听起来您真正想要做的是将数据传递给第三方,因此内部with语句将需要替换为您实际使用的任何实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GCS - 从 Google Cloud Storage 直接读取文本文件到 python - GCS - Read a text file from Google Cloud Storage directly into python 无法从python存储Google客户端上传1GB以上的文件 - Unable to upload a 1GB+ file from python storage google client 使用Python将Twitter数据直接发送到Google Cloud数据存储 - Using Python to send twitter data directly to Google Cloud data storage 如何使用python客户端打开XML文件用于Google云存储 - How to open an XML file with python client for google cloud storage Google Cloud Storage gcsfs - 将 a.tar 文件直接读入 python - Google Cloud Storage gcsfs - read a .tar file directly into python 如何将字符串从Google App Engine的python脚本发送到浏览器客户端作为文件 - How to send a string from a python script at Google App Engine to the browser client as a file 如何从谷歌云存储在 python 上加载 .pickle 文件 - How to load a .pickle file on python from google cloud storage 如何从谷歌云存储中下载 Python 或 Linux 中的文件? - How to download a file in Python or Linux from Google Cloud storage? 如何使用 python 从 Google firebase 存储中删除图像文件 - How to delete a image file from Google firebase Storage using python 将 Avro 文件直接创建到 Google Cloud Storage - Create Avro file directly to Google Cloud Storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM