]> git.proxmox.com Git - ceph.git/blame - ceph/doc/radosgw/swift/python.rst
import quincy beta 17.1.0
[ceph.git] / ceph / doc / radosgw / swift / python.rst
CommitLineData
7c673cae
FG
1.. _python_swift:
2
3=====================
4Python Swift Examples
5=====================
6
7Create a Connection
8===================
9
10This creates a connection so that you can interact with the server:
11
12.. code-block:: python
13
14 import swiftclient
15 user = 'account_name:username'
16 key = 'your_api_key'
17
18 conn = swiftclient.Connection(
19 user=user,
20 key=key,
21 authurl='https://objects.dreamhost.com/auth',
22 )
23
24
25Create a Container
26==================
27
28This creates a new container called ``my-new-container``:
29
30.. code-block:: python
31
32 container_name = 'my-new-container'
33 conn.put_container(container_name)
34
35
36Create an Object
37================
38
39This creates a file ``hello.txt`` from the file named ``my_hello.txt``:
40
41.. code-block:: python
42
43 with open('hello.txt', 'r') as hello_file:
44 conn.put_object(container_name, 'hello.txt',
45 contents= hello_file.read(),
46 content_type='text/plain')
47
48
49List Owned Containers
50=====================
51
52This gets a list of containers that you own, and prints out the container name:
53
54.. code-block:: python
55
56 for container in conn.get_account()[1]:
20effc67 57 print(container['name'])
7c673cae
FG
58
59The output will look something like this::
60
61 mahbuckat1
62 mahbuckat2
63 mahbuckat3
64
65List a Container's Content
66==========================
67
68This gets a list of objects in the container, and prints out each
69object's name, the file size, and last modified date:
70
71.. code-block:: python
72
73 for data in conn.get_container(container_name)[1]:
20effc67 74 print('{0}\t{1}\t{2}'.format(data['name'], data['bytes'], data['last_modified']))
7c673cae
FG
75
76The output will look something like this::
77
78 myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
79 myphoto2.jpg 262518 2011-08-08T21:38:01.000Z
80
81
82Retrieve an Object
83==================
84
85This downloads the object ``hello.txt`` and saves it in
86``./my_hello.txt``:
87
88.. code-block:: python
89
90 obj_tuple = conn.get_object(container_name, 'hello.txt')
91 with open('my_hello.txt', 'w') as my_hello:
92 my_hello.write(obj_tuple[1])
93
94
95Delete an Object
96================
97
98This deletes the object ``hello.txt``:
99
100.. code-block:: python
101
102 conn.delete_object(container_name, 'hello.txt')
103
104Delete a Container
105==================
106
107.. note::
108
109 The container must be empty! Otherwise the request won't work!
110
111.. code-block:: python
112
113 conn.delete_container(container_name)
114