Advanced¶
Attention
The following described pattern may come in very handy and convenient, still even though it may not be obvious, all the provided features do involve interactions with a bucketfs service in the background (upload, download, sync, etc.).
The MappedBucket¶
List files in a Bucket¶
from exasol.bucketfs import (
MappedBucket,
Service,
)
URL = "http://localhost:6666"
CREDENTIALS = {"default": {"username": "w", "password": "write"}}
bucketfs = Service(URL, CREDENTIALS)
default_bucket = MappedBucket(bucketfs["default"])
files = [file for file in default_bucket]
Upload files to a Bucket¶
import io
from exasol.bucketfs import (
MappedBucket,
Service,
)
URL = "http://localhost:6666"
CREDENTIALS = {"default": {"username": "w", "password": "write"}}
bucketfs = Service(URL, CREDENTIALS)
bucket = MappedBucket(bucketfs["default"])
# Upload bytes
data = bytes([65, 65, 65, 65])
bucket["some/path/file1.bin"] = data
# Upload file content
with open("myfile1.txt", "rb") as f:
bucket["destination/path/myfile1.txt"] = f
# Upload file like object
file_like = io.BytesIO(b"some content")
bucket["file/like/file2.txt"] = file_like
# Upload string conent
bucket["string/file2.txt"] = text.encode("utf-8")
# Upload generated content
bucket["string/file2.txt"] = (b"abcd" for _ in range(0, 10))
Download files from a Bucket¶
from exasol.bucketfs import (
MappedBucket,
Service,
)
URL = "http://localhost:6666"
CREDENTIALS = {"default": {"username": "w", "password": "write"}}
bucketfs = Service(URL, CREDENTIALS)
bucket = MappedBucket(bucketfs["default"])
# Download as raw bytes
data = as_bytes(bucket["some/other/path/file2.bin"])
# Download into file
file = as_file(bucket["some/other/path/file2.bin"], filename="myfile.bin")
# Download into string
my_utf8_string = as_string(bucket["some/utf8/encoded/text-file.txt"])
my_ascii_string = as_string(bucket["some/other/text-file.txt"], encoding="ascii")
Delete files from Bucket¶
from exasol.bucketfs import (
MappedBucket,
Service,
)
URL = "http://localhost:6666"
CREDENTIALS = {"default": {"username": "w", "password": "write"}}
bucketfs = Service(URL, CREDENTIALS)
bucket = MappedBucket(bucketfs["default"])
# Delete file from bucket
del bucket["some/path/file1.bin"]
Configure logging¶
import logging
from exasol.bucketfs import Service
# Attention:
# It is essential to configure the root logger at the beginning of your script.
# This ensures that log messages, including those from the bucketfs are handled correctly.
# Without proper configuration, log messages might not appear as expected.
logging.basicConfig(level=logging.INFO)
# Explicityly Configure the bucketfs logger if you need to.
#
# 1. Get a reference to the bucketfs logger
bucketfs_logger = logging.getLogger("exasol.bucketfs")
# 2. Configure the bucketfs logger as needed
# Note:
# By default bucketfs logger is set to NOTSET (https://docs.python.org/3.11/library/logging.html#logging.NOTSET)
# which should be sufficient in lots of cases where the root logger is configured approriately.
bucketfs_logger.setLevel(logging.DEBUG)
...