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