2016-09-07 90 views
0

當我從存儲平臺Web UI下載存儲在Google雲端存儲桶中的GZIP文件時,一切順利,我可以毫無問題地解壓縮文件。從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文件。 任何想法爲什麼?

回答

1

我的輸出文件更改爲二進制文件,它解決了這個問題:

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

代替:

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