Google Cloud Storage#
The GCS client and its companion Bucket class provide the Google Cloud Storage implementation of unicloud's storage contract. Authentication supports a service-account JSON file path, the GOOGLE_APPLICATION_CREDENTIALS environment variable, or an encoded SERVICE_KEY_CONTENT environment variable.
unicloud.google_cloud.gcs.GCS
#
Bases: CloudStorageFactory
Google Cloud Storage client — the :class:CloudStorageFactory implementation for GCS.
Construction requires a project_id and resolves credentials through one of three paths
(service-account file path, GOOGLE_APPLICATION_CREDENTIALS env var, or the base64-encoded
SERVICE_KEY_CONTENT env var).
Examples:
- Instantiate from a service-account JSON file:
- Instantiate using GOOGLE_APPLICATION_CREDENTIALS in the environment:
- Instantiate using SERVICE_KEY_CONTENT (encoded via unicloud.utils.encode):
See Also
unicloud.aws.aws.S3: The matching AWS S3 implementation.
unicloud.utils.encode: Produces the value for SERVICE_KEY_CONTENT.
Source code in src/unicloud/google_cloud/gcs.py
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 | |
project_id
property
#
Return the Google Cloud project ID associated with this client.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The project ID that was passed to :meth: |
client
property
#
Return the cached google.cloud.storage client.
Returns:
| Type | Description |
|---|---|
Client
|
google.cloud.storage.client.Client: The client instance produced by |
Client
|
meth: |
bucket_list
property
#
List every bucket name visible to the current project.
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: Bucket names accessible under :attr: |
Examples:
- Enumerate every visible bucket:
__init__(project_id, service_key_path=None)
#
Initialize the GCS client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_id
|
str
|
The Google Cloud project name that owns (or can access) the buckets you intend to use. |
required |
service_key_path
|
Optional[str]
|
Optional filesystem path to a service-account JSON file. When
|
None
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If |
ValueError
|
If none of the three credential paths resolves (no |
Examples:
- Authenticate with a service-account JSON file path:
- Authenticate via GOOGLE_APPLICATION_CREDENTIALS:
Source code in src/unicloud/google_cloud/gcs.py
__str__()
#
Return a human-readable description of the client.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Multiline string containing the project ID and the client scopes. |
Examples:
- Inspect the client via print():
Source code in src/unicloud/google_cloud/gcs.py
__repr__()
#
Return a developer-facing representation of the client.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Same content as :meth: |
Examples:
- Inspect the client via repr():
Source code in src/unicloud/google_cloud/gcs.py
create_client()
#
Build a google.cloud.storage client from one of the three credential sources.
The selection order is:
- An explicit
service_key_pathpassed to :meth:__init__. - The
GOOGLE_APPLICATION_CREDENTIALSenvironment variable pointing at a service-account JSON file. - The
SERVICE_KEY_CONTENTenvironment variable — base64-encoded contents of a service-account JSON file, decoded via :func:unicloud.utils.decode.
When unicloud code runs on Google Cloud infrastructure (Compute Engine, Cloud Run, ...) the environment's default service account may already be available; in that case none of the three explicit paths is needed, but this method does not try to detect the default — caller is expected to set one of the credential sources above.
Returns:
| Type | Description |
|---|---|
Client
|
google.cloud.storage.client.Client: The initialized storage client. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither |
Source code in src/unicloud/google_cloud/gcs.py
upload(local_path, bucket_path)
#
Upload a single file via the factory-level shortcut.
Prefer :meth:get_bucket followed by :meth:Bucket.upload for directory uploads and
overwrite handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_path
|
str
|
Path to the local file to upload. |
required |
bucket_path
|
str
|
Destination path in the form |
required |
Examples:
- Push a local file into a bucket:
Source code in src/unicloud/google_cloud/gcs.py
download(cloud_path, local_path)
#
Download a single object via the factory-level shortcut.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cloud_path
|
Source path in the form |
required | |
local_path
|
Local destination path for the downloaded file. |
required |
Examples:
- Fetch an object to a local file:
Source code in src/unicloud/google_cloud/gcs.py
get_bucket(bucket_id)
#
Return a :class:Bucket handle for per-object operations on bucket_id.
The wrapped storage.Bucket uses the client's project as the billing project
(user_project=self.project_id), which is required for requester-pays buckets and
harmless otherwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket_id
|
str
|
The GCS bucket ID to look up. The method does not verify that the bucket exists; errors surface on the first actual operation. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Bucket |
Bucket
|
A :class: |
Examples:
- Get a bucket handle and list files:
Source code in src/unicloud/google_cloud/gcs.py
unicloud.google_cloud.gcs.Bucket
#
Bases: AbstractBucket
GCS bucket handle — the :class:AbstractBucket implementation for Google Cloud Storage.
Instances wrap a google.cloud.storage.bucket.Bucket and expose the unicloud contract on
top of it: upload/download (files and directories), delete, list (with optional prefix and
glob pattern), existence-check, rename, and raw blob access.
Examples:
- Prefer :meth:
GCS.get_bucketover constructing directly:
Source code in src/unicloud/google_cloud/gcs.py
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 | |
name
property
#
Return the bucket name.
Returns:
| Name | Type | Description |
|---|---|---|
str |
The name of the wrapped GCS bucket. |
bucket
property
#
Return the underlying google.cloud.storage Bucket object.
Exposed as an escape hatch for callers that need to drop down to the native SDK.
Returns:
| Type | Description |
|---|---|
Bucket
|
google.cloud.storage.bucket.Bucket: The wrapped GCS bucket object. |
__init__(bucket)
#
Wrap a google.cloud.storage Bucket object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket
|
Bucket
|
A |
required |
Source code in src/unicloud/google_cloud/gcs.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, max_results=None, pattern=None)
#
List objects in the bucket with optional prefix, limit, and glob filtering.
Filtering happens server-side for prefix and max_results (cheap) and client-side
for pattern (after the listing is retrieved), so a restrictive prefix plus a
loose pattern is the efficient combination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
Optional[str]
|
Optional object-name prefix, e.g. |
None
|
max_results
|
Optional[int]
|
Optional hard cap on the number of objects returned. Server-side filter. |
None
|
pattern
|
Optional[str]
|
Optional glob pattern applied with :func: |
None
|
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: Matching object names in the order the SDK returns them. |
Examples:
- List every object in the bucket:
- List objects under a prefix only:
- Cap the listing at 10 results:
Source code in src/unicloud/google_cloud/gcs.py
get_file(blob_id)
#
Return the raw google.cloud.storage.blob.Blob for blob_id.
Useful for operations that unicloud does not wrap — for example setting metadata,
generating signed URLs, or streaming downloads. Returns None if the blob does not
exist (via Bucket.get_blob).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blob_id
|
The object name in the bucket. |
required |
Returns:
| Type | Description |
|---|---|
Blob
|
google.cloud.storage.blob.Blob: The blob handle, or |
Examples:
- Get the raw blob and inspect its name:
Source code in src/unicloud/google_cloud/gcs.py
file_exists(file_name)
#
Return whether an object exists in the bucket.
Implemented as a single get_blob call; returns False for any blob that was not
found (including when the caller lacks read permission, so treat the return value as a
positive signal only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
Exact object name to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Examples:
- Probe for an existing file:
- Probe for a missing file:
Source code in src/unicloud/google_cloud/gcs.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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_path
|
Union[str, Path]
|
Path to the local file or directory to upload. |
required |
bucket_path
|
Union[str, Path]
|
Destination object name (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:
Note
Directory uploads preserve the tree structure relative to local_path.
Source code in src/unicloud/google_cloud/gcs.py
download(bucket_path, local_path, overwrite=False)
#
Download a file or 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[Path, str]
|
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 |
|---|---|
FileNotFoundError
|
If the bucket path (file or directory) does not exist. |
ValueError
|
If the local destination exists with |
Examples:
- Download a single file:
- Download every object under a prefix:
Warning
Ensure the destination has sufficient disk space when downloading large directories.
See Also
upload: The inverse operation that pushes files into the bucket.
Source code in src/unicloud/google_cloud/gcs.py
delete(bucket_path)
#
Delete a single blob or every blob under a prefix.
A trailing / on bucket_path triggers the recursive delete; anything else is
treated as a single-blob delete.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket_path
|
str
|
Blob name or directory prefix to delete. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the blob does not exist (single file), or the prefix matches nothing (directory). |
Examples:
- Delete a single blob:
- Delete every blob under a prefix:
Note
Directory deletes are not atomic: a crash mid-delete leaves partial state.
Source code in src/unicloud/google_cloud/gcs.py
rename(old_path, new_path)
#
Rename a blob or directory by copy-then-delete.
GCS has a native Blob.rewrite that is used per-blob for efficiency (no download /
upload round-trip), followed by a delete of the original. For a single blob 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 blob name or directory prefix. Trailing |
required |
new_path
|
str
|
New blob name or directory prefix to rename to. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
- Rename a single blob:
- Rename a directory recursively: