14
14
from astropy .utils .console import ProgressBarOrSpinner
15
15
from astropy .utils .exceptions import AstropyDeprecationWarning
16
16
17
- from ..exceptions import NoResultsWarning , InvalidQueryError
17
+ from ..exceptions import NoResultsWarning
18
18
19
19
from . import utils
20
20
@@ -109,32 +109,14 @@ def get_cloud_uri(self, data_product, include_bucket=True, full_url=False):
109
109
found in the cloud, None is returned.
110
110
"""
111
111
112
- s3_client = self .boto3 .client ('s3' , config = self .config )
113
-
114
- path = utils .mast_relative_path (data_product ["dataURI" ])
115
- if path is None :
116
- raise InvalidQueryError ("Malformed data uri {}" .format (data_product ['dataURI' ]))
112
+ uri_list = self .get_cloud_uri_list (data_product , include_bucket = include_bucket , full_url = full_url )
117
113
118
- if 'galex' in path :
119
- path = path .lstrip ("/mast/" )
120
- elif '/ps1/' in path :
121
- path = path .replace ("/ps1/" , "panstarrs/ps1/public/" )
114
+ # Making sure we got at least 1 URI from the query above.
115
+ if uri_list [0 ] is None :
116
+ warnings .warn ("Unable to locate file {}." .format (data_product ), NoResultsWarning )
122
117
else :
123
- path = path .lstrip ("/" )
124
-
125
- try :
126
- s3_client .head_object (Bucket = self .pubdata_bucket , Key = path )
127
- if include_bucket :
128
- path = "s3://{}/{}" .format (self .pubdata_bucket , path )
129
- elif full_url :
130
- path = "http://s3.amazonaws.com/{}/{}" .format (self .pubdata_bucket , path )
131
- return path
132
- except self .botocore .exceptions .ClientError as e :
133
- if e .response ['Error' ]['Code' ] != "404" :
134
- raise
135
-
136
- warnings .warn ("Unable to locate file {}." .format (data_product ['productFilename' ]), NoResultsWarning )
137
- return None
118
+ # Output from ``get_cloud_uri_list`` is always a list even when it's only 1 URI
119
+ return uri_list [0 ]
138
120
139
121
def get_cloud_uri_list (self , data_products , include_bucket = True , full_url = False ):
140
122
"""
@@ -158,8 +140,33 @@ def get_cloud_uri_list(self, data_products, include_bucket=True, full_url=False)
158
140
List of URIs generated from the data products, list way contain entries that are None
159
141
if data_products includes products not found in the cloud.
160
142
"""
143
+ s3_client = self .boto3 .client ('s3' , config = self .config )
161
144
162
- return [self .get_cloud_uri (product , include_bucket , full_url ) for product in data_products ]
145
+ paths = utils .mast_relative_path (data_products ["dataURI" ])
146
+ if isinstance (paths , str ): # Handle the case where only one product was requested
147
+ paths = [paths ]
148
+
149
+ uri_list = []
150
+ for path in paths :
151
+ if path is None :
152
+ uri_list .append (None )
153
+ else :
154
+ try :
155
+ # Use `head_object` to verify that the product is available on S3 (not all products are)
156
+ s3_client .head_object (Bucket = self .pubdata_bucket , Key = path )
157
+ if include_bucket :
158
+ s3_path = "s3://{}/{}" .format (self .pubdata_bucket , path )
159
+ uri_list .append (s3_path )
160
+ elif full_url :
161
+ path = "http://s3.amazonaws.com/{}/{}" .format (self ._pubdata_bucket , path )
162
+ uri_list .append (path )
163
+ except self .botocore .exceptions .ClientError as e :
164
+ if e .response ['Error' ]['Code' ] != "404" :
165
+ raise
166
+ warnings .warn ("Unable to locate file {}." .format (path ), NoResultsWarning )
167
+ uri_list .append (None )
168
+
169
+ return uri_list
163
170
164
171
def download_file (self , data_product , local_path , cache = True , verbose = True ):
165
172
"""
0 commit comments