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"
CREDENTAILS = {"default": {"username": "w", "password": "write"}}
bucketfs = Service(URL, CREDENTAILS)

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"
CREDENTAILS = {"default": {"username": "w", "password": "write"}}

bucketfs = Service(URL, CREDENTAILS)
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"
CREDENTAILS = {"default": {"username": "w", "password": "write"}}

bucketfs = Service(URL, CREDENTAILS)
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"
CREDENTAILS = {"default": {"username": "w", "password": "write"}}

bucketfs = Service(URL, CREDENTAILS)
bucket = MappedBucket(bucketfs["default"])

# Delete file from bucket
del bucket["some/path/file1.bin"]