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, |
Ash Wilson | 45e3434 | 2015-01-23 14:25:34 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Ash Wilson | 322a7e6 | 2015-02-12 16:25:26 -0500 | [diff] [blame] | 216 | resp, err := c.Request("PUT", url, ropts) |
| 217 | res.Header = resp.Header |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 218 | res.Err = err |
| 219 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 220 | } |
| 221 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 222 | // CopyOptsBuilder allows extensions to add additional parameters to the |
| 223 | // Copy request. |
| 224 | type CopyOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 225 | ToObjectCopyMap() (map[string]string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // CopyOpts is a structure that holds parameters for copying one object to |
| 229 | // another. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 230 | type CopyOpts struct { |
| 231 | Metadata map[string]string |
| 232 | ContentDisposition string `h:"Content-Disposition"` |
| 233 | ContentEncoding string `h:"Content-Encoding"` |
| 234 | ContentType string `h:"Content-Type"` |
| 235 | Destination string `h:"Destination,required"` |
| 236 | } |
| 237 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 238 | // ToObjectCopyMap formats a CopyOpts into a map of headers. |
| 239 | func (opts CopyOpts) ToObjectCopyMap() (map[string]string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 240 | if opts.Destination == "" { |
| 241 | return nil, fmt.Errorf("Required CopyOpts field 'Destination' not set.") |
| 242 | } |
| 243 | h, err := gophercloud.BuildHeaders(opts) |
| 244 | if err != nil { |
| 245 | return nil, err |
| 246 | } |
| 247 | for k, v := range opts.Metadata { |
| 248 | h["X-Object-Meta-"+k] = v |
| 249 | } |
| 250 | return h, nil |
| 251 | } |
| 252 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 253 | // Copy is a function that copies one object to another. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 254 | func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts CopyOptsBuilder) CopyResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 255 | var res CopyResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 256 | h := c.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 257 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 258 | headers, err := opts.ToObjectCopyMap() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 259 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 260 | res.Err = err |
| 261 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 262 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 263 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 264 | for k, v := range headers { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 265 | h[k] = v |
| 266 | } |
| 267 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 268 | url := copyURL(c, containerName, objectName) |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 269 | resp, err := c.Request("COPY", url, gophercloud.RequestOpts{ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 270 | MoreHeaders: h, |
| 271 | OkCodes: []int{201}, |
| 272 | }) |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 273 | res.Header = resp.Header |
jrperritt | f7a8e28 | 2014-10-28 10:00:48 -0500 | [diff] [blame] | 274 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 275 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 278 | // DeleteOptsBuilder allows extensions to add additional parameters to the |
| 279 | // Delete request. |
| 280 | type DeleteOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 281 | ToObjectDeleteQuery() (string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 284 | // DeleteOpts is a structure that holds parameters for deleting an object. |
| 285 | type DeleteOpts struct { |
| 286 | MultipartManifest string `q:"multipart-manifest"` |
| 287 | } |
| 288 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 289 | // ToObjectDeleteQuery formats a DeleteOpts into a query string. |
| 290 | func (opts DeleteOpts) ToObjectDeleteQuery() (string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 291 | q, err := gophercloud.BuildQueryString(opts) |
| 292 | if err != nil { |
| 293 | return "", err |
| 294 | } |
| 295 | return q.String(), nil |
| 296 | } |
| 297 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 298 | // Delete is a function that deletes an object. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 299 | func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts DeleteOptsBuilder) DeleteResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 300 | var res DeleteResult |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 301 | url := deleteURL(c, containerName, objectName) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 302 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 303 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 304 | query, err := opts.ToObjectDeleteQuery() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 305 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 306 | res.Err = err |
| 307 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 308 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 309 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 310 | } |
| 311 | |
Jamie Hannaford | 1d27afa | 2015-03-24 16:20:45 +0100 | [diff] [blame] | 312 | resp, err := c.Delete(url, nil) |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 313 | res.Header = resp.Header |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame] | 314 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 315 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 316 | } |
| 317 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 318 | // GetOptsBuilder allows extensions to add additional parameters to the |
| 319 | // Get request. |
| 320 | type GetOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 321 | ToObjectGetQuery() (string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 322 | } |
| 323 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 324 | // GetOpts is a structure that holds parameters for getting an object's metadata. |
| 325 | type GetOpts struct { |
| 326 | Expires string `q:"expires"` |
| 327 | Signature string `q:"signature"` |
| 328 | } |
| 329 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 330 | // ToObjectGetQuery formats a GetOpts into a query string. |
| 331 | func (opts GetOpts) ToObjectGetQuery() (string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 332 | q, err := gophercloud.BuildQueryString(opts) |
| 333 | if err != nil { |
| 334 | return "", err |
| 335 | } |
| 336 | return q.String(), nil |
| 337 | } |
| 338 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 339 | // Get is a function that retrieves the metadata of an object. To extract just the custom |
| 340 | // metadata, pass the GetResult response to the ExtractMetadata function. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 341 | func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts GetOptsBuilder) GetResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 342 | var res GetResult |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 343 | url := getURL(c, containerName, objectName) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 344 | |
| 345 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 346 | query, err := opts.ToObjectGetQuery() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 347 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 348 | res.Err = err |
| 349 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 350 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 351 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 352 | } |
| 353 | |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 354 | resp, err := c.Request("HEAD", url, gophercloud.RequestOpts{ |
| 355 | OkCodes: []int{200, 204}, |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 356 | }) |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 357 | res.Header = resp.Header |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 358 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 359 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 360 | } |
| 361 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 362 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 363 | // Update request. |
| 364 | type UpdateOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 365 | ToObjectUpdateMap() (map[string]string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 366 | } |
| 367 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 368 | // UpdateOpts is a structure that holds parameters for updating, creating, or deleting an |
| 369 | // object's metadata. |
| 370 | type UpdateOpts struct { |
| 371 | Metadata map[string]string |
| 372 | ContentDisposition string `h:"Content-Disposition"` |
| 373 | ContentEncoding string `h:"Content-Encoding"` |
| 374 | ContentType string `h:"Content-Type"` |
| 375 | DeleteAfter int `h:"X-Delete-After"` |
| 376 | DeleteAt int `h:"X-Delete-At"` |
| 377 | DetectContentType bool `h:"X-Detect-Content-Type"` |
| 378 | } |
| 379 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 380 | // ToObjectUpdateMap formats a UpdateOpts into a map of headers. |
| 381 | func (opts UpdateOpts) ToObjectUpdateMap() (map[string]string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 382 | h, err := gophercloud.BuildHeaders(opts) |
| 383 | if err != nil { |
| 384 | return nil, err |
| 385 | } |
| 386 | for k, v := range opts.Metadata { |
| 387 | h["X-Object-Meta-"+k] = v |
| 388 | } |
| 389 | return h, nil |
| 390 | } |
| 391 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 392 | // 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] | 393 | func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts UpdateOptsBuilder) UpdateResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 394 | var res UpdateResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 395 | h := c.AuthenticatedHeaders() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 396 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 397 | if opts != nil { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 398 | headers, err := opts.ToObjectUpdateMap() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 399 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 400 | res.Err = err |
| 401 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 402 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 403 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 404 | for k, v := range headers { |
| 405 | h[k] = v |
| 406 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 407 | } |
| 408 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 409 | url := updateURL(c, containerName, objectName) |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 410 | resp, err := c.Request("POST", url, gophercloud.RequestOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 411 | MoreHeaders: h, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 412 | }) |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 413 | res.Header = resp.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 | } |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 417 | |
| 418 | // HTTPMethod represents an HTTP method string (e.g. "GET"). |
| 419 | type HTTPMethod string |
| 420 | |
| 421 | var ( |
| 422 | // GET represents an HTTP "GET" method. |
| 423 | GET HTTPMethod = "GET" |
| 424 | // POST represents an HTTP "POST" method. |
| 425 | POST HTTPMethod = "POST" |
| 426 | ) |
| 427 | |
| 428 | // CreateTempURLOpts are options for creating a temporary URL for an object. |
| 429 | type CreateTempURLOpts struct { |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 430 | // (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] | 431 | // are "GET" and "POST". |
| 432 | Method HTTPMethod |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 433 | // (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] | 434 | TTL int |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 435 | // (Optional) Split is the string on which to split the object URL. Since only |
| 436 | // the object path is used in the hash, the object URL needs to be parsed. If |
| 437 | // empty, the default OpenStack URL split point will be used ("/v1/"). |
| 438 | Split string |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | // CreateTempURL is a function for creating a temporary URL for an object. It |
| 442 | // allows users to have "GET" or "POST" access to a particular tenant's object |
| 443 | // for a limited amount of time. |
| 444 | func CreateTempURL(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateTempURLOpts) (string, error) { |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 445 | if opts.Split == "" { |
| 446 | opts.Split = "/v1/" |
| 447 | } |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 448 | duration := time.Duration(opts.TTL) * time.Second |
| 449 | expiry := time.Now().Add(duration).Unix() |
| 450 | getHeader, err := accounts.Get(c, nil).Extract() |
| 451 | if err != nil { |
| 452 | return "", err |
| 453 | } |
| 454 | secretKey := []byte(getHeader.TempURLKey) |
| 455 | url := getURL(c, containerName, objectName) |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 456 | splitPath := strings.Split(url, opts.Split) |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 457 | baseURL, objectPath := splitPath[0], splitPath[1] |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 458 | objectPath = opts.Split + objectPath |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 459 | body := fmt.Sprintf("%s\n%d\n%s", opts.Method, expiry, objectPath) |
| 460 | hash := hmac.New(sha1.New, secretKey) |
| 461 | hash.Write([]byte(body)) |
| 462 | hexsum := fmt.Sprintf("%x", hash.Sum(nil)) |
| 463 | return fmt.Sprintf("%s%s?temp_url_sig=%s&temp_url_expires=%d", baseURL, objectPath, hexsum, expiry), nil |
| 464 | } |