Swift

Siehe auch

Swift ist ein verteilter Objekt-/Blob-Speicher. Das OpenStack Object Store-Projekt, bekannt als Swift, bietet eine Cloud-Speicher-Software, mit der grosse Datenmengen über eine einfache API gespeichert und abgerufen werden können. Swift ist auf Skalierbarkeit ausgelegt und auf Zuverlässigkeit, Verfügbarkeit und konkurrierende Zugriffe hin optimiert.

Links

Tipp

Wer Infomaniak Swiss Backup mit Swift Storage und Cyberduck auf Mac oder Windows verwendet, muss die URL https://swift02-api.cloud.infomaniak.ch und diese „Project:Domain:Username“-Struktur verwenden: sb_project_SBI-AB123456:Default:SBI-AB123456

Installation

sudo dnf -y install python3-devel

pip3 install --user python-swiftclient
pip3 install --user python-keystoneclient

Verwendung

In den Beispielen wird der Infomaniak Swift Object-Storage verwendet.

Tipp

Der Umgang mit swift wird bedeutend einfacher, wenn man vorher eine OpenStack.rc-Datei „sourced“, um sich damit die (nachfolgend gezeigten) tausend Parameter zu sparen. Ein Beispiel für solch eine rc-Datei findet sich weiter unten.

source path/to/openstack.rc
swift list --lh

Verzeichnis-Listing anzeigen (ohne .rc-File):

swift \
    --os-auth-url=https://swiss-backup02.infomaniak.com/identity/v3 \
    --os-username=SBI-AB123456 \
    --os-password='mypassword' \
    --auth-version=3 \
    --os-tenant-name=sb_project_SBI-AB123456 \
    --os-user-domain-name=default \
    list --lh

Beispiel-Ausgabe mit Bedeutung:

count  bytes  modify-timestamp     bucket-name
  110  568M   2022-12-16 23:54:04  host.example.com
  ...
 9.5K  37G

Swift-Container löschen:

swift \
    --os-auth-url=https://swiss-backup02.infomaniak.com/identity/v3 \
    --os-username=SBI-AB123456 \
    --os-password='mypassword' \
    --auth-version=3 \
    --os-tenant-name=sb_project_SBI-AB123456 \
    --os-user-domain-name=default \
    delete \
    mybucket

Quota für einen Container einrichten:

source path/to/openstack.rc

swift stat mybucket
# 5GB for example
swift post --meta 'quota-bytes: 5000000000' mybucket
swift stat mybucket

Quota für einen Container entfernen:

source path/to/openstack.rc

swift stat mybucket
swift post --meta 'quota-bytes: ' mybucket
swift stat mybucket

OpenStack.rc-Datei

export OS_AUTH_URL=https://api.pub1.infomaniak.cloud/identity/v3
export OS_PROJECT_NAME=PCP-ABCDEF1
export OS_PROJECT_DOMAIN_NAME=default
export OS_USERNAME=PCU-ABCDEF1
export OS_USER_DOMAIN_NAME=default
export OS_PROJECT_ID=07f32f66709d4de6aca23bad819ced3d
export OS_IDENTITY_API_VERSION=3
export OS_INTERFACE=public
export OS_REGION_NAME=dc3-a
# To avoid being prompted for your password each time,
# write your password below and uncomment the line
OS_PASSWORD='linuxfabrik'
[ -z "$OS_PASSWORD" ] && read -e -p "Please enter your OpenStack Password for project $OS_PROJECT_NAME as user $OS_USERNAME: " OS_PASSWORD
export OS_PASSWORD

Swift Cheat Sheet

# List containers/objects
swift list
swift list container_name

# Create a container with the default storage policy
swift post mybucket

# Set access list
swift post --read-acl 'project:*' test
swift post --write-acl 'project:linus' test

# Upload
swift upload mybucket ~/myfile.txt --object-name myobject

# Stats
swift stat mybucket

# Enable versioning
swift post --header "X-Versions-Enabled: true" mybucket

# List all versions of objects in mybucket
swift list --versions mybucket

# List all versions of objects in mybucket in JSON for later processing
swift list --json --versions mybucket > /tmp/all-objects.json

# Download
swift download mybucket myobject

# Download a specific version of an object in mybucket
# VERSION_ID is in the form of a timestamp in Unix epoch format
VERSION_ID=1681378321.87282
swift download --version-id $VERSION_ID mybucket myobject

# Limit listing operation using the prefix of object names
swift list --prefix 'tux.jpg' mybucket

# Limit delete operation using the prefix of object names
swift delete --prefix 'tux.jpg' mybucket

Umgang mit Object Versioning

Swift bietet die Option mehrere Versionen einer Datei zu behalten. Damit wird bei jeder Änderung der Datei eine Kopie erstellt, die als versteckte Datei im Object Store landet.

Um effizienter arbeiten zu können, zuerst alle Metadaten als JSON-File herunterladen, und dann Queries dagegen laufenzulassen:

swift list --json --versions mybucket > /tmp/all-objects.json

# list all "current" objects
jq '.[]|select(.is_latest == true )' /tmp/all-objects.json

# select all objects with name "urn:oid:10006490"
jq '.[]|select(.name == "urn:oid:10006490" )' /tmp/all-objects.json

Eine komplizierte Abfrage mit Argumenten:

# select the last object with name `objectName` since `restorePIT`
NAME=tux.jpg
jq \
    --arg restorePIT $(date -d "2023-07-07T11:28:52" +%s) \
    --arg objectName $NAME '[.[]|select((.name == $objectName ) and ((.version_id)|tonumber <= $restorePIT))]|max_by(.version_id|tonumber)' \
    /tmp/all-objects.json

Erklärung des JQ-Statements '[.[]|select((.name == $objectName ) and ((.version_id)|tonumber <= $restorePIT))]|max_by(.version_id|tonumber)':

  • ' opening quote for jq filter

  • [: pack the resulting object of filter in an array (is needed because max_by function needs an array as input)

  • .[]: list all objects from the input array (contents of /tmp/all-objects.json)

  • |select((.name == $objectName ) and ((.version_id)|tonumber <= $restorePIT)):

    • select all the objects where .name == objectName (--arg objectName)

    • and

    • of which the .version_id converted to a number is smaller or equal to restorePIT (--arg restorePIT)

  • ]: closing bracket of the array (is needed because max_by function needs an array as input)

  • |max_by(.version_id|tonumber): select the object of which the .version_id converted to a number is the highest of the input array of objects

  • ': closing quote for jq filter

Built on 2024-04-18