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