问题
Reading of a file downloaded from Google Cloud Storage fails in a python + flask + gunicorn + nginx + Compute Engine app. Link to the code: https://github.com/samuq/CE-test . The line number 64 of the file 'ETL_SHP_READ_SQL_WRITE' returns nothing, although the file is valid and has data in it:
prj_blob.download_to_file(self.prj_file)
logger.log_text(self.prj_file)
line 64 --> euref_fin.ImportFromWkt(self.prj_file.read())).
回答1:
file.seek(0) helped to solve the problem; somehow I assume that after blob.download_to_file(file_name) the file reader isn't in the start of the file. Code:
try:
fd, path = tempfile.mkstemp()
with os.fdopen(fd, 'w+') as prj_file:
# do stuff with temp file
prj_blob.download_to_file(prj_file)
prj_file.seek(0)
euref_fin.ImportFromWkt(prj_file.read())
logger.log_text(str(euref_fin))
logger.log_text('euref_fin printed!')
finally:
os.remove(path)
来源:https://stackoverflow.com/questions/53631410/reading-of-a-file-from-google-cloud-storage-fails-in-a-python-flask-gunicorn