Common
Authentication functions implementation.
auth_validation(station_type, access_type, *args, **kwargs)
Function called by auth_validator
Source code in docs/rs-server/services/common/rs_server_common/authentication/authentication.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
|
auth_validator(station, access_type)
Decorator to validate API key access or oauth2 authentication (keycloak) for a specific station and access type.
This decorator checks if the authorization contains the necessary role to access the specified station with the specified access type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
station
|
str
|
The name of the station, either "adgs" or "cadip". |
required |
access_type
|
str
|
The type of access, such as "download" or "read". |
required |
Raises:
Type | Description |
---|---|
HTTPException
|
If the authorization does not include the required role to access the specified station with the specified access type. |
Returns:
Name | Type | Description |
---|---|---|
function |
Callable
|
The decorator function. |
Source code in docs/rs-server/services/common/rs_server_common/authentication/authentication.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|
authenticate(request, apikey_value='')
async
FastAPI Security dependency for the cluster mode. Check the api key validity, passed as an HTTP header, or that the user is authenticated with oauth2 (keycloak).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
apikey_value
|
Security
|
API key passed in HTTP header |
''
|
Returns:
Type | Description |
---|---|
AuthInfo
|
Tuple of (IAM roles, config, user login) information from the keycloak account, associated to the api key |
AuthInfo
|
or the user oauth2 account. |
Source code in docs/rs-server/services/common/rs_server_common/authentication/authentication.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
authenticate_from_pytest(auth_info)
'authenticate' function called from pytest.
Source code in docs/rs-server/services/common/rs_server_common/authentication/authentication.py
41 42 43 |
|
get_issuer_and_public_key()
async
Get issuer URL from OIDC environment, and public key from the issuer.
Source code in docs/rs-server/services/common/rs_server_common/authentication/authentication.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
Database connection.
Taken from: https://praciano.com.br/fastapi-and-async-sqlalchemy-20-with-pytest-done-right.html
DatabaseSessionManager
Database session configuration.
Source code in docs/rs-server/services/common/rs_server_common/db/database.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
__filelock(func)
staticmethod
Avoid concurrent writing to the database using a file locK.
Source code in docs/rs-server/services/common/rs_server_common/db/database.py
138 139 140 141 |
|
__init__()
Create a Database session configuration.
Source code in docs/rs-server/services/common/rs_server_common/db/database.py
47 48 49 50 |
|
close()
Close database session.
Source code in docs/rs-server/services/common/rs_server_common/db/database.py
95 96 97 98 99 100 |
|
connect()
Open new database connection instance.
Source code in docs/rs-server/services/common/rs_server_common/db/database.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
create_all()
Create all database tables.
Source code in docs/rs-server/services/common/rs_server_common/db/database.py
143 144 145 146 147 |
|
drop_all()
Drop all database tables.
Source code in docs/rs-server/services/common/rs_server_common/db/database.py
149 150 151 152 153 |
|
open_session(url='')
Open database session.
Source code in docs/rs-server/services/common/rs_server_common/db/database.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
reraise_http_exception(exception)
classmethod
Re-raise all exceptions into HTTP exceptions.
Source code in docs/rs-server/services/common/rs_server_common/db/database.py
155 156 157 158 159 160 161 162 163 164 |
|
session()
Open new database session instance.
Source code in docs/rs-server/services/common/rs_server_common/db/database.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
url()
classmethod
Get database connection URL.
Source code in docs/rs-server/services/common/rs_server_common/db/database.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
get_db()
Return a database session for FastAPI dependency injection.
Source code in docs/rs-server/services/common/rs_server_common/db/database.py
170 171 172 173 174 175 176 177 178 |
|
Logging utility.
CustomFormatter
Bases: Formatter
Custom logging formatter with colored text. See: https://stackoverflow.com/a/56944256
Source code in docs/rs-server/services/common/rs_server_common/utils/logging.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
Logging
Logging utility.
Attributes:
Name | Type | Description |
---|---|---|
lock |
For code synchronization |
|
level |
Minimal log level to use for all new logging instances. |
Source code in docs/rs-server/services/common/rs_server_common/utils/logging.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
default(name='rspy')
classmethod
Return a default Logger class instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Logger name. You can pass name to use your current module name. |
'rspy'
|
Source code in docs/rs-server/services/common/rs_server_common/utils/logging.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
OpenTelemetry utility
init_traces(app, service_name)
Init instrumentation of OpenTelemetry traces.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
app
|
FastAPI
|
FastAPI application |
required |
service_name
|
str
|
service name |
required |
Source code in docs/rs-server/services/common/rs_server_common/utils/opentelemetry.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
request_hook(span, request)
HTTP requests intrumentation
Source code in docs/rs-server/services/common/rs_server_common/utils/opentelemetry.py
42 43 44 45 46 47 |
|
response_hook(span, request, response)
HTTP responses intrumentation
Source code in docs/rs-server/services/common/rs_server_common/utils/opentelemetry.py
50 51 52 53 54 55 |
|
This module is used to share common functions between apis endpoints
check_and_fix_timerange(item)
This function ensures the item does not have a single timerange value
Source code in docs/rs-server/services/common/rs_server_common/utils/utils.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
extract_eo_product(eo_product, mapper)
This function is creating key:value pairs from an EOProduct properties
Source code in docs/rs-server/services/common/rs_server_common/utils/utils.py
213 214 215 216 217 218 |
|
is_valid_date_format(date)
Check if a string adheres to the expected date format "YYYY-MM-DDTHH:MM:SS[.sss]Z".
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date
|
str
|
The string to be validated for the specified date format. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the input string adheres to the expected date format, otherwise False. |
Source code in docs/rs-server/services/common/rs_server_common/utils/utils.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
odata_to_stac(feature_template, odata_dict, odata_stac_mapper)
Maps OData values to a given STAC template.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature_template
|
dict
|
The STAC feature template to be populated. |
required |
odata_dict
|
dict
|
The dictionary containing OData values. |
required |
odata_stac_mapper
|
dict
|
The mapping dictionary for converting OData keys to STAC properties. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The populated STAC feature template. |
Raises:
Type | Description |
---|---|
ValueError
|
If the provided STAC feature template is invalid. |
Source code in docs/rs-server/services/common/rs_server_common/utils/utils.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
|
validate_inputs_format(date_time, raise_errors=True)
Validate the format and content of a time interval string.
This function checks whether the provided time interval string is in a valid format and whether the start and stop dates conform to the ISO 8601 standard. It supports a variety of interval formats, including open-ended intervals.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date_time
|
str
|
The time interval string to validate. Supported formats include: - "2024-01-01T00:00:00Z/2024-01-02T23:59:59Z" (closed interval) - "../2024-01-02T23:59:59Z" (open start interval) - "2024-01-01T00:00:00Z/.." (open end interval) - "2024-01-01T00:00:00Z" (fixed date) |
required |
raise_errors
|
bool
|
If True, raises an exception for invalid input. If False, returns [None, None, None] for invalid input. |
True
|
Returns:
Type | Description |
---|---|
Any
|
List[Union[datetime, None]]: A list containing three elements: - fixed_date (datetime or None): The single fixed date if applicable. - start_date (datetime or None): The start date of the interval. - stop_date (datetime or None): The stop date of the interval. Returns [None, None, None] if the input is invalid or empty. |
Raises:
Type | Description |
---|---|
HTTPException
|
If |
Note
- The input interval should use the ISO 8601 format for dates and times.
- If using an open-ended interval, one side of the interval can be omitted (e.g., "../2024-01-02T23:59:59Z").
Source code in docs/rs-server/services/common/rs_server_common/utils/utils.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
validate_sort_input(sortby)
Used to transform stac sort parameter to odata type. -datetime = startTimeFromAscendingNode DESC.
Source code in docs/rs-server/services/common/rs_server_common/utils/utils.py
221 222 223 224 225 226 |
|
validate_str_list(parameter)
Validates and parses a parameter that can be either a string or a comma-separated list of strings.
The function processes the input parameter to: - Strip whitespace from each item in a comma-separated list. - Return a single string if the list has only one item. - Return a list of strings if the input contains multiple valid items.
Examples:
-
Input: 'S1A' Output: 'S1A' (str)
-
Input: 'S1A, S2B' Output: ['S1A', 'S2B'] (list of str)
# Test case bgfx, when input contains ',' but not a validd value, output should not be ['S1A', ''] - Input: 'S1A,' Output: 'S1A' (str)
- Input: 'S1A, S2B, ' Output: ['S1A', 'S2B'] (list of str)
Source code in docs/rs-server/services/common/rs_server_common/utils/utils.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
EODAG Provider.
CustomEODataAccessGateway
Bases: EODataAccessGateway
EODataAccessGateway with a custom config directory management.
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/eodag_provider.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
__del__()
Destructor
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/eodag_provider.py
66 67 68 69 70 71 |
|
__init__(*args, **kwargs)
Constructor
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/eodag_provider.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
create(*args, **kwargs)
cached
classmethod
Return a cached instance of the class.
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/eodag_provider.py
73 74 75 76 77 |
|
override_config_from_env()
Update the eodag conf from the latest EODAG__
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/eodag_provider.py
79 80 81 82 83 84 85 86 87 |
|
EodagProvider
Bases: Provider
An EODAG provider.
It uses EODAG to provide data from external sources.
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/eodag_provider.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
__init__(config_file, provider)
Create a EODAG provider.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_file
|
Path
|
the path to the eodag configuration file |
required |
provider
|
str
|
the name of the eodag provider |
required |
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/eodag_provider.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
create_eodag_product(product_id, filename)
Initialize an EO product with minimal properties.
The title is used by EODAG as the name of the downloaded file. The download link is used by EODAG as http request url for download. The geometry is mandatory in an EO Product so we add the all earth as geometry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
product_id
|
str
|
the id of EO Product |
required |
filename
|
str
|
the name of the downloaded file |
required |
Returns:
Name | Type | Description |
---|---|---|
product |
EOProduct
|
the initialized EO Product |
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/eodag_provider.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
download(product_id, to_file)
Download the expected product at the given local location.
EODAG needs an EOProduct to download. We build an EOProduct from the id and download location to be able to call EODAG for download.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
product_id
|
str
|
the id of the product to download |
required |
to_file
|
Path
|
the path where the product has to be download |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/eodag_provider.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|
Provider mechanism.
CreateProviderFailed
Bases: Exception
Exception raised when an error occurred during the init of a provider.
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/provider.py
53 54 |
|
DownloadProductFailed
Bases: Exception
Exception raised when an error occurred during the download.
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/provider.py
61 62 |
|
Product
dataclass
A product.
A product has an external identifier and a dictionary of metadata.
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/provider.py
42 43 44 45 46 47 48 49 50 |
|
Provider
Bases: ABC
A product provider.
A provider gives a common interface to search for files from an external data source and download them locally.
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/provider.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
download(product_id, to_file)
abstractmethod
Download the given product to the given local path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
product_id
|
str
|
id of the product to download |
required |
to_file
|
Path
|
path where the file should be downloaded |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/provider.py
101 102 103 104 105 106 107 108 109 110 111 112 |
|
search(**kwargs)
Search for products with the given time range.
The search result is a dictionary of products found indexed by id.
Returns:
Type | Description |
---|---|
Any
|
The files found indexed by file id. Specific to each provider. |
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/provider.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
SearchProductFailed
Bases: Exception
Exception raised when an error occurred during the search.
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/provider.py
57 58 |
|
TimeRange
dataclass
A time range.
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/provider.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
duration()
Duration of the timerange.
Returns: duration of the timerange
Source code in docs/rs-server/services/common/rs_server_common/data_retrieval/provider.py
31 32 33 34 35 36 |
|
TODO Docstring to be added.
CustomSessionRedirect
Bases: Session
Custom session to handle HTTP 307 redirects and retain Authorization headers for allowed hosts.
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
|
__init__(trusted_domains=list[str] | None)
Initialize the CustomSession instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trusted_domains
|
list
|
List of allowed hosts for redirection in case of change of protocol (HTTP <> HTTPS). |
list[str] | None
|
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
114 115 116 117 118 119 120 121 122 |
|
should_strip_auth(old_url, new_url)
Override the default behavior of stripping Authorization headers during redirection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
old_url
|
str
|
The URL of the original request. |
required |
new_url
|
str
|
The URL to which the request is redirected. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
Whether to strip Authorization headers (False to retain them). |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
|
GetKeysFromS3Config
dataclass
S3 configuration for download
Attributes:
Name | Type | Description |
---|---|---|
s3_files |
list
|
A list with the S3 object keys to be downloaded. |
bucket |
str
|
The S3 bucket name. |
local_prefix |
str
|
The local prefix where files will be downloaded. |
overwrite |
bool
|
Flag indicating whether to overwrite existing files. Default is False. |
max_retries |
int
|
The maximum number of download retries. Default is DWN_S3FILE_RETRIES. |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
PutFilesToS3Config
dataclass
Configuration for uploading files to S3.
Attributes:
Name | Type | Description |
---|---|---|
files |
List
|
A list with the local file paths to be uploaded. |
bucket |
str
|
The S3 bucket name. |
s3_path |
str
|
The S3 path where files will be uploaded. |
max_retries |
int
|
The maximum number of upload retries. Default is UP_S3FILE_RETRIES. |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
S3StorageHandler
Interacts with an S3 storage
S3StorageHandler for interacting with an S3 storage service.
Attributes:
Name | Type | Description |
---|---|---|
access_key_id |
str
|
The access key ID for S3 authentication. |
secret_access_key |
str
|
The secret access key for S3 authentication. |
endpoint_url |
str
|
The endpoint URL for the S3 service. |
region_name |
str
|
The region name. |
s3_client |
client
|
The s3 client to interact with the s3 storage |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 |
|
__get_s3_client(access_key_id, secret_access_key, endpoint_url, region_name)
Retrieve or create an S3 client instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
access_key_id
|
str
|
The access key ID for S3 authentication. |
required |
secret_access_key
|
str
|
The secret access key for S3 authentication. |
required |
endpoint_url
|
str
|
The endpoint URL for the S3 service. |
required |
region_name
|
str
|
The region name. |
required |
Returns:
Name | Type | Description |
---|---|---|
client |
boto3
|
An S3 client instance. |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
__init__(access_key_id, secret_access_key, endpoint_url, region_name)
Initialize the S3StorageHandler instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
access_key_id
|
str
|
The access key ID for S3 authentication. |
required |
secret_access_key
|
str
|
The secret access key for S3 authentication. |
required |
endpoint_url
|
str
|
The endpoint URL for the S3 service. |
required |
region_name
|
str
|
The region name. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the connection to the S3 storage cannot be established. |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
check_bucket_access(bucket)
Check the accessibility of an S3 bucket.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bucket
|
str
|
The S3 bucket name. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If an error occurs during the bucket access check. |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
|
check_file_overwriting(local_file, overwrite)
Check if file exists and determine if it should be overwritten.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
local_file
|
str
|
Path to the local file. |
required |
overwrite
|
bool
|
Whether to overwrite the existing file. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the file should be overwritten, False otherwise. |
Note: - If the file already exists and the overwrite flag is set to True, the function logs a message, deletes the existing file, and returns True. - If the file already exists and the overwrite flag is set to False, the function logs a warning message, and returns False. In this case, the existing file won't be deleted. - If the file doesn't exist, the function returns True.
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
|
check_s3_key_on_bucket(bucket, s3_key)
Check if the s3 key available in the bucket.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bucket
|
str
|
The S3 bucket name. |
required |
s3_key
|
str
|
The s3 key that should be checked |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If an error occurs during the bucket access check or if the s3_key is not available. |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
|
connect_s3()
Establish a connection to the S3 service.
If the S3 client is not already instantiated, this method calls the private get_s3_client method to create an S3 client instance using the provided credentials and configuration (see __init).
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
222 223 224 225 226 227 228 229 230 231 232 233 234 |
|
delete_file_from_s3(bucket, key, max_retries=S3_MAX_RETRIES)
Delete a file from S3. The functionality implies a retry mechanism at the application level, which is different than the retry mechanism from the s3 protocol level, with "retries" parameter from the s3 Config
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bucket
|
str
|
The S3 bucket name. |
required |
key
|
str
|
The S3 object key. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If an error occurs during the bucket access check. |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
|
disconnect_s3()
Close the connection to the S3 service.
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
236 237 238 239 240 241 |
|
files_to_be_downloaded(bucket, paths)
Create a list with the S3 keys to be downloaded.
The list will have the s3 keys to be downloaded from the bucket. It contains pairs (local_prefix_where_the_file_will_be_downloaded, full_s3_key_path) If a s3 key doesn't exist, the pair will be (None, requested_s3_key_path)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bucket
|
str
|
The S3 bucket name. |
required |
paths
|
list
|
List of S3 object keys. |
required |
Returns:
Name | Type | Description |
---|---|---|
list_with_files |
list
|
List of tuples (local_prefix, full_s3_key_path). |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
|
files_to_be_uploaded(paths)
Creates a list with the local files to be uploaded.
The list contains pairs (s3_path, absolute_local_file_path) If the local file doesn't exist, the pair will be (None, requested_file_to_upload)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
paths
|
list
|
List of local file paths. |
required |
Returns:
Name | Type | Description |
---|---|---|
list_with_files |
list
|
List of tuples (s3_path, absolute_local_file_path). |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
|
get_basename(input_path)
staticmethod
Get the filename from a full path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_path
|
str
|
The full path. |
required |
Returns:
Name | Type | Description |
---|---|---|
filename |
str
|
The filename. |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
319 320 321 322 323 324 325 326 327 328 329 330 |
|
get_keys_from_s3(config)
Download S3 keys specified in the configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
GetKeysFromS3Config
|
Configuration for the S3 download. |
required |
Returns:
Type | Description |
---|---|
list
|
List[str]: A list with the S3 keys that couldn't be downloaded. |
Raises:
Type | Description |
---|---|
Exception
|
Any unexpected exception raised during the download process. |
The function attempts to download files from S3 according to the provided configuration. It returns a list of S3 keys that couldn't be downloaded successfully.
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 |
|
get_secrets_from_file(secrets, secret_file)
staticmethod
Read secrets from a specified file.
It reads the secrets from .s3cfg or aws credentials files This function should not be used in production
Parameters:
Name | Type | Description | Default |
---|---|---|---|
secrets
|
dict
|
Dictionary to store retrieved secrets. |
required |
secret_file
|
str
|
Path to the file containing secrets. |
required |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
list_s3_files_obj(bucket, prefix)
Retrieve the content of an S3 directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bucket
|
str
|
The S3 bucket name. |
required |
prefix
|
str
|
The S3 object key prefix. |
required |
Returns:
Name | Type | Description |
---|---|---|
s3_files |
list
|
List containing S3 object keys. |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
|
put_files_to_s3(config)
Upload files to S3 according to the provided configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
PutFilesToS3Config
|
Configuration for the S3 upload. |
required |
Returns:
Type | Description |
---|---|
list
|
List[str]: A list with the local file paths that couldn't be uploaded. |
Raises:
Type | Description |
---|---|
Exception
|
Any unexpected exception raised during the upload process. |
The function attempts to upload files to S3 according to the provided configuration. It returns a list of local files that couldn't be uploaded successfully.
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 |
|
s3_path_parser(s3_url)
staticmethod
Parses S3 URL to extract bucket, prefix, and file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s3_url
|
str
|
The S3 URL. |
required |
Returns:
Type | Description |
---|---|
bucket, prefix, s3_file) (tuple
|
Tuple containing bucket, prefix, and file. |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
|
s3_streaming_upload(stream_url, trusted_domains, auth, bucket, key, max_retries=S3_MAX_RETRIES)
Upload a file to an S3 bucket using HTTP byte-streaming with retries.
This method retrieves data from stream_url
in chunks and uploads it to the specified S3 bucket
(bucket
) under the specified key (key
). It includes retry logic for network and S3 client errors,
with exponential backoff between retries. The method handles errors during both the HTTP request and the
S3 upload process, raising a RuntimeError
if the retries are exhausted without success.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stream_url
|
str
|
The URL of the file to be streamed and uploaded. |
required |
trusted_domains
|
list
|
List of allowed hosts for redirection in case of change of protocol (HTTP <> HTTPS). |
required |
auth
|
Any
|
Authentication credentials for the HTTP request (if required). |
required |
bucket
|
str
|
The name of the target S3 bucket. |
required |
key
|
str
|
The S3 object key (file path) to store the streamed file. |
required |
max_retries
|
int
|
The maximum number of retry attempts if an error occurs
(default is |
S3_MAX_RETRIES
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If there is a failure during the streaming upload process, either due to the HTTP request or the S3 upload, after exhausting all retries. |
Process
- The function attempts to download the file from
stream_url
using streaming and upload it to S3. - It redirects the url request by overriding the default should_strip_auth, see CustomSessionRedirect
- If an error occurs (e.g., connection error, S3 client error), it retries the operation with exponential backoff.
- The default chunk size for streaming is set to 64KB, and multipart upload configuration is used for large files.
- After
max_retries
attempts, if the upload is unsuccessful, aRuntimeError
is raised.
Retry Mechanism
- Retries occur for network-related errors (
RequestException
) or S3 client errors (ClientError
,BotoCoreError
). - The function waits before retrying, with the delay time increasing exponentially
(based on the
backoff_factor
). - The backoff formula is
backoff_factor * (2 ** (attempt - 1))
, allowing progressively longer wait times between retries.
Exception Handling
- HTTP errors such as timeouts or bad responses (4xx, 5xx) are handled using
requests.exceptions.RequestException
. - S3 client errors such as
ClientError
andBotoCoreError
are captured, logged, and retried. - Any other unexpected errors are caught and re-raised as
RuntimeError
.
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 |
|
transfer_from_s3_to_s3(config)
Copy S3 keys specified in the configuration. Args: config (TransferFromS3ToS3Config): Configuration object containing bucket source, bucket destination, S3 files, maximum retries.
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
A list of S3 keys that failed to be copied. |
Raises:
Type | Description |
---|---|
Exception
|
Any unexpected exception raised during the upload process. |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 |
|
wait_timeout(timeout)
Wait for a specified timeout duration (minimum 200 ms).
This function implements a simple timeout mechanism, where it sleeps for 0.2 seconds in each iteration until the cumulative sleep time reaches the specified timeout duration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
float
|
The total duration to wait in seconds. |
required |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 |
|
TransferFromS3ToS3Config
dataclass
S3 configuration for copying a list with keys between buckets
Attributes:
Name | Type | Description |
---|---|---|
s3_files |
list
|
A list with the S3 object keys to be copied. |
bucket_src |
str
|
The source S3 bucket name. |
bucket_dst |
str
|
The destination S3 bucket name. |
max_retries |
int
|
The maximum number of download retries. Default is DWN_S3FILE_RETRIES. |
Source code in docs/rs-server/services/common/rs_server_common/s3_storage_handler/s3_storage_handler.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|