Object Storage using the Python SDK

 

See Python SDK for how to set up using the Python SDK.

Containers

Containers for an account can be interacted with using a Connection instance, conn, and the Object Store service. Some examples are given below.

Note

Containers can typically be passed into functions using either a Container instance or the container name as a string.

Listing containers for an account:

for cont in conn.object_store.containers(): print(cont)

Creating a new container:

cont = conn.object_store.create_container("CONTAINER_1")

Deleting a container using its name:

conn.object_store.delete_container("CONTAINER_1")

Note

The container must be empty before deletion. This command will not raise an error if the container specified is not found unless ignore_missing is set to False.

Getting metadata for a container by passing a Container instance:

Setting system metadata for a container, such as the read and write access, as well as an example custom property, by passing a Container instance:

Note

Deleting metadata for a container by passing a Container instance:

Note

Alternatively, set_container_metadata can be used with values set to "" to delete custom and system metadata.

Note

Objects

Objects in a container can be interacted with using a Connection instance, conn, and the Object Store service. Some examples are given below.

Note

Listing objects in a container by passing the container name:

In the example above, objs is a generator object. Specific Object instances can be obtained from this in a number of ways, such as list comprehension:

Objects can also be accessed directly using the container name and file name to return an Object instance:

Equivalently:

Note

Specific objects can also be accessed via a Connection instance by passing the container name and file name. This will return a tuple, containing (headers, body) for the object specified:

Similarly, using a Connection instance, container name and file name, a Response object can be returned, which stores the object headers and content as attributes:

Getting metadata for a container using an Object instance (in the form of either obj_1 or obj_2):

Note

Downloading an object’s contents using an Object instance (in the form of either obj_1 or obj_2):

Alternatively, downloading contents using the Response object from above:

In the two examples above, file_1 will store the file contents as a bytes object. This can be written out in a number of ways, such as:

Saving contents directly, without storing an intermediate Object or Response object:

Uploading a new object:

Deleting an object using the container and file names:

Note

Setting system and custom metadata for an object by passing an Object instance:

Note

Deleting custom metadata for an object using the file and container names:

Note

Note

When deleting custom metadata, the key should be in lower case, and underscores, ‘_’, in the original key name should be replaced with dashes, ‘-‘.

Swiftclient

An alternative to openstacksdk is swiftclient, which comprises a command line tool (see Swift CLI) and two separate APIs, SwiftService and Connection, for accessing swift programmatically.

This can be installed using:

SwiftService

Below are two examples to illustrate the use of the swiftclient.SwiftService API.

Note

Listing containers for your account:

Listing and downloading all text files in a container:

Warning

Connection

Below are two examples to illustrate the use of the swiftclient.Connection API.

Note

Listing the response headers and containers for your account:

Deleting an object:

References