blob: 8f645fc1632c0daefa5dbe13edc06685795d33f8 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package objects
2
3import (
4 "fmt"
5
6 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud/openstack/storage"
8 "github.com/rackspace/gophercloud/openstack/utils"
9)
10
11type ListResult *perigee.Response
12type DownloadResult *perigee.Response
13type GetResult *perigee.Response
14
15// List is a function that retrieves all objects in a container. It also returns the details
16// for the container. To extract only the object information or names, pass the ListResult
17// response to the GetInfo or GetNames function, respectively.
18func List(c *storage.Client, opts ListOpts) (ListResult, error) {
19 contentType := ""
20
21 h, err := c.GetHeaders()
22 if err != nil {
23 return nil, err
24 }
25
26 query := utils.BuildQuery(opts.Params)
27
28 if !opts.Full {
29 contentType = "text/plain"
30 }
31
32 url := c.GetContainerURL(opts.Container) + query
33 resp, err := perigee.Request("GET", url, perigee.Options{
34 Results: true,
35 MoreHeaders: h,
36 OkCodes: []int{200, 204},
37 Accept: contentType,
38 })
39 return resp, err
40}
41
42// Download is a function that retrieves the content and metadata for an object.
43// To extract just the content, pass the DownloadResult response to the GetContent
44// function.
45func Download(c *storage.Client, opts DownloadOpts) (DownloadResult, error) {
46 h, err := c.GetHeaders()
47 if err != nil {
48 return nil, err
49 }
50
51 for k, v := range opts.Headers {
52 h[k] = v
53 }
54
55 query := utils.BuildQuery(opts.Params)
56
57 url := c.GetObjectURL(opts.Container, opts.Name) + query
58 resp, err := perigee.Request("GET", url, perigee.Options{
59 Results: true,
60 MoreHeaders: h,
61 OkCodes: []int{200},
62 })
63 return resp, err
64}
65
66// Create is a function that creates a new object or replaces an existing object.
67func Create(c *storage.Client, opts CreateOpts) error {
68 var reqBody []byte
69
70 h, err := c.GetHeaders()
71 if err != nil {
72 return err
73 }
74
75 for k, v := range opts.Headers {
76 h[k] = v
77 }
78
79 for k, v := range opts.Metadata {
80 h["X-Object-Meta-"+k] = v
81 }
82
83 query := utils.BuildQuery(opts.Params)
84
85 content := opts.Content
86 if content != nil {
87 reqBody = make([]byte, content.Len())
88 _, err = content.Read(reqBody)
89 if err != nil {
90 return err
91 }
92 }
93
94 url := c.GetObjectURL(opts.Container, opts.Name) + query
95 _, err = perigee.Request("PUT", url, perigee.Options{
96 ReqBody: reqBody,
97 MoreHeaders: h,
98 OkCodes: []int{201},
99 })
100 return err
101}
102
103// Copy is a function that copies one object to another.
104func Copy(c *storage.Client, opts CopyOpts) error {
105 h, err := c.GetHeaders()
106 if err != nil {
107 return err
108 }
109
110 for k, v := range opts.Metadata {
111 h["X-Object-Meta-"+k] = v
112 }
113
114 h["Destination"] = fmt.Sprintf("/%s/%s", opts.NewContainer, opts.NewName)
115
116 url := c.GetObjectURL(opts.Container, opts.Name)
117 _, err = perigee.Request("COPY", url, perigee.Options{
118 MoreHeaders: h,
119 OkCodes: []int{201},
120 })
121 return err
122}
123
124// Delete is a function that deletes an object.
125func Delete(c *storage.Client, opts DeleteOpts) error {
126 h, err := c.GetHeaders()
127 if err != nil {
128 return err
129 }
130
131 query := utils.BuildQuery(opts.Params)
132
133 url := c.GetObjectURL(opts.Container, opts.Name) + query
134 _, err = perigee.Request("DELETE", url, perigee.Options{
135 MoreHeaders: h,
136 OkCodes: []int{204},
137 })
138 return err
139}
140
141// Get is a function that retrieves the metadata of an object. To extract just the custom
142// metadata, pass the GetResult response to the GetMetadata function.
143func Get(c *storage.Client, opts GetOpts) (GetResult, error) {
144 h, err := c.GetHeaders()
145 if err != nil {
146 return nil, err
147 }
148
149 for k, v := range opts.Headers {
150 h[k] = v
151 }
152
153 url := c.GetObjectURL(opts.Container, opts.Name)
154 resp, err := perigee.Request("HEAD", url, perigee.Options{
155 MoreHeaders: h,
156 OkCodes: []int{204},
157 })
158 return resp, err
159}
160
161// Update is a function that creates, updates, or deletes an object's metadata.
162func Update(c *storage.Client, opts UpdateOpts) error {
163 h, err := c.GetHeaders()
164 if err != nil {
165 return err
166 }
167
168 for k, v := range opts.Headers {
169 h[k] = v
170 }
171
172 for k, v := range opts.Metadata {
173 h["X-Object-Meta-"+k] = v
174 }
175
176 url := c.GetObjectURL(opts.Container, opts.Name)
177 _, err = perigee.Request("POST", url, perigee.Options{
178 MoreHeaders: h,
179 OkCodes: []int{202, 204},
180 })
181 return err
182}