Blob Storage
Integrations for interacting with Azure Blob Storage
AzureBlobStorageContainer
Bases: ObjectStorageBlock
, WritableFileSystem
, WritableDeploymentStorage
Represents a container in Azure Blob Storage.
This class provides methods for downloading and uploading files and folders to and from the Azure Blob Storage container.
Attributes:
Name | Type | Description |
---|---|---|
container_name |
str
|
The name of the Azure Blob Storage container. |
credentials |
AzureBlobStorageCredentials
|
The credentials to use for authentication with Azure. |
base_folder |
Optional[str]
|
A base path to a folder within the container to use for reading and writing objects. |
Source code in prefect_azure/blob_storage.py
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 |
|
download_folder_to_path(from_folder, to_folder, **download_kwargs)
async
Download a folder from the container to a local path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
from_folder |
str
|
The folder path in the container to download. |
required |
to_folder |
Union[str, Path]
|
The local path to download the folder to. |
required |
**download_kwargs |
Dict[str, Any]
|
Additional keyword arguments passed into
|
{}
|
Returns:
Type | Description |
---|---|
Coroutine[Any, Any, Path]
|
The local path where the folder was downloaded. |
Example
Download the contents of container folder folder
from the container
to the local folder local_folder
:
from prefect_azure import AzureBlobStorageCredentials
from prefect_azure.blob_storage import AzureBlobStorageContainer
credentials = AzureBlobStorageCredentials(
connection_string="connection_string",
)
block = AzureBlobStorageContainer(
container_name="container",
credentials=credentials,
)
block.download_folder_to_path(
from_folder="folder",
to_folder="local_folder"
)
Source code in prefect_azure/blob_storage.py
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 |
|
download_object_to_file_object(from_path, to_file_object, **download_kwargs)
async
Downloads an object from the container to a file object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
from_path |
The path of the object to download within the container. |
required | |
to_file_object |
BinaryIO
|
The file object to download the object to. |
required |
**download_kwargs |
Dict[str, Any]
|
Additional keyword arguments for the download operation. |
{}
|
Returns:
Type | Description |
---|---|
Coroutine[Any, Any, BinaryIO]
|
The file object that the object was downloaded to. |
Example
Download the object object
from the container to a file object:
from prefect_azure import AzureBlobStorageCredentials
from prefect_azure.blob_storage import AzureBlobStorageContainer
credentials = AzureBlobStorageCredentials(
connection_string="connection_string",
)
block = AzureBlobStorageContainer(
container_name="container",
credentials=credentials,
)
with open("file.txt", "wb") as f:
block.download_object_to_file_object(
from_path="object",
to_file_object=f
)
Source code in prefect_azure/blob_storage.py
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 |
|
download_object_to_path(from_path, to_path, **download_kwargs)
async
Downloads an object from a container to a specified path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
from_path |
str
|
The path of the object in the container. |
required |
to_path |
Union[str, Path]
|
The path where the object will be downloaded to. |
required |
**download_kwargs |
Dict[str, Any]
|
Additional keyword arguments for the download operation. |
{}
|
Returns:
Type | Description |
---|---|
Coroutine[Any, Any, Path]
|
The path where the object was downloaded to. |
Example
Download the object object
from the container to the local path
file.txt
:
from prefect_azure import AzureBlobStorageCredentials
from prefect_azure.blob_storage import AzureBlobStorageContainer
credentials = AzureBlobStorageCredentials(
connection_string="connection_string",
)
block = AzureBlobStorageContainer(
container_name="container",
credentials=credentials,
)
block.download_object_to_path(
from_path="object",
to_path="file.txt"
)
Source code in prefect_azure/blob_storage.py
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 |
|
get_directory(from_path=None, local_path=None)
async
Downloads the contents of a direry from the blob storage to a local path.
Used to enable flow code storage for deployments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
from_path |
str
|
The path of the directory in the blob storage. |
None
|
local_path |
str
|
The local path where the directory will be downloaded. |
None
|
Source code in prefect_azure/blob_storage.py
618 619 620 621 622 623 624 625 626 627 628 629 630 631 |
|
put_directory(local_path=None, to_path=None, ignore_file=None)
async
Uploads a directory to the blob storage.
Used to enable flow code storage for deployments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
local_path |
str
|
The local path of the directory to upload. Defaults to current directory. |
None
|
to_path |
str
|
The destination path in the blob storage. Defaults to root directory. |
None
|
ignore_file |
str
|
The path to a file containing patterns to ignore during upload. |
None
|
Source code in prefect_azure/blob_storage.py
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 |
|
read_path(path)
async
Reads the contents of a file at the specified path and returns it as bytes.
Used to enable results storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The path of the file to read. |
required |
Returns:
Type | Description |
---|---|
bytes
|
The contents of the file as bytes. |
Source code in prefect_azure/blob_storage.py
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
|
upload_from_file_object(from_file_object, to_path, **upload_kwargs)
async
Uploads an object from a file object to the specified path in the blob storage container.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
from_file_object |
BinaryIO
|
The file object to upload. |
required |
to_path |
str
|
The path in the blob storage container to upload the object to. |
required |
**upload_kwargs |
Dict[str, Any]
|
Additional keyword arguments to pass to the upload_blob method. |
{}
|
Returns:
Type | Description |
---|---|
Coroutine[Any, Any, str]
|
The path where the object was uploaded to. |
Example
Upload a file object to the container at the path object
:
from prefect_azure import AzureBlobStorageCredentials
from prefect_azure.blob_storage import AzureBlobStorageContainer
credentials = AzureBlobStorageCredentials(
connection_string="connection_string",
)
block = AzureBlobStorageContainer(
container_name="container",
credentials=credentials,
)
with open("file.txt", "rb") as f:
block.upload_from_file_object(
from_file_object=f,
to_path="object"
)
Source code in prefect_azure/blob_storage.py
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 |
|
upload_from_folder(from_folder, to_folder, **upload_kwargs)
async
Uploads files from a local folder to a specified folder in the Azure Blob Storage container.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
from_folder |
Union[str, Path]
|
The path to the local folder containing the files to upload. |
required |
to_folder |
str
|
The destination folder in the Azure Blob Storage container. |
required |
**upload_kwargs |
Dict[str, Any]
|
Additional keyword arguments to pass to the
|
{}
|
Returns:
Type | Description |
---|---|
Coroutine[Any, Any, str]
|
The full path of the destination folder in the container. |
Example
Upload the contents of the local folder local_folder
to the container
folder folder
:
from prefect_azure import AzureBlobStorageCredentials
from prefect_azure.blob_storage import AzureBlobStorageContainer
credentials = AzureBlobStorageCredentials(
connection_string="connection_string",
)
block = AzureBlobStorageContainer(
container_name="container",
credentials=credentials,
)
block.upload_from_folder(
from_folder="local_folder",
to_folder="folder"
)
Source code in prefect_azure/blob_storage.py
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 |
|
upload_from_path(from_path, to_path, **upload_kwargs)
async
Uploads an object from a local path to the specified destination path in the blob storage container.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
from_path |
Union[str, Path]
|
The local path of the object to upload. |
required |
to_path |
str
|
The destination path in the blob storage container. |
required |
**upload_kwargs |
Dict[str, Any]
|
Additional keyword arguments to pass to the
|
{}
|
Returns:
Type | Description |
---|---|
Coroutine[Any, Any, str]
|
The destination path in the blob storage container. |
Example
Upload a file from the local path file.txt
to the container
at the path object
:
from prefect_azure import AzureBlobStorageCredentials
from prefect_azure.blob_storage import AzureBlobStorageContainer
credentials = AzureBlobStorageCredentials(
connection_string="connection_string",
)
block = AzureBlobStorageContainer(
container_name="container",
credentials=credentials,
)
block.upload_from_path(
from_path="file.txt",
to_path="object"
)
Source code in prefect_azure/blob_storage.py
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 |
|
write_path(path, content)
async
Writes the content to the specified path in the blob storage.
Used to enable results storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The path where the content will be written. |
required |
content |
bytes
|
The content to be written. |
required |
Source code in prefect_azure/blob_storage.py
696 697 698 699 700 701 702 703 704 705 706 707 |
|
blob_storage_download(container, blob, blob_storage_credentials)
async
Downloads a blob with a given key from a given Blob Storage container.
Args:
blob: Name of the blob within this container to retrieve.
container: Name of the Blob Storage container to retrieve from.
blob_storage_credentials: Credentials to use for authentication with Azure.
Returns:
A bytes
representation of the downloaded blob.
Example:
Download a file from a Blob Storage container
from prefect import flow
from prefect_azure import AzureBlobStorageCredentials
from prefect_azure.blob_storage import blob_storage_download
@flow
def example_blob_storage_download_flow():
connection_string = "connection_string"
blob_storage_credentials = AzureBlobStorageCredentials(
connection_string=connection_string,
)
data = blob_storage_download(
container="prefect",
blob="prefect.txt",
blob_storage_credentials=blob_storage_credentials,
)
return data
example_blob_storage_download_flow()
Source code in prefect_azure/blob_storage.py
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 |
|
blob_storage_list(container, blob_storage_credentials, name_starts_with=None, include=None, **kwargs)
async
List objects from a given Blob Storage container.
Args:
container: Name of the Blob Storage container to retrieve from.
blob_storage_credentials: Credentials to use for authentication with Azure.
name_starts_with: Filters the results to return only blobs whose names
begin with the specified prefix.
include: Specifies one or more additional datasets to include in the response.
Options include: 'snapshots', 'metadata', 'uncommittedblobs', 'copy',
'deleted', 'deletedwithversions', 'tags', 'versions', 'immutabilitypolicy',
'legalhold'.
**kwargs: Addtional kwargs passed to ContainerClient.list_blobs()
Returns:
A list
of dict
s containing metadata about the blob.
Example:
from prefect import flow
from prefect_azure import AzureBlobStorageCredentials
from prefect_azure.blob_storage import blob_storage_list
@flow
def example_blob_storage_list_flow():
connection_string = "connection_string"
blob_storage_credentials = AzureBlobStorageCredentials(
connection_string="connection_string",
)
data = blob_storage_list(
container="container",
blob_storage_credentials=blob_storage_credentials,
)
return data
example_blob_storage_list_flow()
Source code in prefect_azure/blob_storage.py
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 |
|
blob_storage_upload(data, container, blob_storage_credentials, blob=None, overwrite=False)
async
Uploads data to an Blob Storage container.
Args:
data: Bytes representation of data to upload to Blob Storage.
container: Name of the Blob Storage container to upload to.
blob_storage_credentials: Credentials to use for authentication with Azure.
blob: Name of the blob within this container to retrieve.
overwrite: If True
, an existing blob with the same name will be overwritten.
Defaults to False
and an error will be thrown if the blob already exists.
Returns:
The blob name of the uploaded object
Example:
Read and upload a file to a Blob Storage container
from prefect import flow
from prefect_azure import AzureBlobStorageCredentials
from prefect_azure.blob_storage import blob_storage_upload
@flow
def example_blob_storage_upload_flow():
connection_string = "connection_string"
blob_storage_credentials = AzureBlobStorageCredentials(
connection_string=connection_string,
)
with open("data.csv", "rb") as f:
blob = blob_storage_upload(
data=f.read(),
container="container",
blob="data.csv",
blob_storage_credentials=blob_storage_credentials,
overwrite=False,
)
return blob
example_blob_storage_upload_flow()
Source code in prefect_azure/blob_storage.py
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 |
|