Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / doc / radosgw / swift / ruby.rst
1 .. _ruby_swift:
2
3 =====================
4  Ruby 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:: ruby
13
14         require 'cloudfiles'
15         username = 'account_name:user_name'
16         api_key  = 'your_secret_key'
17
18         conn = CloudFiles::Connection.new(
19                 :username => username,
20                 :api_key  => api_key,
21                 :auth_url => 'http://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:: ruby
31
32         container = conn.create_container('my-new-container')
33
34
35 Create an Object
36 ================
37
38 This creates a file ``hello.txt`` from the file named ``my_hello.txt``
39
40 .. code-block:: ruby
41
42         obj = container.create_object('hello.txt')
43         obj.load_from_filename('./my_hello.txt')
44         obj.content_type = 'text/plain'
45
46
47
48 List Owned Containers
49 =====================
50
51 This gets a list of Containers that you own, and also prints out 
52 the container name:
53
54 .. code-block:: ruby
55
56         conn.containers.each do |container|
57                 puts container
58         end
59
60 The output will look something like this::
61
62    mahbuckat1
63    mahbuckat2
64    mahbuckat3
65
66
67 List a Container's Contents
68 ===========================
69
70 This gets a list of objects in the container, and prints out each 
71 object's name, the file size, and last modified date:
72
73 .. code-block:: ruby
74
75         require 'date'  # not necessary in the next version
76
77         container.objects_detail.each do |name, data|
78                 puts "#{name}\t#{data[:bytes]}\t#{data[:last_modified]}"
79         end
80
81 The output will look something like this::
82
83    myphoto1.jpg 251262  2011-08-08T21:35:48.000Z
84    myphoto2.jpg 262518  2011-08-08T21:38:01.000Z
85
86
87
88 Retrieve an Object
89 ==================
90
91 This downloads the object ``hello.txt`` and saves it in
92 ``./my_hello.txt``:
93
94 .. code-block:: ruby
95
96         obj = container.object('hello.txt')
97         obj.save_to_filename('./my_hello.txt')
98
99
100 Delete an Object
101 ================
102
103 This deletes the object ``goodbye.txt``:
104
105 .. code-block:: ruby
106
107         container.delete_object('goodbye.txt')
108         
109
110 Delete a Container
111 ==================
112
113 .. note::
114
115    The container must be empty! Otherwise the request won't work!
116
117 .. code-block:: ruby
118
119         container.delete_container('my-new-container')