]> git.proxmox.com Git - ceph.git/blob - ceph/doc/radosgw/swift/python.rst
import quincy beta 17.1.0
[ceph.git] / ceph / doc / radosgw / swift / python.rst
1 .. _python_swift:
2
3 =====================
4 Python Swift Examples
5 =====================
6
7 Create a Connection
8 ===================
9
10 This 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
25 Create a Container
26 ==================
27
28 This 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
36 Create an Object
37 ================
38
39 This 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
49 List Owned Containers
50 =====================
51
52 This 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]:
57 print(container['name'])
58
59 The output will look something like this::
60
61 mahbuckat1
62 mahbuckat2
63 mahbuckat3
64
65 List a Container's Content
66 ==========================
67
68 This gets a list of objects in the container, and prints out each
69 object'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]:
74 print('{0}\t{1}\t{2}'.format(data['name'], data['bytes'], data['last_modified']))
75
76 The 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
82 Retrieve an Object
83 ==================
84
85 This 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
95 Delete an Object
96 ================
97
98 This deletes the object ``hello.txt``:
99
100 .. code-block:: python
101
102 conn.delete_object(container_name, 'hello.txt')
103
104 Delete 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