Skip to content

Commit

Permalink
Merge pull request nsidc#920 from itcarroll/fix-download-gzip
Browse files Browse the repository at this point in the history
download using chunk iteration rather than raw response
  • Loading branch information
itcarroll authored Jan 7, 2025
2 parents 76a4f59 + db0fa4e commit 91866ac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html)

## [Unreleased]

### Fixed

- `earthaccess.download` will let requests automatically decode compressed content
([#887](https://github.com/nsidc/earthaccess/issues/887))
([**@itcarroll**](https://github.com/itcarroll))

## [v0.12.0] - 2024-11-13

### Changed
Expand Down
12 changes: 4 additions & 8 deletions earthaccess/store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime
import logging
import shutil
import traceback
from functools import lru_cache
from itertools import chain
Expand Down Expand Up @@ -670,16 +669,13 @@ def _download_file(self, url: str, directory: Path) -> str:
if not path.exists():
try:
session = self.auth.get_session()
with session.get(
url,
stream=True,
allow_redirects=True,
) as r:
with session.get(url, stream=True, allow_redirects=True) as r:
r.raise_for_status()
with open(path, "wb") as f:
# This is to cap memory usage for large files at 1MB per write to disk per thread
# Cap memory usage for large files at 1MB per write to disk per thread
# https://docs.python-requests.org/en/latest/user/quickstart/#raw-response-content
shutil.copyfileobj(r.raw, f, length=1024 * 1024)
for chunk in r.iter_content(chunk_size=1024 * 1024):
f.write(chunk)
except Exception:
logger.exception(f"Error while downloading the file {local_filename}")
raise Exception
Expand Down

0 comments on commit 91866ac

Please sign in to comment.