Basic’s

The Bucketfs Service

A single bucketfs service can host multiple buckets. In order to interact with a bucketfs service one can use the exasol.bucketfs.Service class.

List buckets

from exasol.bucketfs import Service

URL = "http://localhost:6666"
CREDENTIALS = {"default": {"username": "w", "password": "write"}}

bucketfs = Service(URL, CREDENTIALS)
buckets = [bucket for bucket in bucketfs]

Get a Bucket reference

from exasol.bucketfs import Service

URL = "http://localhost:6666"
CREDENTIALS = {"default": {"username": "w", "password": "write"}}

bucketfs = Service(URL, CREDENTIALS)
default_bucket = bucketfs["default"]

Bucket class

A Bucket contains a set of files which may be restricted, depending on the credentials of the requester. Using exasol.bucketfs.Bucket class the user can interact (download, upload, list and delete) files. with the files in the bucket.

List files in a Bucket

from exasol.bucketfs import Service

URL = "http://localhost:6666"
CREDENTIALS = {"default": {"username": "w", "password": "write"}}
bucketfs = Service(URL, CREDENTIALS)

default_bucket = bucketfs["default"]
files = [file for file in default_bucket]

Upload files to a Bucket

import io

from exasol.bucketfs import Service

URL = "http://localhost:6666"
CREDENTIALS = {"default": {"username": "w", "password": "write"}}

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

# Upload bytes
data = bytes([65, 65, 65, 65])
bucket.upload("some/other/path/file2.bin", data)

# Upload file content
with open("myfile2.txt", "rb") as f:
    bucket.upload("destination/path/myfile2.txt", f)

# Upload file like object
file_like = io.BytesIO(b"some content")
bucket.upload("file/like/file1.txt", file_like)

# Upload string content
text = "Some string content"
bucket.upload("string/file1.txt", text.encode("utf-8"))

# Upload generated content
generator = (b"abcd" for _ in range(0, 10))
bucket.upload("string/file2.txt", generator)

Download files from a Bucket

Note

When downloading a file from a bucket it will be provided back to the caller as an iterable set of byte chunks. This keeps the reception efficient and flexible regarding memory usage. Still most users will prefer to get a more tangible object from the download, in that case the bucketfs converters should be used.

Available Converters

  • exasol.bucketfs.as_bytes

  • exasol.bucketfs.as_string

  • exasol.bucketfs.as_hash

  • exasol.bucketfs.as_file

from exasol.bucketfs import (
    Service,
    as_bytes,
    as_file,
    as_string,
)

URL = "http://localhost:6666"
CREDENTIALS = {"default": {"username": "w", "password": "write"}}

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

# Download as raw bytes
data = as_bytes(bucket.download("some/other/path/file2.bin"))

# Download into file
file = as_file(bucket.download("some/other/path/file2.bin"), filename="myfile.bin")

# Download into string
my_utf8_string = as_string(bucket.download("some/utf8/encoded/text-file.txt"))
my_ascii_string = as_string(
    bucket.download("some/other/text-file.txt"), encoding="ascii"
)


Delete files from Bucket

from exasol.bucketfs import Service

URL = "http://localhost:6666"
CREDENTIALS = {"default": {"username": "w", "password": "write"}}

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

# Delete file from bucket
bucket.delete("some/other/path/file2.bin")