注意

本文档适用于 Ceph 的开发版本。

Python Swift 示例

创建连接

这将创建一个连接,以便您可以与服务器交互

import swiftclient
user = 'account_name:username'
key = 'your_api_key'

conn = swiftclient.Connection(
        user=user,
        key=key,
        authurl='https://objects.dreamhost.com/auth',
)

创建容器

这将创建一个名为 my-new-container 的新容器

container_name = 'my-new-container'
conn.put_container(container_name)

创建对象

这会从名为 my_hello.txt 的文件创建文件 hello.txt

with open('hello.txt', 'r') as hello_file:
        conn.put_object(container_name, 'hello.txt',
                                        contents= hello_file.read(),
                                        content_type='text/plain')

列出拥有的容器

这会获取您拥有的容器列表,并打印出容器名称

for container in conn.get_account()[1]:
        print(container['name'])

输出将类似于

mahbuckat1
mahbuckat2
mahbuckat3

列出容器内容

这会获取容器中对象的列表,并打印出每个对象的名称、文件大小和上次修改日期

for data in conn.get_container(container_name)[1]:
        print('{0}\t{1}\t{2}'.format(data['name'], data['bytes'], data['last_modified']))

输出将类似于

myphoto1.jpg 251262  2011-08-08T21:35:48.000Z
myphoto2.jpg 262518  2011-08-08T21:38:01.000Z

检索对象

这会下载对象 hello.txt 并将其保存到 ./my_hello.txt

obj_tuple = conn.get_object(container_name, 'hello.txt')
with open('my_hello.txt', 'w') as my_hello:
        my_hello.write(obj_tuple[1])

删除对象

这会删除对象 hello.txt

conn.delete_object(container_name, 'hello.txt')

删除容器

注意

容器必须为空!否则请求将无法工作!

conn.delete_container(container_name)

由 Ceph 基金会为您呈现

Ceph 文档是由非营利性 Ceph 基金会 资助和托管的社区资源。如果您希望支持这项工作和我们的其他努力,请考虑 立即加入