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