Skip to content

Commit 1d3120e

Browse files
authored
Add S3 credential parameters (#124)
1 parent c1a1074 commit 1d3120e

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

astrocut/asdf_cutouts.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,21 @@
1717
from astropy.modeling import models
1818

1919

20-
def _get_cloud_http(s3_uri: Union[str, S3Path], verbose: bool = False) -> str:
20+
def _get_cloud_http(s3_uri: Union[str, S3Path], key: str = None, secret: str = None,
21+
token: str = None, verbose: bool = False) -> str:
2122
"""
2223
Get the HTTP URI of a cloud resource from an S3 URI.
2324
2425
Parameters
2526
----------
2627
s3_uri : string | S3Path
2728
the S3 URI of the cloud resource
29+
key : string
30+
Default None. Access key ID for S3 file system.
31+
secret : string
32+
Default None. Secret access key for S3 file system.
33+
token : string
34+
Default None. Security token for S3 file system.
2835
verbose : bool
2936
Default False. If true intermediate information is printed.
3037
"""
@@ -38,7 +45,7 @@ def _get_cloud_http(s3_uri: Union[str, S3Path], verbose: bool = False) -> str:
3845
print(f'Attempting to access private S3 bucket: {s3_path.bucket}')
3946

4047
# create file system and get URL of file
41-
fs = s3fs.S3FileSystem(anon=is_anon)
48+
fs = s3fs.S3FileSystem(anon=is_anon, key=key, secret=secret, token=token)
4249
with fs.open(s3_uri, 'rb') as f:
4350
return f.url()
4451

@@ -252,8 +259,8 @@ def _write_asdf(cutout: astropy.nddata.Cutout2D, gwcsobj: gwcs.wcs.WCS, outfile:
252259

253260
def asdf_cut(input_file: Union[str, pathlib.Path, S3Path], ra: float, dec: float, cutout_size: int = 20,
254261
output_file: Union[str, pathlib.Path] = "example_roman_cutout.fits",
255-
write_file: bool = True, fill_value: Union[int, float] = np.nan,
256-
verbose: bool = False) -> astropy.nddata.Cutout2D:
262+
write_file: bool = True, fill_value: Union[int, float] = np.nan, key: str = None,
263+
secret: str = None, token: str = None, verbose: bool = False) -> astropy.nddata.Cutout2D:
257264
"""
258265
Takes a single ASDF input file (`input_file`) and generates a cutout of designated size `cutout_size`
259266
around the given coordinates (`coordinates`).
@@ -276,8 +283,17 @@ def asdf_cut(input_file: Union[str, pathlib.Path, S3Path], ra: float, dec: float
276283
Optional, default True. Flag to write the cutout to a file or not.
277284
fill_value: int | float
278285
Optional, default `np.nan`. The fill value for pixels outside the original image.
286+
key : string
287+
Default None. Access key ID for S3 file system. Only applicable if `input_file` is a
288+
cloud resource.
289+
secret : string
290+
Default None. Secret access key for S3 file system. Only applicable if `input_file` is a
291+
cloud resource.
292+
token : string
293+
Default None. Security token for S3 file system. Only applicable if `input_file` is a
294+
cloud resource.
279295
verbose : bool
280-
Default False. If true intermediate information is printed.
296+
Default False. If True, intermediate information is printed.
281297
282298
Returns
283299
-------
@@ -288,7 +304,7 @@ def asdf_cut(input_file: Union[str, pathlib.Path, S3Path], ra: float, dec: float
288304
# if file comes from AWS cloud bucket, get HTTP URL to open with asdf
289305
file = input_file
290306
if (isinstance(input_file, str) and input_file.startswith('s3://')) or isinstance(input_file, S3Path):
291-
file = _get_cloud_http(input_file, verbose)
307+
file = _get_cloud_http(input_file, key, secret, token, verbose)
292308

293309
# get the 2d image data
294310
with asdf.open(file) as f:

astrocut/tests/test_asdf_cut.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def test_get_cloud_http(mock_s3fs, mock_requests):
335335
s3_uri = "s3://test_bucket/test_file.asdf"
336336
http_uri = _get_cloud_http(s3_uri)
337337
assert http_uri == HTTP_URI
338-
mock_s3fs.assert_called_with(anon=True)
338+
mock_s3fs.assert_called_with(anon=True, key=None, secret=None, token=None)
339339
mock_fs.open.assert_called_once_with(s3_uri, 'rb')
340340
mock_file.url.assert_called_once()
341341

@@ -347,5 +347,5 @@ def test_get_cloud_http(mock_s3fs, mock_requests):
347347

348348
# test function with private bucket
349349
mock_resp.status_code = 403
350-
http_uri = _get_cloud_http(s3_uri)
351-
mock_s3fs.assert_called_with(anon=False)
350+
http_uri = _get_cloud_http(s3_uri, key="access")
351+
mock_s3fs.assert_called_with(anon=False, key="access", secret=None, token=None)

0 commit comments

Comments
 (0)