Skip to content

Blob

Interface for Datatailr's binary large object (blob) storage.

Blob storage lets you persist and retrieve arbitrary files and binary data. Each blob is identified by a unique name (key/path).

This class keeps all data in one bucket, the user data bucket. The name of that bucket is the value of the platform tag blob_storage_prefix followed by user-data. For example, if the tag is acme-, the bucket is acme-user-data.

A name that you pass to this class is a path inside the user data bucket. The first part of the name is a folder, not a bucket. With the tag above, Blob().put("reports/summary.csv", data) writes to blob://acme-user-data/reports/summary.csv. This class cannot read or write other buckets. Every method, acls and set_acls included, adds the bucket to the name. The full path blob://<blob_storage_prefix>user-data/<name> is also accepted.

The dt blob CLI does not add the bucket. To find a blob of this class with the CLI, put the bucket name first, for example dt blob ls acme-user-data/reports/. The command dt tag get blob_storage_prefix prints the value of the tag.

Warning

The platform keeps data of its own in the user data bucket. Workflow data is in the folder <prefix>workflows/, where <prefix> is the value of the tag. Distributed joblib data is in the folder joblib-pools/. Do not write to these folders or delete them.

Notes

This class wraps Datatailr blob operations (list/copy/read/write/delete). Large reads/writes use temporary files internally. A single transfer (get, put, get_file, put_file) may run for up to TRANSFER_TIMEOUT seconds (30 minutes). Other blob operations time out after 30 seconds.

Examples:

Basic file copy to/from blob storage:

>>> from datatailr import Blob
>>> b = Blob()
>>> _ = open("/tmp/data.txt", "wb").write(b"qwerty123")
>>> b.put_file("my_folder/data.txt", "/tmp/data.txt")
>>> b.get_file("my_folder/data.txt", "/tmp/data_copy.txt")

Read/write blob contents as bytes:

>>> payload = b"hello world"
>>> b.put("my_folder/hello.bin", payload)
>>> data = b.get("my_folder/hello.bin")
>>> data == payload
True

List and delete:

>>> sorted(b.ls("my_folder/"), key=lambda x: x["name"])
[{'is_file': True, 'last_modified': ..., 'name': 'my_folder/data.txt', 'size': 9}, {'is_file': True, 'last_modified': ..., 'name': 'my_folder/hello.bin', 'size': 11}]
>>> b.delete("my_folder/hello.bin")
>>> b.exists("my_folder/hello.bin")
False

__acl_from_cli staticmethod

Build an ACL from the JSON output of dt blob get-acl.

The output lists user and group names in users_read, users_write, groups_read and groups_write. The flags others_read and others_write mean that the dtusers group has the permission.

acls

Retrieve the access control list (ACL) for a specific blob path.

Blobs have read and write permissions only, so the returned ACL has the keys Permission.READ and Permission.WRITE. Access for all users ("others" in dt blob get-acl) shows as the dtusers group. Users and groups that no longer exist are left out.

Parameters:

  • path

    (str) –

    The name of the blob or folder in the user data bucket, for example my_folder/data.txt. The full path blob://<blob_storage_prefix>user-data/my_folder/data.txt is also accepted.

Returns:

  • ACL ( ACL ) –

    The ACL for the blob path.

Example:

from datatailr import Blob, Permission
acl = Blob().acls("my_folder/data.txt")
readers = acl.permissions[Permission.READ]

delete

Delete a blob.

Parameters:

  • name

    (str) –

    The name of the blob to delete, as a path inside the user data bucket.

exists

Check if a blob exists.

Parameters:

  • name

    (str) –

    The name of the blob to check, as a path inside the user data bucket.

Returns:

  • bool ( bool ) –

    True if the blob exists, False otherwise.

get

Get the content of a blob.

Parameters:

  • name

    (str) –

    The name of the blob to retrieve, as a path inside the user data bucket.

Returns:

  • bytes ( bytes ) –

    The content of the blob.

get_file

Copy a blob file to a local file.

Parameters:

  • name

    (str) –

    The name of the blob to retrieve, as a path inside the user data bucket.

  • path

    (str) –

    The path to store the blob as a file.

ls

List files in the specified prefix. If recursive is True, list files in subdirectories recursively.

Parameters:

  • prefix

    (str) –

    The prefix to list files from, as a path inside the user data bucket. Use "/" to list the top of the bucket.

  • recursive

    (bool, default: False ) –

    Whether to list files in subdirectories recursively.

  • filter_by

    (str, default: '' ) –

    The filter expression to apply to the list of files.

  • limit

    (int, default: -1 ) –

    The maximum number of files to return.

  • offset

    (int, default: 0 ) –

    The offset to start returning files from.

Returns:

  • list ( list[dict] ) –

    A list of file dictionaries in the specified path. If recursive is True, the list will contain files in subdirectories recursively. Each dictionary contains the following keys:

    • name: The path of the entry inside the user data bucket. The name of a folder ends with /.
    • size: The size of the file.
    • last_modified: The last modified time of the file.
    • is_file: Whether the entry is a file.

put

Put a blob object into the blob storage.

Parameters:

  • name

    (str) –

    The name of the blob to create, as a path inside the user data bucket.

  • blob

    (bytes | str) –

    The content to store. A string is encoded as UTF-8.

  • acl

    (ACL, default: None ) –

    The access control list for the blob.

put_file

Copy a local file to a blob.

Parameters:

  • name

    (str) –

    The name of the blob to create, as a path inside the user data bucket.

  • path

    (str) –

    The path of the local file to copy.

  • acl

    (ACL, default: None ) –

    The access control list for the blob.

set_acls

Set the access control list (ACL) for a specific blob path, replacing the existing ACL.

To give a permission to all users, add Group("dtusers") to it.

Replacing an ACL that is already stored requires write permission on it. Setting an ACL on a path that has none yet, including a directory, is limited to administrators. A path with no ACL can be read or changed only by administrators. Files created under a directory pick up that directory's ACL when they are not given one of their own. The ACL is the file's when the path names a file, and the directory's when the path ends with / or names no file.

Parameters:

  • path

    (str) –

    The name of the blob or folder in the user data bucket, for example my_folder/data.txt. The full path blob://<blob_storage_prefix>user-data/my_folder/data.txt is also accepted.

  • acls

    (ACL) –

    The ACL to apply.

Example:

from datatailr import Blob, ACL, Permission, User, Group
acls = ACL({Permission.READ: [User("alice"), Group("analysts")],
            Permission.WRITE: [User("alice")]})
Blob().set_acls("my_folder/data.txt", acls)