【问题标题】:Handling big files with Google Cloud Storage API使用 Google Cloud Storage API 处理大文件
【发布时间】:2015-10-15 22:02:58
【问题描述】:

我需要实现的是使用cloudstorage 库将文件列表连接到一个文件中。这需要在 mapreduce 分片内进行,该分片的内存上限为 512MB,但连接的文件可能大于 512MB。

当文件大小达到内存限制时,以下代码段会中断。

list_of_files = [...]
with cloudstorage.open(filename...) as file_handler:
    for a in list_of_files:
        with cloudstorage.open(a) as f:
            file_handler.write(f.read())

有没有办法解决这个问题?也许以块的形式打开或附加文件?以及如何做到这一点?谢谢!

== 编辑 ==

经过一些测试,内存限制似乎只适用于f.read(),而写入大文件是可以的。分块读取文件解决了我的问题,但我真的很喜欢@Ian-Lewis 指出的compose() 函数。谢谢!

【问题讨论】:

    标签: python file append google-cloud-storage cloud-storage


    【解决方案1】:

    对于大文件,您需要将文件拆分为较小的文件,上传每个文件,然后将它们合并为composite objects。您将需要使用库中的compose() function。好像有no docs on it yet

    在您上传所有部分后,如下所示应该可以工作。要确保的一件事是要组合的路径文件不包含存储桶名称或开头的斜杠。

    stat = cloudstorage.compose(
        [
            "path/to/part1",
            "path/to/part2",
            "path/to/part3",
            # ...
        ],
        "/my_bucket/path/to/output"
    )
    

    如果可能,您可能还想使用 gsutil 工具进行检查。它可以为你做automatic splitting, uploading in parallel, and compositing of large files

    【讨论】:

      猜你喜欢
      • 2014-10-12
      • 2013-08-19
      • 1970-01-01
      • 2020-06-13
      • 2020-04-04
      • 1970-01-01
      • 2018-04-22
      • 2015-08-01
      • 1970-01-01
      相关资源
      最近更新 更多