Azure Blob Storage BlobPath

Core Interfaces

class blob_path.backends.azure_blob_storage.AzureBlobPath(storage_account: str, container: str, name: PurePath)

BlobPath modeling Azure Blob Storage.

Properties:
  • Globally Unique: True

An AzureBlobPath is located by three parameters: storage_account, container and a name. You can pass this path around anywhere (any server, lambda, container, etc.) and the correct Azure Blob Store location will always be uniquely identified (__eq__, serialise and deserialise also behaves sanely here, that is, no matter the location, same serialised representations point to the same location globally and uniquely).

Implements: blob_path.core.BlobPath

Apart from the interface exposed by BlobPath, this class provides some extension points users can use to tweak how communication with Azure is done (you should be wholly able to tweak all performance and security params). Its advised to only override the methods below for extending the functionality of a path Methods that are safe to inherit and override: download, upload and credential

This class does not use any implicit variables other than for providing the create_default factory function

property container: str

getter for container

classmethod create_default(p: PurePath) Self

Create a new AzureBlobPath, the container and storage_account would be injected from implicit variables.

Implicit variables:
  • storage_account: IMPLICIT_BLOB_PATH_GEN_AZURE_BLOB_STORAGE_ACCOUNT

  • container: IMPLICIT_BLOB_PATH_GEN_AZURE_BLOB_CONTAINER

Parameters:

p – A PurePath which represents the “object_key” that you want to use

Returns:

An AzureBlobPath

credential()

Generate the credential used for authenticating with azure.

Override this method if you want to change how your credentials are located

delete() bool

Delete the file if it exists.

How delete happens is based on the underlying storage and is not important. The file might be accessible through other means if the underlying storage keeps some sort of archive (like S3 versioned buckets), but doing an exists should return False once delete is called, no matter what how the underlying storage works. A read on the file using open will raise DoesNotExist if a file is deleted.

Returns:

True if the file existed and was deleted, else False

classmethod deserialise(data: SerialisedBlobPath) Self

Deserialise a given serialised representation.

Do not use this method directly in your code, you should use blob_path.deserialise.deserialise

Parameters:

data – A SerialisedBlobPath whose kind should always be equal to self.kind

Returns:

A new BlobPath instance

download(handle: IO[bytes])

Download data for the given Azure Blob Store path and write it to the provided binary handle.

Users can extend this method if they want to change how the download is done This is recommended if you want to tweak your performance etc.

exists() bool

Check if the file exists.

Returns:

a boolean based on whether the file exists or not

kind = 'blob-path-azure'

kind is a globally unique class variable which uniquely identifies a subtype of BlobPath

Each subtype defines its kind which should never clash with any other implementation. kind is used for serialisation

property name: PurePath

getter for object path or name

open(mode: str = 'r') Generator

Open the underlying file in the given mode.

This function mimics the builtin open function. It fetches the file from the underlying storage and opens it. Returns a file handle to the downloaded file. If the file is opened in write mode, it is uploaded back to the cloud when the handle is closed. Currently this function can only be opened with a context manager. (you can’t manually call close right now) If the file is opened using w mode, then the file does not need to exist in the underlying storage

Parameters:

mode – the mode in which the file should be opened. Currently only a is not supported

Returns:

a file handle where the user can read/write data. Once the context is finished, the file is uploaded to the backend if file was opened in w mode

Raises:

blob_path.interface.DoesNotExist – The file does not exist

property parent: AzureBlobPath

The logical parent of the path.

Behavior is consistent with pathlib.PurePath.parent. In case of an empty path/root path, the current path is returned as is

Returns:

A new BlobPath which is the parent of the current path

serialise() SerialisedBlobPath

serialise a BlobPath to a JSON-able dict which can be passed around

Generally, if a BlobPath is deserialised from some serialised representation, it should be perfectly reproducible. That is two path representations of the same serialisation anywhere (different process, different server, etc.) should point to the same file if it is accessible. This might not always be true (depending on what storage backend you are using), read the documentation of the underlying backend for caveats That said, the library tries to follow this requirement diligently, all paths which can be uniquely pointed from anywhere in the world (S3, Azure Blob Store, etc) always follow this.

Returns:

A JSON-able dict

Return type:

blob_path.interface.SerialisedBlobPath

property storage_account: str

getter for storage account

upload(handle: IO[bytes]) None

Upload data to the given Azure Blob Store path.

Users can extend this method if they want to change how the download is done This is recommended if you want to tweak your performance etc.

Serialisation

class blob_path.backends.azure_blob_storage.Payload(*, storage_account: str, container: str, name: list[str])