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