S3Client class¶
Handles operations using s3 client and resource from boto3.
This class provides attributes and methods that can improve the way on how users operate with S3 in AWS. In essence, it wraps some boto3 methods to build some useful features that makes it easy to extract information and manage S3 buckets and objects.
Examples:
# Importing the class
from cloudgeass.aws.s3 import S3Client
# Setting up an object and getting the list of buckets with an account
s3 = S3Client()
buckets = s3.list_buckets()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logger_level |
int
|
The logger level to be configured on the class logger object |
logging.INFO
|
Attributes:
Name | Type | Description |
---|---|---|
logger |
logging.Logger
|
A logger object to log steps according to a predefined logger level |
client |
botocore.client.S3
|
A S3 boto3 client to execute operations |
resource |
botocore.client.S3
|
A S3 boto3 resource to execute operations |
Methods
list_buckets() -> list: Lists the names of all S3 buckets associated with the client.
bucket_objects_report() -> pd.DataFrame: Retrieves a report of objects within a specified S3 bucket.
all_buckets_objects_report() -> pd.DataFrame: Retrieves a report of objects from all buckets in the account.
get_date_partition_value_from_prefix() -> int: Extracts the date partition value from a given URI prefix.
get_last_date_partition() -> int: Retrieves the last date partition from a table in a S3 bucket.
About the key word argument **client_kwargs:
Users can get customized client and resource attributes for the given service passing additional keyword arguments. Under the hood, both client and resource class attributes are initialized as following:
# Setting up a boto3 client and resource
self.client = boto3.client("s3", **client_kwargs)
self.resource = boto3.resource("s3", **client_kwargs)
Source code in cloudgeass/aws/s3.py
19 20 21 22 23 24 25 26 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 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 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 |
|
list_buckets()
¶
Lists the names of all S3 buckets associated with the client.
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
A list of bucket names. |
Raises:
Type | Description |
---|---|
botocore.exceptions.ClientError
|
If there's an error while making the request. |
Examples:
# Importing the class
from cloudgeass.aws.s3 import S3Client
# Setting up a class instance and getting the list of buckets
s3 = S3Client()
buckets = s3.list_buckets()
Source code in cloudgeass/aws/s3.py
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 |
|
bucket_objects_report(bucket_name, prefix='')
¶
Retrieve a report of objects within a specified S3 bucket.
This method receives a bucket name and an optional bucket prefix to return a report in a pandas DataFrame format with all information about objects within the bucket and the given prefix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bucket_name |
str
|
The name of the S3 bucket. |
required |
prefix |
str
|
A prefix to filter objects. |
''
|
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pd.DataFrame: A DataFrame containing information about the objects. |
Raises:
Type | Description |
---|---|
botocore.exceptions.ClientError
|
If there's an error while making the request. |
Note
This method lists objects in the specified S3 bucket and creates a DataFrame with relevant information. The DataFrame includes columns for 'BucketName', 'Key', 'ObjectType', 'Size', 'SizeFormatted', 'LastModified', 'ETag', and 'StorageClass'. The 'ObjectType' column is determined by the file extension of the object key. The 'SizeFormatted' column provides a human-readable representation of the file size.
Examples:
# Importing the class
from cloudgeass.aws.s3 import S3Client
# Setting up a class instance and getting a complete bucket report
s3 = S3Client()
df_objects_report = s3.bucket_objects_report(
bucket_name="some-bucket-name",
prefix="some-optional-prefix"
)
Source code in cloudgeass/aws/s3.py
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 |
|
all_buckets_objects_report(prefix='', exclude_buckets=list())
¶
Retrieve a report of objects from all buckets in the AWS account.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix |
str
|
A prefix to filter objects. |
''
|
exclude_buckets |
list
|
List of bucket names to exclude from the report.. |
list()
|
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pd.DataFrame: A DataFrame containing information about the objects |
pd.DataFrame
|
from all specified buckets. |
Raises:
Type | Description |
---|---|
botocore.exceptions.ClientError
|
If there's an error while making the request. |
Note
This method lists calls self.list_buckets() to get a list of all buckets within an account and loops over this list to call self.bucket_objects_report() for each bucket in order to retrieve a pandas DataFrame with information about objects. At the end, all individual DataFrames are concatenated together to form the return for this method.
Examples:
# Importing the class
from cloudgeass.aws.s3 import S3Client
# Getting a report of objects in all buckets
s3 = S3Client()
df_all_buckets_report = s3.all_buckets_objects_report()
Source code in cloudgeass/aws/s3.py
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 |
|
get_date_partition_value_from_prefix(prefix_uri, partition_mode='name=value', date_partition_name='anomesdia', date_partition_idx=-2)
¶
Extracts the date partition value from a given URI prefix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix_uri |
str
|
The URI prefix containing the date partition information. |
required |
partition_mode |
str
|
The mode for extracting the partition value. Options are "name=value" (default) or "value". |
'name=value'
|
date_partition_name |
str
|
The name of the date partition in the URI. |
'anomesdia'
|
date_partition_idx |
int
|
The index of the date partition in the URI when using "value" mode. |
-2
|
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The extracted date partition value. |
Raises:
Type | Description |
---|---|
ValueError
|
If there's an issue with the URI or partition extraction. |
Note
This method extracts the date partition value from a given URI prefix based on the specified partition mode. - In "name=value" mode, it looks for the partition name in the URI and extracts the corresponding value. - In "value" mode, it directly extracts the partition value using the specified index.
Examples:
# Importing the class
from cloudgeass.aws.s3 import S3Client
# Setting up a class instance
s3 = S3Client()
# Getting the partition value given a partition URI
uri = "s3://my-bucket/anomesdia=20230101/data/"
partition_value = s3.get_date_partition_value_from_prefix(
partition_uri=uri,
partition_mode="name=value"
)
# 20230101
Source code in cloudgeass/aws/s3.py
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 |
|
get_last_date_partition(bucket_name, table_prefix, partition_mode='name=value', date_partition_name='anomesdia', date_partition_idx=-2)
¶
Retrieves the last date partition from a table in a S3 bucket.
In big data scenarios, tables are stored in S3 in URIs that, at most, uses the following structure: s3://bucket-name/table-name/partition-name=value/data.parquet. So, applications that needs to consume partitioned tables applies filters and other optimization techniques to retrieve only data they need.
In ETL proccess that stores new data frequently in daily, monthly, weekly or any other basis, new partitions are added everytime. So, consumers (other ETL proccesses or applications) that need to retrieve only the last data from a parent proccess may need to know in advance if the parent proccess had already stored their data.
One way to do that is by looking at the date partitions of a given table as S3 prefixes and retrieving the value of the last partition. And that's what this method does.
How is it possible?
As a rule of construction, to get the last date partition from a table, the following steps are done:
- Retrieval of a pandas DataFrame with all objects information from the given bucket (using the table name/prefix as a filter)
- Extraction of all objects keys
- Collection of all partition values from the object keys
- Sorting of all partition values and collection of the last one
As an additional information, this method puts together other methods from S3Client class as following:
bucket_objects_report()
to get all objects from the bucketget_date_partition_value_from_prefix()
to get partition value
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bucket_name |
str
|
The name of the S3 bucket. |
required |
table_prefix |
str
|
The table name used as a prefix filter |
required |
partition_mode |
str
|
The mode for extracting the partition value. Options are "name=value" (default) or "value". |
'name=value'
|
date_partition_name |
str
|
The name of the date partition in the URI. |
'anomesdia'
|
date_partition_idx |
int
|
The index of the date partition in the URI when using "value" mode. |
-2
|
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The last date partition value. |
Raises:
Type | Description |
---|---|
botocore.exceptions.ClientError
|
If there's an error while making the request. |
Examples:
# Importing the class
from cloudgeass.aws.s3 import S3Client
# Setting up a class instance
s3 = S3Client()
# Getting the last date partition value from a given table
bucket_name = "my-bucket"
table_name = "my-table-name"
last_partition = s3.get_last_date_partition(
bucket_name=bucket_name,
table_name=table_name
)
Source code in cloudgeass/aws/s3.py
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 |
|