summaryrefslogtreecommitdiffstats
path: root/src/ceph/doc/radosgw/swift/python.rst
blob: 28d92d7cc9715d26ab86802ccf23fd93c3f280b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
.. _python_swift:

=====================
Python Swift Examples
=====================

Create a Connection
===================

This creates a connection so that you can interact with the server:

.. code-block:: python

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

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


Create a Container
==================

This creates a new container called ``my-new-container``:

.. code-block:: python

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


Create an Object
================

This creates a file ``hello.txt`` from the file named ``my_hello.txt``:

.. code-block:: python

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


List Owned Containers
=====================

This gets a list of containers that you own, and prints out the container name:

.. code-block:: python

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

The output will look something like this::

   mahbuckat1
   mahbuckat2
   mahbuckat3

List a Container's Content
==========================

This gets a list of objects in the container, and prints out each
object's name, the file size, and last modified date:

.. code-block:: python

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

The output will look something like this::

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


Retrieve an Object
==================

This downloads the object ``hello.txt`` and saves it in
``./my_hello.txt``:

.. code-block:: python

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


Delete an Object
================

This deletes the object ``hello.txt``:

.. code-block:: python

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

Delete a Container
==================

.. note::

   The container must be empty! Otherwise the request won't work!

.. code-block:: python

	conn.delete_container(container_name)