我正在尝试使用python从Amazon S3下载一些文件并将其解压缩到本地磁盘。
以下是我用来下载和解压缩文件的代码。
def download(self):
filename = BASE_OUTPUT_DIR + "/" + str(self.jobId) + "_" + string.replace(self.jobName, " ", "_") + "_" + str(self.count)
try:
response = requests.get(self.fileUrl.rstrip(), stream=True, verify=False)
if response.status_code == 200:
with open(filename + ".gz", 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
subprocess.call(["gunzip", "--name", filename])
return filename
except Exception as e:
self.logger.debug("An exception occurred while downloading file (%s)" % (filename))
self.logger.debug("Trying to delete the file")
try:
os.system('rm -rf %s' % filename)
except Exception as e:
self.logger.debug(e)
return None
有时我收到此错误gzip: /tmp/files/file_8340.gz: unexpected end of file
。
我怀疑文件,但这个问题随机文件随机发生。我认为下载没有完成,但我不确定。