Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 5 | "io" |
| 6 | "time" |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 7 | |
| 8 | "github.com/racker/perigee" |
| 9 | "github.com/rackspace/gophercloud" |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 11 | ) |
| 12 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 13 | // ListOptsBuilder allows extensions to add additional parameters to the List |
| 14 | // request. |
| 15 | type ListOptsBuilder interface { |
| 16 | ToObjectListParams() (bool, string, error) |
| 17 | } |
| 18 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 19 | // ListOpts is a structure that holds parameters for listing objects. |
| 20 | type ListOpts struct { |
| 21 | Full bool |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 22 | Limit int `q:"limit"` |
| 23 | Marker string `q:"marker"` |
| 24 | EndMarker string `q:"end_marker"` |
| 25 | Format string `q:"format"` |
| 26 | Prefix string `q:"prefix"` |
| 27 | Delimiter string `q:"delimiter"` |
| 28 | Path string `q:"path"` |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 29 | } |
| 30 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 31 | // ToObjectListParams formats a ListOpts into a query string and boolean |
| 32 | // representing whether to list complete information for each object. |
| 33 | func (opts ListOpts) ToObjectListParams() (bool, string, error) { |
| 34 | q, err := gophercloud.BuildQueryString(opts) |
| 35 | if err != nil { |
| 36 | return false, "", err |
| 37 | } |
| 38 | return opts.Full, q.String(), nil |
| 39 | } |
| 40 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 41 | // List is a function that retrieves all objects in a container. It also returns the details |
| 42 | // for the container. To extract only the object information or names, pass the ListResult |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 43 | // response to the ExtractInfo or ExtractNames function, respectively. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 44 | func List(c *gophercloud.ServiceClient, containerName string, opts ListOptsBuilder) pagination.Pager { |
| 45 | headers := map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"} |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 46 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 47 | url := listURL(c, containerName) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 48 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 49 | full, query, err := opts.ToObjectListParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 50 | if err != nil { |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 51 | return pagination.Pager{Err: err} |
| 52 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 53 | url += query |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 54 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 55 | if full { |
| 56 | headers = map[string]string{"Accept": "application/json", "Content-Type": "application/json"} |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 57 | } |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 60 | createPage := func(r pagination.PageResult) pagination.Page { |
| 61 | p := ObjectPage{pagination.MarkerPageBase{PageResult: r}} |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 62 | p.MarkerPageBase.Owner = p |
| 63 | return p |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 66 | pager := pagination.NewPager(c, url, createPage) |
| 67 | pager.Headers = headers |
| 68 | return pager |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 71 | // DownloadOptsBuilder allows extensions to add additional parameters to the |
| 72 | // Download request. |
| 73 | type DownloadOptsBuilder interface { |
| 74 | ToObjectDownloadParams() (map[string]string, string, error) |
| 75 | } |
| 76 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 77 | // DownloadOpts is a structure that holds parameters for downloading an object. |
| 78 | type DownloadOpts struct { |
| 79 | IfMatch string `h:"If-Match"` |
| 80 | IfModifiedSince time.Time `h:"If-Modified-Since"` |
| 81 | IfNoneMatch string `h:"If-None-Match"` |
| 82 | IfUnmodifiedSince time.Time `h:"If-Unmodified-Since"` |
| 83 | Range string `h:"Range"` |
| 84 | Expires string `q:"expires"` |
| 85 | MultipartManifest string `q:"multipart-manifest"` |
| 86 | Signature string `q:"signature"` |
| 87 | } |
| 88 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 89 | // ToObjectDownloadParams formats a DownloadOpts into a query string and map of |
| 90 | // headers. |
| 91 | func (opts ListOpts) ToObjectDownloadParams() (map[string]string, string, error) { |
| 92 | q, err := gophercloud.BuildQueryString(opts) |
| 93 | if err != nil { |
| 94 | return nil, "", err |
| 95 | } |
| 96 | h, err := gophercloud.BuildHeaders(opts) |
| 97 | if err != nil { |
| 98 | return nil, q.String(), err |
| 99 | } |
| 100 | return h, q.String(), nil |
| 101 | } |
| 102 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 103 | // Download is a function that retrieves the content and metadata for an object. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 104 | // To extract just the content, pass the DownloadResult response to the |
| 105 | // ExtractContent function. |
| 106 | func Download(c *gophercloud.ServiceClient, containerName, objectName string, opts DownloadOptsBuilder) DownloadResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 107 | var res DownloadResult |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 108 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 109 | url := downloadURL(c, containerName, objectName) |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 110 | h := c.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 111 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 112 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 113 | headers, query, err := opts.ToObjectDownloadParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 114 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 115 | res.Err = err |
| 116 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | for k, v := range headers { |
| 120 | h[k] = v |
| 121 | } |
| 122 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 123 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 126 | resp, err := perigee.Request("GET", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 127 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 128 | OkCodes: []int{200}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 129 | }) |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 130 | |
| 131 | res.Body = resp.HttpResponse.Body |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 132 | res.Err = err |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 133 | res.Header = resp.HttpResponse.Header |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 134 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 135 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 138 | // CreateOptsBuilder allows extensions to add additional parameters to the |
| 139 | // Create request. |
| 140 | type CreateOptsBuilder interface { |
| 141 | ToObjectCreateParams() (map[string]string, string, error) |
| 142 | } |
| 143 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 144 | // CreateOpts is a structure that holds parameters for creating an object. |
| 145 | type CreateOpts struct { |
| 146 | Metadata map[string]string |
| 147 | ContentDisposition string `h:"Content-Disposition"` |
| 148 | ContentEncoding string `h:"Content-Encoding"` |
| 149 | ContentLength int `h:"Content-Length"` |
| 150 | ContentType string `h:"Content-Type"` |
| 151 | CopyFrom string `h:"X-Copy-From"` |
| 152 | DeleteAfter int `h:"X-Delete-After"` |
| 153 | DeleteAt int `h:"X-Delete-At"` |
| 154 | DetectContentType string `h:"X-Detect-Content-Type"` |
| 155 | ETag string `h:"ETag"` |
| 156 | IfNoneMatch string `h:"If-None-Match"` |
| 157 | ObjectManifest string `h:"X-Object-Manifest"` |
| 158 | TransferEncoding string `h:"Transfer-Encoding"` |
| 159 | Expires string `q:"expires"` |
| 160 | MultipartManifest string `q:"multiple-manifest"` |
| 161 | Signature string `q:"signature"` |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 162 | } |
| 163 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 164 | // ToObjectCreateParams formats a CreateOpts into a query string and map of |
| 165 | // headers. |
| 166 | func (opts CreateOpts) ToObjectCreateParams() (map[string]string, string, error) { |
| 167 | q, err := gophercloud.BuildQueryString(opts) |
| 168 | if err != nil { |
| 169 | return nil, "", err |
| 170 | } |
| 171 | h, err := gophercloud.BuildHeaders(opts) |
| 172 | if err != nil { |
| 173 | return nil, q.String(), err |
| 174 | } |
| 175 | |
| 176 | for k, v := range opts.Metadata { |
| 177 | h["X-Object-Meta-"+k] = v |
| 178 | } |
| 179 | |
| 180 | return h, q.String(), nil |
| 181 | } |
| 182 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 183 | // Create is a function that creates a new object or replaces an existing object. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 184 | func Create(c *gophercloud.ServiceClient, containerName, objectName string, content io.Reader, opts CreateOptsBuilder) CreateResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 185 | var res CreateResult |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 186 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 187 | url := createURL(c, containerName, objectName) |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 188 | h := c.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 189 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 190 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 191 | headers, query, err := opts.ToObjectCreateParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 192 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 193 | res.Err = err |
| 194 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 195 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 196 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 197 | for k, v := range headers { |
| 198 | h[k] = v |
| 199 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 200 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 201 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 202 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 203 | |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 204 | contentType := h["Content-Type"] |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 205 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 206 | resp, err := perigee.Request("PUT", url, perigee.Options{ |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 207 | ContentType: contentType, |
| 208 | ReqBody: content, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 209 | MoreHeaders: h, |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 210 | OkCodes: []int{201, 202}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 211 | }) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 212 | res.Header = resp.HttpResponse.Header |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 213 | res.Err = err |
| 214 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 215 | } |
| 216 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 217 | // CopyOptsBuilder allows extensions to add additional parameters to the |
| 218 | // Copy request. |
| 219 | type CopyOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 220 | ToObjectCopyMap() (map[string]string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // CopyOpts is a structure that holds parameters for copying one object to |
| 224 | // another. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 225 | type CopyOpts struct { |
| 226 | Metadata map[string]string |
| 227 | ContentDisposition string `h:"Content-Disposition"` |
| 228 | ContentEncoding string `h:"Content-Encoding"` |
| 229 | ContentType string `h:"Content-Type"` |
| 230 | Destination string `h:"Destination,required"` |
| 231 | } |
| 232 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 233 | // ToObjectCopyMap formats a CopyOpts into a map of headers. |
| 234 | func (opts CopyOpts) ToObjectCopyMap() (map[string]string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 235 | if opts.Destination == "" { |
| 236 | return nil, fmt.Errorf("Required CopyOpts field 'Destination' not set.") |
| 237 | } |
| 238 | h, err := gophercloud.BuildHeaders(opts) |
| 239 | if err != nil { |
| 240 | return nil, err |
| 241 | } |
| 242 | for k, v := range opts.Metadata { |
| 243 | h["X-Object-Meta-"+k] = v |
| 244 | } |
| 245 | return h, nil |
| 246 | } |
| 247 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 248 | // Copy is a function that copies one object to another. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 249 | func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts CopyOptsBuilder) CopyResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 250 | var res CopyResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 251 | h := c.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 252 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 253 | headers, err := opts.ToObjectCopyMap() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 254 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 255 | res.Err = err |
| 256 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 257 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 258 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 259 | for k, v := range headers { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 260 | h[k] = v |
| 261 | } |
| 262 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 263 | url := copyURL(c, containerName, objectName) |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 264 | resp, err := perigee.Request("COPY", url, perigee.Options{ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 265 | MoreHeaders: h, |
| 266 | OkCodes: []int{201}, |
| 267 | }) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 268 | res.Header = resp.HttpResponse.Header |
jrperritt | f7a8e28 | 2014-10-28 10:00:48 -0500 | [diff] [blame^] | 269 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 270 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 271 | } |
| 272 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 273 | // DeleteOptsBuilder allows extensions to add additional parameters to the |
| 274 | // Delete request. |
| 275 | type DeleteOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 276 | ToObjectDeleteQuery() (string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 277 | } |
| 278 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 279 | // DeleteOpts is a structure that holds parameters for deleting an object. |
| 280 | type DeleteOpts struct { |
| 281 | MultipartManifest string `q:"multipart-manifest"` |
| 282 | } |
| 283 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 284 | // ToObjectDeleteQuery formats a DeleteOpts into a query string. |
| 285 | func (opts DeleteOpts) ToObjectDeleteQuery() (string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 286 | q, err := gophercloud.BuildQueryString(opts) |
| 287 | if err != nil { |
| 288 | return "", err |
| 289 | } |
| 290 | return q.String(), nil |
| 291 | } |
| 292 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 293 | // Delete is a function that deletes an object. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 294 | func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts DeleteOptsBuilder) DeleteResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 295 | var res DeleteResult |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 296 | url := deleteURL(c, containerName, objectName) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 297 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 298 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 299 | query, err := opts.ToObjectDeleteQuery() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 300 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 301 | res.Err = err |
| 302 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 303 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 304 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame] | 307 | resp, err := perigee.Request("DELETE", url, perigee.Options{ |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 308 | MoreHeaders: c.AuthenticatedHeaders(), |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 309 | OkCodes: []int{204}, |
| 310 | }) |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame] | 311 | res.Header = resp.HttpResponse.Header |
| 312 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 313 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 314 | } |
| 315 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 316 | // GetOptsBuilder allows extensions to add additional parameters to the |
| 317 | // Get request. |
| 318 | type GetOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 319 | ToObjectGetQuery() (string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 320 | } |
| 321 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 322 | // GetOpts is a structure that holds parameters for getting an object's metadata. |
| 323 | type GetOpts struct { |
| 324 | Expires string `q:"expires"` |
| 325 | Signature string `q:"signature"` |
| 326 | } |
| 327 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 328 | // ToObjectGetQuery formats a GetOpts into a query string. |
| 329 | func (opts GetOpts) ToObjectGetQuery() (string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 330 | q, err := gophercloud.BuildQueryString(opts) |
| 331 | if err != nil { |
| 332 | return "", err |
| 333 | } |
| 334 | return q.String(), nil |
| 335 | } |
| 336 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 337 | // Get is a function that retrieves the metadata of an object. To extract just the custom |
| 338 | // metadata, pass the GetResult response to the ExtractMetadata function. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 339 | func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts GetOptsBuilder) GetResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 340 | var res GetResult |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 341 | url := getURL(c, containerName, objectName) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 342 | |
| 343 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 344 | query, err := opts.ToObjectGetQuery() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 345 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 346 | res.Err = err |
| 347 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 348 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 349 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 350 | } |
| 351 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 352 | resp, err := perigee.Request("HEAD", url, perigee.Options{ |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 353 | MoreHeaders: c.AuthenticatedHeaders(), |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 354 | OkCodes: []int{200, 204}, |
| 355 | }) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 356 | res.Header = resp.HttpResponse.Header |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 357 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 358 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 359 | } |
| 360 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 361 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 362 | // Update request. |
| 363 | type UpdateOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 364 | ToObjectUpdateMap() (map[string]string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 365 | } |
| 366 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 367 | // UpdateOpts is a structure that holds parameters for updating, creating, or deleting an |
| 368 | // object's metadata. |
| 369 | type UpdateOpts struct { |
| 370 | Metadata map[string]string |
| 371 | ContentDisposition string `h:"Content-Disposition"` |
| 372 | ContentEncoding string `h:"Content-Encoding"` |
| 373 | ContentType string `h:"Content-Type"` |
| 374 | DeleteAfter int `h:"X-Delete-After"` |
| 375 | DeleteAt int `h:"X-Delete-At"` |
| 376 | DetectContentType bool `h:"X-Detect-Content-Type"` |
| 377 | } |
| 378 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 379 | // ToObjectUpdateMap formats a UpdateOpts into a map of headers. |
| 380 | func (opts UpdateOpts) ToObjectUpdateMap() (map[string]string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 381 | h, err := gophercloud.BuildHeaders(opts) |
| 382 | if err != nil { |
| 383 | return nil, err |
| 384 | } |
| 385 | for k, v := range opts.Metadata { |
| 386 | h["X-Object-Meta-"+k] = v |
| 387 | } |
| 388 | return h, nil |
| 389 | } |
| 390 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 391 | // Update is a function that creates, updates, or deletes an object's metadata. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 392 | func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts UpdateOptsBuilder) UpdateResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 393 | var res UpdateResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 394 | h := c.AuthenticatedHeaders() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 395 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 396 | if opts != nil { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 397 | headers, err := opts.ToObjectUpdateMap() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 398 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 399 | res.Err = err |
| 400 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 401 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 402 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 403 | for k, v := range headers { |
| 404 | h[k] = v |
| 405 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 406 | } |
| 407 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 408 | url := updateURL(c, containerName, objectName) |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 409 | resp, err := perigee.Request("POST", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 410 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 411 | OkCodes: []int{202}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 412 | }) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 413 | res.Header = resp.HttpResponse.Header |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 414 | res.Err = err |
| 415 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 416 | } |