blob: 615a59dad8efb287d7f0b39af83c10d63b75bc2c [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package objects
2
3import (
4 "fmt"
Jon Perritt816d2a02014-03-11 20:49:46 -05005 "github.com/racker/perigee"
Jon Perrittb6b1d022014-04-14 21:50:45 -05006 storage "github.com/rackspace/gophercloud/openstack/storage/v1"
Jon Perritt816d2a02014-03-11 20:49:46 -05007 "github.com/rackspace/gophercloud/openstack/utils"
Jon Perritteb575642014-04-24 15:16:31 -05008 "net/http"
Jon Perritt816d2a02014-03-11 20:49:46 -05009)
10
Jon Perritteb575642014-04-24 15:16:31 -050011type ListResult *http.Response
12type DownloadResult *http.Response
13type GetResult *http.Response
Jon Perritt816d2a02014-03-11 20:49:46 -050014
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
Jon Perritteb575642014-04-24 15:16:31 -050017// response to the ExtractInfo or ExtractNames function, respectively.
Jon Perritt816d2a02014-03-11 20:49:46 -050018func 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{
Jon Perritt816d2a02014-03-11 20:49:46 -050034 MoreHeaders: h,
35 OkCodes: []int{200, 204},
36 Accept: contentType,
37 })
Jon Perritteb575642014-04-24 15:16:31 -050038 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -050039}
40
41// Download is a function that retrieves the content and metadata for an object.
Jon Perritteb575642014-04-24 15:16:31 -050042// To extract just the content, pass the DownloadResult response to the ExtractContent
Jon Perritt816d2a02014-03-11 20:49:46 -050043// function.
44func Download(c *storage.Client, opts DownloadOpts) (DownloadResult, error) {
45 h, err := c.GetHeaders()
46 if err != nil {
47 return nil, err
48 }
49
50 for k, v := range opts.Headers {
51 h[k] = v
52 }
53
54 query := utils.BuildQuery(opts.Params)
55
56 url := c.GetObjectURL(opts.Container, opts.Name) + query
57 resp, err := perigee.Request("GET", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050058 MoreHeaders: h,
59 OkCodes: []int{200},
60 })
Jon Perritteb575642014-04-24 15:16:31 -050061 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -050062}
63
64// Create is a function that creates a new object or replaces an existing object.
65func Create(c *storage.Client, opts CreateOpts) error {
66 var reqBody []byte
67
68 h, err := c.GetHeaders()
69 if err != nil {
70 return err
71 }
72
73 for k, v := range opts.Headers {
74 h[k] = v
75 }
76
77 for k, v := range opts.Metadata {
78 h["X-Object-Meta-"+k] = v
79 }
80
81 query := utils.BuildQuery(opts.Params)
82
83 content := opts.Content
84 if content != nil {
85 reqBody = make([]byte, content.Len())
86 _, err = content.Read(reqBody)
87 if err != nil {
88 return err
89 }
90 }
91
92 url := c.GetObjectURL(opts.Container, opts.Name) + query
93 _, err = perigee.Request("PUT", url, perigee.Options{
94 ReqBody: reqBody,
95 MoreHeaders: h,
96 OkCodes: []int{201},
97 })
98 return err
99}
100
101// Copy is a function that copies one object to another.
102func Copy(c *storage.Client, opts CopyOpts) error {
103 h, err := c.GetHeaders()
104 if err != nil {
105 return err
106 }
107
108 for k, v := range opts.Metadata {
109 h["X-Object-Meta-"+k] = v
110 }
111
112 h["Destination"] = fmt.Sprintf("/%s/%s", opts.NewContainer, opts.NewName)
113
114 url := c.GetObjectURL(opts.Container, opts.Name)
115 _, err = perigee.Request("COPY", url, perigee.Options{
116 MoreHeaders: h,
117 OkCodes: []int{201},
118 })
119 return err
120}
121
122// Delete is a function that deletes an object.
123func Delete(c *storage.Client, opts DeleteOpts) error {
124 h, err := c.GetHeaders()
125 if err != nil {
126 return err
127 }
128
129 query := utils.BuildQuery(opts.Params)
130
131 url := c.GetObjectURL(opts.Container, opts.Name) + query
132 _, err = perigee.Request("DELETE", url, perigee.Options{
133 MoreHeaders: h,
134 OkCodes: []int{204},
135 })
136 return err
137}
138
139// Get is a function that retrieves the metadata of an object. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -0500140// metadata, pass the GetResult response to the ExtractMetadata function.
Jon Perritt816d2a02014-03-11 20:49:46 -0500141func Get(c *storage.Client, opts GetOpts) (GetResult, error) {
142 h, err := c.GetHeaders()
143 if err != nil {
144 return nil, err
145 }
146
147 for k, v := range opts.Headers {
148 h[k] = v
149 }
150
151 url := c.GetObjectURL(opts.Container, opts.Name)
152 resp, err := perigee.Request("HEAD", url, perigee.Options{
153 MoreHeaders: h,
154 OkCodes: []int{204},
155 })
Jon Perritteb575642014-04-24 15:16:31 -0500156 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -0500157}
158
159// Update is a function that creates, updates, or deletes an object's metadata.
160func Update(c *storage.Client, opts UpdateOpts) error {
161 h, err := c.GetHeaders()
162 if err != nil {
163 return err
164 }
165
166 for k, v := range opts.Headers {
167 h[k] = v
168 }
169
170 for k, v := range opts.Metadata {
171 h["X-Object-Meta-"+k] = v
172 }
173
174 url := c.GetObjectURL(opts.Container, opts.Name)
175 _, err = perigee.Request("POST", url, perigee.Options{
176 MoreHeaders: h,
177 OkCodes: []int{202, 204},
178 })
179 return err
180}