Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fetch_wasabi_file() utility function. #141

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cosipy/util/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .data_fetching import fetch_wasabi_file
46 changes: 46 additions & 0 deletions cosipy/util/data_fetching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os
from awscli.clidriver import create_clidriver

def fetch_wasabi_file(file,
output = None,
override = False,
bucket = 'cosi-pipeline-public',
endpoint = 'https://s3.us-west-1.wasabisys.com',
access_key = 'GBAL6XATQZNRV3GFH9Y4',
secret_key = 'GToOczY5hGX3sketNO2fUwiq4DJoewzIgvTCHoOv'):
"""
Download a file from COSI's Wasabi acccount.

Parameters
----------
file : str
Full path to file in Wasabi
output : str, optional
Full path to the downloaded file in the local system. By default it will use
the current durectory and the same file name as the input file.
bucket : str, optional
Passed to aws --bucket option
endpoint : str, optional
Passed to aws --endpoint-url option
access_key : str, optional
AWS_ACCESS_KEY_ID
secret_key : str, optional
AWS_SECRET_ACCESS_KEY
"""

if output is None:
output = file.split('/')[-1]

if os.path.exists(output) and not override:
raise RuntimeError(f"File {output} already exists.")

cli = create_clidriver()

cli.session.set_credentials(access_key, secret_key)

cli.main(['s3api', 'get-object',
'--bucket', bucket,
'--key', file,
'--endpoint-url', endpoint,
output])

2 changes: 1 addition & 1 deletion docs/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ If you are instead interested in an overview on how to use cosipy, see out `tuto
threeml
ts_map
image_deconvolution
util



4 changes: 4 additions & 0 deletions docs/api/util.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Utilities
=========

.. autofunction:: cosipy.util.fetch_wasabi_file
3 changes: 2 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
'scoords',
'pandas',
'tqdm',
'scipy']
'scipy',
'awscli']

# There seems to be a conflict between unittest.mock (used by sphinx) and metaclasses
# The cosipy.threeml.custom_functions.Band_Eflux includes a metaclass from
Expand Down
8 changes: 8 additions & 0 deletions tests/util/test_data_fetching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from cosipy.util import fetch_wasabi_file

fetch_wasabi_file('test_file.txt', override = True)

f = open('test_file.txt')

assert f.read() == 'Small file used for testing purposes.\n'

Loading