【问题标题】:Corrupted GZIP file downloaded from Google Cloud Storage从 Google Cloud Storage 下载的损坏的 GZIP 文件
【发布时间】:2016-09-07 02:05:57
【问题描述】:

当我从 Storage Platform Web UI 下载存储在 Google Cloud Storage 存储桶中的 GZIP 文件时,一切正常,我可以毫无问题地解压缩文件。

但是,当我使用 googleapiclient 和 Python 下载文件时,我无法解压缩它。 7-Zip 表示文件已损坏。 我的代码:

import io
from apiclient.http import MediaIoBaseDownload
from googleapiclient import http
bucket='bqtoredshiftdaily'

out_file=os.path.join(current_dir,process_name,"Upload",gcsfile.replace("/", "_"))

with open(out_file, 'w') as f:

    req = gcs_service.objects().get_media(bucket=bucket, object=gcsfile)

    downloader = http.MediaIoBaseDownload(f, req)

    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download {}%.".format(int(status.progress() * 100)))

下载成功,但正如我所说,我无法解压缩下载的 GZIP 文件。 知道为什么吗?

【问题讨论】:

    标签: python gzip google-cloud-storage


    【解决方案1】:

    我将输出文件更改为二进制并解决了:

    with open(out_file, 'wb') as f:
    

    代替:

    with open(out_file, 'w') as f:
    

    【讨论】:

      猜你喜欢
      • 2017-11-08
      • 2013-08-15
      • 1970-01-01
      • 2020-09-30
      • 2013-08-19
      • 2021-04-17
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多