注意
本文档适用于 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)
创建对象
这会创建一个文件hello.txt
来自名为my_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')
删除容器
Note
容器必须为空!否则请求将不会工作!
conn.delete_container(container_name)
由 Ceph 基金会带给您
Ceph 文档是一个社区资源,由非盈利的 Ceph 基金会资助和托管Ceph Foundation. 如果您想支持这一点和我们的其他工作,请考虑加入现在加入.