AWS S3#
The S3 client and its companion Bucket class provide the AWS S3 implementation of unicloud's storage contract. Credentials are read from the standard AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY / AWS_DEFAULT_REGION environment variables; additional boto3 configuration (for example a custom botocore.config.Config) can be passed via the configs argument.
unicloud.aws.aws.S3
#
Bases: CloudStorageFactory
AWS S3 client — the :class:CloudStorageFactory implementation for Amazon S3.
Instantiating the class constructs a boto3 S3 client using credentials pulled from the
environment. Additional keyword arguments that you would otherwise pass to boto3.client
(for example a custom botocore.config.Config, or a different region_name) can be
forwarded via the configs parameter.
Examples:
- Create a client from environment variables only:
- Create a client with a custom botocore config and region override:
See Also
unicloud.google_cloud.gcs.GCS: The matching Google Cloud Storage implementation.
Source code in src/unicloud/aws/aws.py
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 | |
client
property
#
Return the cached boto3 S3 client.
Returns:
| Type | Description |
|---|---|
|
boto3.client: The S3 client instance produced by :meth: |
__init__(configs=None)
#
Initialize the S3 client.
Credentials are read from the standard AWS environment variables. Any extra keyword
arguments you want passed to boto3.client("s3", ...) can be supplied via configs
— they override the defaults that this class sets for service_name, region_name,
aws_access_key_id, and aws_secret_access_key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
configs
|
Optional[Dict]
|
Optional dictionary of extra keyword arguments to forward to |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any of |
NoCredentialsError
|
Propagated from |
PartialCredentialsError
|
Propagated from |
Examples:
- Default construction reading every credential from the environment:
- Pass a custom botocore Config to override the signature version:
See Also
https://docs.aws.amazon.com/cli/v1/userguide/cli-configure-envvars.html: Canonical list of the AWS environment variables this class honors.
Source code in src/unicloud/aws/aws.py
create_client(configs=None)
#
Build a boto3 S3 client from environment credentials plus optional overrides.
This is the hook used internally by :meth:__init__ and re-exposed publicly so callers
can rebuild the client (for example after rotating a signing config) without re-creating
the :class:S3 instance. Alternative authentication paths — IAM roles on EC2/ECS/Lambda,
the shared credentials file written by aws configure, AWS SSO — are all picked up by
boto3 itself when the corresponding env vars happen to be set to dummy values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
configs
|
Optional[Dict]
|
Optional dictionary of extra keyword arguments to merge into the
|
None
|
Returns:
| Type | Description |
|---|---|
client
|
boto3.client: A configured S3 client ready for |
client
|
etc. calls. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any of |
NoCredentialsError
|
Raised by |
PartialCredentialsError
|
Raised by |
Examples:
- Build a client with a custom signature version and inspect the region:
Source code in src/unicloud/aws/aws.py
upload(local_path, bucket_path)
#
Upload a single file to S3 via the factory-level shortcut.
Prefer :meth:get_bucket followed by :meth:Bucket.upload for anything beyond a
one-shot file push — the bucket-level API supports recursive directory uploads and
overwrite handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_path
|
Union[str, Path]
|
Path to the local file to upload. |
required |
bucket_path
|
str
|
Destination path in the form |
required |
Raises:
| Type | Description |
|---|---|
Exception
|
Any |
Examples:
- Upload a local file to a bucket under a named key:
Source code in src/unicloud/aws/aws.py
download(bucket_path, local_path)
#
Download a single object from S3 via the factory-level shortcut.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket_path
|
str
|
Source path in the form |
required |
local_path
|
Union[str, Path]
|
Local destination path for the downloaded file. |
required |
Raises:
| Type | Description |
|---|---|
Exception
|
Any |
Examples:
- Download a single object to a local path:
Source code in src/unicloud/aws/aws.py
get_bucket(bucket_name)
#
Return a :class:Bucket handle for per-object operations on bucket_name.
The returned object wraps a boto3.resources.factory.s3.Bucket resource (which is a
richer interface than the flat client) and exposes the unicloud :class:AbstractBucket
surface on top of it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket_name
|
str
|
The AWS S3 bucket name to look up. The method does not verify that the bucket exists; that error surfaces on the first actual operation. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Bucket |
Bucket
|
A :class: |
Examples:
- Get a bucket and list its contents:
Source code in src/unicloud/aws/aws.py
unicloud.aws.aws.Bucket
#
Bases: AbstractBucket
AWS S3 bucket handle — the :class:AbstractBucket implementation for S3.
Instances wrap a boto3.resources.factory.s3.Bucket and expose the unicloud contract on top
of it: upload/download (files and directories), delete, list, existence-check, and rename.
Examples:
- Prefer :meth:
S3.get_bucketover constructing directly, then probe the bucket:
Source code in src/unicloud/aws/aws.py
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 | |
bucket
property
#
Return the underlying boto3 S3 Bucket resource.
Exposed as an escape hatch for callers that need to drop down to the native SDK (for example to set lifecycle policies, which unicloud does not wrap).
Returns:
| Type | Description |
|---|---|
|
boto3.resources.factory.s3.Bucket: The wrapped boto3 resource. |
name
property
#
Return the bucket name.
Returns:
| Name | Type | Description |
|---|---|---|
str |
The name of the wrapped S3 bucket. |
__init__(bucket)
#
Wrap a boto3 S3 Bucket resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket
|
A |
required |
Examples:
- Instantiate directly from a boto3 resource:
- Or let S3.get_bucket build it for you (preferred):
Source code in src/unicloud/aws/aws.py
__str__()
#
Return "Bucket: <name>".
Returns:
| Name | Type | Description |
|---|---|---|
str |
Human-readable representation including the bucket name. |
__repr__()
#
Return "Bucket: <name>" — same as :meth:__str__.
Returns:
| Name | Type | Description |
|---|---|---|
str |
Developer-facing representation. |
list_files(prefix=None)
#
List object keys in the bucket, optionally filtered by a key prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
Optional[str]
|
Optional key prefix to filter the listing. Passing |
None
|
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: Object keys matching the prefix, in the order the SDK returns them. |
Examples:
- List every object in the bucket:
- List only objects under a folder:
Source code in src/unicloud/aws/aws.py
upload(local_path, bucket_path, overwrite=False)
#
Upload a file or directory to the bucket.
When local_path is a directory, every file beneath it is uploaded recursively and the
relative tree under bucket_path is preserved. Empty directories raise a
ValueError because S3 has no concept of an empty directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_path
|
Union[str, Path]
|
Path to the local file or directory to upload. |
required |
bucket_path
|
str
|
Destination key (for a single file) or destination prefix (for a
directory). Trailing |
required |
overwrite
|
bool
|
If |
False
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If |
ValueError
|
If |
Examples:
- Upload a single file:
- Upload a directory recursively, overwriting any conflicts:
Source code in src/unicloud/aws/aws.py
download(bucket_path, local_path, overwrite=False)
#
Download a file or a directory from the bucket.
A trailing / on bucket_path triggers the recursive-directory code path; anything
else is treated as a single-object download.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket_path
|
str
|
Path inside the bucket to download. Trailing |
required |
local_path
|
Union[str, Path]
|
Local destination. For a single file this is the full filename; for a directory it is the directory root (created if missing). |
required |
overwrite
|
bool
|
If |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the local destination already exists with |
Examples:
- Download a single object:
- Download a directory recursively with overwrites:
Source code in src/unicloud/aws/aws.py
delete(bucket_path)
#
Delete a single object or a directory (recursively) from the bucket.
A trailing / on bucket_path triggers the recursive delete; anything else is
treated as a single-object delete.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket_path
|
str
|
Object key or directory prefix to delete. Trailing |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the key does not exist (for single files) or the prefix matches nothing (for directories). |
Examples:
- Delete a single file:
- Delete every object under a prefix:
Source code in src/unicloud/aws/aws.py
file_exists(file_name)
#
Return whether an exact object key exists in the bucket.
Implemented as a list_objects prefix filter followed by an exact-match check, so it
is safe against the common "prefix match" pitfall where "file" would also match
"file-backup".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
Exact object key to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Examples:
- Check for an existing object:
- Check for a missing object:
Source code in src/unicloud/aws/aws.py
rename(old_path, new_path)
#
Rename an object or directory by copy-then-delete.
S3 has no native rename, so this method copies each matching object to the new key and then deletes the original. For a single object the operation is effectively atomic; for a directory it is not — a crash mid-rename leaves partial state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old_path
|
str
|
Current object key or directory prefix. Trailing |
required |
new_path
|
str
|
New object key or directory prefix to rename to. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
- Rename a single object:
- Rename a directory recursively: