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