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