【发布时间】:2019-03-19 04:33:09
【问题描述】:
这是我通常将 GCS 文件下载到本地的方式:
storage_client = storage.Client()
bucket = storage_client.get_bucket('mybucket')
blob = bucket.blob('myBigFile.txt')
blob.download_to_filename('myBigFile.txt)
我正在使用的文件比 Cloud Functions 的允许大小/内存大得多(例如,几 GB 到几 TB),因此上述方法不适用于这些大文件。
是否有更简单的“流式传输”(参见下面的示例 1)或“直接访问”(参见下面的示例 2)方式来处理 Cloud Function 中的 GCS 文件?
我想要做的两个例子是:
# 1. Load it in chunks of 5GB -- "Streaming"
storage_client = storage.Client()
bucket = storage_client.get_bucket('mybucket')
blob = bucket.blob('myBigFile.txt')
while True:
data = blob.download_to_filename('myBigFile.txt', chunk_size=5GB)
do_something(data)
if not data: break
或者:
# 2. Read the data from GCS without downloading it locally -- "Direct Access"
storage_client = storage.Client()
bucket = storage_client.get_bucket('mybucket')
blob = bucket.blob('myBigFile.txt')
with blob.read_filename('myBigFile.txt') as f:
do_something(f)
我不确定这两种方法是否可行,但我留下了一些关于它如何工作的选项。似乎支持Streaming Option,但我不确定如何将其应用于上述情况。
【问题讨论】:
-
您能否评论一下您计划如何在您的函数中使用包含在云存储桶对象中的数据?也许如果我们知道如何使用数据,我们可以提供更好的指导?
-
@Kolban 我刚刚用几个例子更新了这个问题。我将数据保存为新的文件类型(例如 avro)或将其插入数据库。
-
这可能是你想要的吗? stackoverflow.com/questions/50380237/…
标签: python google-cloud-platform google-cloud-storage google-cloud-functions