.. code-block:: python
- import cloudfiles
- username = 'account_name:username'
- api_key = 'your_api_key'
+ import swiftclient
+ user = 'account_name:username'
+ key = 'your_api_key'
- conn = cloudfiles.get_connection(
- username=username,
- api_key=api_key,
+ conn = swiftclient.Connection(
+ user=user,
+ key=key,
authurl='https://objects.dreamhost.com/auth',
)
.. code-block:: python
- container = conn.create_container('my-new-container')
-
+ container_name = 'my-new-container'
+ conn.put_container(container_name)
+
Create an Object
================
.. code-block:: python
- obj = container.create_object('hello.txt')
- obj.content_type = 'text/plain'
- obj.load_from_filename('./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')
+
List Owned Containers
=====================
.. code-block:: python
- for container in conn.get_all_containers():
- print container.name
+ for container in conn.get_account()[1]:
+ print container['name']
The output will look something like this::
List a Container's Content
==========================
-This gets a list of objects in the container, and prints out each
+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 obj in container.get_objects():
- print "{0}\t{1}\t{2}".format(obj.name, obj.size, obj.last_modified)
+ 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::
.. code-block:: python
- obj = container.get_object('hello.txt')
- obj.save_to_filename('./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])
Delete an Object
.. code-block:: python
- container.delete_object('goodbye.txt')
-
+ conn.delete_object(container_name, 'hello.txt')
+
Delete a Container
==================
.. code-block:: python
- conn.delete_container(container.name)
+ conn.delete_container(container_name)