blob: 013d50e7f323a94379f2f623acd42b1c87250351 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package objects
2
3import (
Jon Perritt90957602015-02-01 17:03:06 -07004 "crypto/hmac"
Jamie Hannaford08096232015-07-13 12:47:28 +02005 "crypto/md5"
Jon Perritt90957602015-02-01 17:03:06 -07006 "crypto/sha1"
Jon Perritt816d2a02014-03-11 20:49:46 -05007 "fmt"
Jon Perritt8c93a302014-09-28 22:35:57 -05008 "io"
Jamie Hannaford08096232015-07-13 12:47:28 +02009 "net/http"
Jon Perritt90957602015-02-01 17:03:06 -070010 "strings"
Jon Perritt8c93a302014-09-28 22:35:57 -050011 "time"
Ash Wilson604320e2014-09-10 16:02:28 -040012
Ash Wilson604320e2014-09-10 16:02:28 -040013 "github.com/rackspace/gophercloud"
Jon Perritt90957602015-02-01 17:03:06 -070014 "github.com/rackspace/gophercloud/openstack/objectstorage/v1/accounts"
Ash Wilsonca6f7562014-09-16 15:43:54 -040015 "github.com/rackspace/gophercloud/pagination"
Jon Perritt816d2a02014-03-11 20:49:46 -050016)
17
Jon Perritte90aced2014-10-12 23:24:06 -050018// ListOptsBuilder allows extensions to add additional parameters to the List
19// request.
20type ListOptsBuilder interface {
21 ToObjectListParams() (bool, string, error)
22}
23
Jon Perritt8c93a302014-09-28 22:35:57 -050024// ListOpts is a structure that holds parameters for listing objects.
25type ListOpts struct {
Jon Perritt9415ca72014-11-03 11:58:48 -060026 // 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 Perritt8c93a302014-09-28 22:35:57 -050030 Full bool
Jon Perritt04851d32014-10-14 02:07:13 -050031 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 Wilsonca6f7562014-09-16 15:43:54 -040038}
39
Jon Perritte90aced2014-10-12 23:24:06 -050040// ToObjectListParams formats a ListOpts into a query string and boolean
41// representing whether to list complete information for each object.
42func (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 Perritt816d2a02014-03-11 20:49:46 -050050// 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 Perritteb575642014-04-24 15:16:31 -050052// response to the ExtractInfo or ExtractNames function, respectively.
Jon Perritte90aced2014-10-12 23:24:06 -050053func List(c *gophercloud.ServiceClient, containerName string, opts ListOptsBuilder) pagination.Pager {
54 headers := map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
Jon Perritt816d2a02014-03-11 20:49:46 -050055
Jon Perrittea4e3012014-10-09 22:03:19 -050056 url := listURL(c, containerName)
Jon Perrittde47eac2014-09-30 15:34:17 -050057 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050058 full, query, err := opts.ToObjectListParams()
Jon Perrittde47eac2014-09-30 15:34:17 -050059 if err != nil {
Jon Perrittde47eac2014-09-30 15:34:17 -050060 return pagination.Pager{Err: err}
61 }
Jon Perritte90aced2014-10-12 23:24:06 -050062 url += query
Jon Perritt816d2a02014-03-11 20:49:46 -050063
Jon Perritte90aced2014-10-12 23:24:06 -050064 if full {
65 headers = map[string]string{"Accept": "application/json", "Content-Type": "application/json"}
Jon Perrittde47eac2014-09-30 15:34:17 -050066 }
Ash Wilsonca6f7562014-09-16 15:43:54 -040067 }
68
Ash Wilsonb8b16f82014-10-20 10:19:49 -040069 createPage := func(r pagination.PageResult) pagination.Page {
70 p := ObjectPage{pagination.MarkerPageBase{PageResult: r}}
Ash Wilsonca6f7562014-09-16 15:43:54 -040071 p.MarkerPageBase.Owner = p
72 return p
Jon Perritt816d2a02014-03-11 20:49:46 -050073 }
74
Ash Wilsonca6f7562014-09-16 15:43:54 -040075 pager := pagination.NewPager(c, url, createPage)
76 pager.Headers = headers
77 return pager
Jon Perritt816d2a02014-03-11 20:49:46 -050078}
79
Jon Perritte90aced2014-10-12 23:24:06 -050080// DownloadOptsBuilder allows extensions to add additional parameters to the
81// Download request.
82type DownloadOptsBuilder interface {
83 ToObjectDownloadParams() (map[string]string, string, error)
84}
85
Jon Perritt8c93a302014-09-28 22:35:57 -050086// DownloadOpts is a structure that holds parameters for downloading an object.
87type 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 Perritte90aced2014-10-12 23:24:06 -050098// ToObjectDownloadParams formats a DownloadOpts into a query string and map of
99// headers.
Paul Querna7dc6fe62014-11-01 08:09:41 -0700100func (opts DownloadOpts) ToObjectDownloadParams() (map[string]string, string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500101 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 Perritt816d2a02014-03-11 20:49:46 -0500112// Download is a function that retrieves the content and metadata for an object.
Jon Perritte90aced2014-10-12 23:24:06 -0500113// To extract just the content, pass the DownloadResult response to the
114// ExtractContent function.
115func Download(c *gophercloud.ServiceClient, containerName, objectName string, opts DownloadOptsBuilder) DownloadResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500116 var res DownloadResult
Jon Perritt8c93a302014-09-28 22:35:57 -0500117
Jon Perrittea4e3012014-10-09 22:03:19 -0500118 url := downloadURL(c, containerName, objectName)
Ash Wilson77857dc2014-10-22 09:09:02 -0400119 h := c.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500120
Jon Perrittde47eac2014-09-30 15:34:17 -0500121 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -0500122 headers, query, err := opts.ToObjectDownloadParams()
Jon Perrittde47eac2014-09-30 15:34:17 -0500123 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500124 res.Err = err
125 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500126 }
127
128 for k, v := range headers {
129 h[k] = v
130 }
131
Jon Perritte90aced2014-10-12 23:24:06 -0500132 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500133 }
134
Ash Wilson4bf41a32015-02-12 15:52:44 -0500135 resp, err := c.Request("GET", url, gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500136 MoreHeaders: h,
Paul Querna9163df22014-11-01 09:38:51 -0700137 OkCodes: []int{200, 304},
Jon Perritt816d2a02014-03-11 20:49:46 -0500138 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600139 if resp != nil {
140 res.Header = resp.Header
141 res.Body = resp.Body
142 }
Jon Perritt5db08922014-09-30 21:32:48 -0500143 res.Err = err
Jamie Hannaford2e784862014-10-27 10:40:27 +0100144
Jon Perritt5db08922014-09-30 21:32:48 -0500145 return res
Jon Perritt8c93a302014-09-28 22:35:57 -0500146}
147
Jon Perritte90aced2014-10-12 23:24:06 -0500148// CreateOptsBuilder allows extensions to add additional parameters to the
149// Create request.
150type CreateOptsBuilder interface {
151 ToObjectCreateParams() (map[string]string, string, error)
152}
153
Jon Perritt8c93a302014-09-28 22:35:57 -0500154// CreateOpts is a structure that holds parameters for creating an object.
155type CreateOpts struct {
156 Metadata map[string]string
157 ContentDisposition string `h:"Content-Disposition"`
158 ContentEncoding string `h:"Content-Encoding"`
Jon Perritte376fa52014-11-03 11:35:48 -0600159 ContentLength int64 `h:"Content-Length"`
Jon Perritt8c93a302014-09-28 22:35:57 -0500160 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 Perritt816d2a02014-03-11 20:49:46 -0500172}
173
Jon Perritte90aced2014-10-12 23:24:06 -0500174// ToObjectCreateParams formats a CreateOpts into a query string and map of
175// headers.
176func (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 Perritt816d2a02014-03-11 20:49:46 -0500193// Create is a function that creates a new object or replaces an existing object.
Brendan ODonnella69b3472015-04-27 13:59:41 -0500194func Create(c *gophercloud.ServiceClient, containerName, objectName string, content io.ReadSeeker, opts CreateOptsBuilder) CreateResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500195 var res CreateResult
Jon Perritt816d2a02014-03-11 20:49:46 -0500196
Jon Perrittea4e3012014-10-09 22:03:19 -0500197 url := createURL(c, containerName, objectName)
Ash Wilson322a7e62015-02-12 16:25:26 -0500198 h := make(map[string]string)
Jon Perritt816d2a02014-03-11 20:49:46 -0500199
Jon Perrittde47eac2014-09-30 15:34:17 -0500200 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -0500201 headers, query, err := opts.ToObjectCreateParams()
Jon Perrittde47eac2014-09-30 15:34:17 -0500202 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500203 res.Err = err
204 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500205 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500206
Jon Perrittde47eac2014-09-30 15:34:17 -0500207 for k, v := range headers {
208 h[k] = v
209 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500210
Jon Perritte90aced2014-10-12 23:24:06 -0500211 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500212 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500213
Ash Wilson322a7e62015-02-12 16:25:26 -0500214 ropts := gophercloud.RequestOpts{
215 RawBody: content,
Jon Perritt816d2a02014-03-11 20:49:46 -0500216 MoreHeaders: h,
Ash Wilson45e34342015-01-23 14:25:34 -0500217 }
218
Jamie Hannaford08096232015-07-13 12:47:28 +0200219 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 Perritta2c88b22015-05-18 11:23:30 -0600225 }
Jamie Hannaford08096232015-07-13 12:47:28 +0200226
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 Perritt5db08922014-09-30 21:32:48 -0500243 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500244}
245
Jon Perritte90aced2014-10-12 23:24:06 -0500246// CopyOptsBuilder allows extensions to add additional parameters to the
247// Copy request.
248type CopyOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500249 ToObjectCopyMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500250}
251
252// CopyOpts is a structure that holds parameters for copying one object to
253// another.
Jon Perritt8c93a302014-09-28 22:35:57 -0500254type 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 Perritt04851d32014-10-14 02:07:13 -0500262// ToObjectCopyMap formats a CopyOpts into a map of headers.
263func (opts CopyOpts) ToObjectCopyMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500264 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 Perritt816d2a02014-03-11 20:49:46 -0500277// Copy is a function that copies one object to another.
Jon Perritte90aced2014-10-12 23:24:06 -0500278func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts CopyOptsBuilder) CopyResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500279 var res CopyResult
Ash Wilson77857dc2014-10-22 09:09:02 -0400280 h := c.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500281
Jon Perritt04851d32014-10-14 02:07:13 -0500282 headers, err := opts.ToObjectCopyMap()
Jon Perritt8c93a302014-09-28 22:35:57 -0500283 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500284 res.Err = err
285 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500286 }
Jon Perritte90aced2014-10-12 23:24:06 -0500287
Jon Perritt8c93a302014-09-28 22:35:57 -0500288 for k, v := range headers {
Jon Perritt816d2a02014-03-11 20:49:46 -0500289 h[k] = v
290 }
291
Jon Perrittea4e3012014-10-09 22:03:19 -0500292 url := copyURL(c, containerName, objectName)
Ash Wilson4bf41a32015-02-12 15:52:44 -0500293 resp, err := c.Request("COPY", url, gophercloud.RequestOpts{
Jon Perritt8c93a302014-09-28 22:35:57 -0500294 MoreHeaders: h,
295 OkCodes: []int{201},
296 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600297 if resp != nil {
298 res.Header = resp.Header
299 }
jrperrittf7a8e282014-10-28 10:00:48 -0500300 res.Err = err
Jon Perritt5db08922014-09-30 21:32:48 -0500301 return res
Jon Perritt8c93a302014-09-28 22:35:57 -0500302}
303
Jon Perritte90aced2014-10-12 23:24:06 -0500304// DeleteOptsBuilder allows extensions to add additional parameters to the
305// Delete request.
306type DeleteOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500307 ToObjectDeleteQuery() (string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500308}
309
Jon Perritt8c93a302014-09-28 22:35:57 -0500310// DeleteOpts is a structure that holds parameters for deleting an object.
311type DeleteOpts struct {
312 MultipartManifest string `q:"multipart-manifest"`
313}
314
Jon Perritt26780d52014-10-14 11:35:58 -0500315// ToObjectDeleteQuery formats a DeleteOpts into a query string.
316func (opts DeleteOpts) ToObjectDeleteQuery() (string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500317 q, err := gophercloud.BuildQueryString(opts)
318 if err != nil {
319 return "", err
320 }
321 return q.String(), nil
322}
323
Jon Perritt8c93a302014-09-28 22:35:57 -0500324// Delete is a function that deletes an object.
Jon Perritte90aced2014-10-12 23:24:06 -0500325func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts DeleteOptsBuilder) DeleteResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500326 var res DeleteResult
Jon Perrittea4e3012014-10-09 22:03:19 -0500327 url := deleteURL(c, containerName, objectName)
Jon Perritt8c93a302014-09-28 22:35:57 -0500328
Jon Perrittde47eac2014-09-30 15:34:17 -0500329 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500330 query, err := opts.ToObjectDeleteQuery()
Jon Perrittde47eac2014-09-30 15:34:17 -0500331 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500332 res.Err = err
333 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500334 }
Jon Perritte90aced2014-10-12 23:24:06 -0500335 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500336 }
337
Jamie Hannaford1d27afa2015-03-24 16:20:45 +0100338 resp, err := c.Delete(url, nil)
Jon Perritta2c88b22015-05-18 11:23:30 -0600339 if resp != nil {
340 res.Header = resp.Header
341 }
Jon Perritt10a7ec12014-10-27 11:29:33 -0500342 res.Err = err
Jon Perritt5db08922014-09-30 21:32:48 -0500343 return res
Jon Perritt8c93a302014-09-28 22:35:57 -0500344}
345
Jon Perritte90aced2014-10-12 23:24:06 -0500346// GetOptsBuilder allows extensions to add additional parameters to the
347// Get request.
348type GetOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500349 ToObjectGetQuery() (string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500350}
351
Jon Perritt8c93a302014-09-28 22:35:57 -0500352// GetOpts is a structure that holds parameters for getting an object's metadata.
353type GetOpts struct {
354 Expires string `q:"expires"`
355 Signature string `q:"signature"`
356}
357
Jon Perritt26780d52014-10-14 11:35:58 -0500358// ToObjectGetQuery formats a GetOpts into a query string.
359func (opts GetOpts) ToObjectGetQuery() (string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500360 q, err := gophercloud.BuildQueryString(opts)
361 if err != nil {
362 return "", err
363 }
364 return q.String(), nil
365}
366
Jon Perritt8c93a302014-09-28 22:35:57 -0500367// 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 Perritte90aced2014-10-12 23:24:06 -0500369func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts GetOptsBuilder) GetResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500370 var res GetResult
Jon Perrittea4e3012014-10-09 22:03:19 -0500371 url := getURL(c, containerName, objectName)
Jon Perrittde47eac2014-09-30 15:34:17 -0500372
373 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500374 query, err := opts.ToObjectGetQuery()
Jon Perrittde47eac2014-09-30 15:34:17 -0500375 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500376 res.Err = err
377 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500378 }
Jon Perritte90aced2014-10-12 23:24:06 -0500379 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500380 }
381
Ash Wilson4bf41a32015-02-12 15:52:44 -0500382 resp, err := c.Request("HEAD", url, gophercloud.RequestOpts{
383 OkCodes: []int{200, 204},
Jon Perritt8c93a302014-09-28 22:35:57 -0500384 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600385 if resp != nil {
386 res.Header = resp.Header
387 }
Jon Perritt5db08922014-09-30 21:32:48 -0500388 res.Err = err
Jon Perritt5db08922014-09-30 21:32:48 -0500389 return res
Jon Perritt8c93a302014-09-28 22:35:57 -0500390}
391
Jon Perritte90aced2014-10-12 23:24:06 -0500392// UpdateOptsBuilder allows extensions to add additional parameters to the
393// Update request.
394type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500395 ToObjectUpdateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500396}
397
Jon Perritt8c93a302014-09-28 22:35:57 -0500398// UpdateOpts is a structure that holds parameters for updating, creating, or deleting an
399// object's metadata.
400type 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 Perritt04851d32014-10-14 02:07:13 -0500410// ToObjectUpdateMap formats a UpdateOpts into a map of headers.
411func (opts UpdateOpts) ToObjectUpdateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500412 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 Perritt8c93a302014-09-28 22:35:57 -0500422// Update is a function that creates, updates, or deletes an object's metadata.
Jon Perritte90aced2014-10-12 23:24:06 -0500423func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts UpdateOptsBuilder) UpdateResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500424 var res UpdateResult
Ash Wilson77857dc2014-10-22 09:09:02 -0400425 h := c.AuthenticatedHeaders()
Jon Perritt8c93a302014-09-28 22:35:57 -0500426
Jon Perrittde47eac2014-09-30 15:34:17 -0500427 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500428 headers, err := opts.ToObjectUpdateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500429 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500430 res.Err = err
431 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500432 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500433
Jon Perrittde47eac2014-09-30 15:34:17 -0500434 for k, v := range headers {
435 h[k] = v
436 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500437 }
438
Jon Perrittea4e3012014-10-09 22:03:19 -0500439 url := updateURL(c, containerName, objectName)
Ash Wilson4bf41a32015-02-12 15:52:44 -0500440 resp, err := c.Request("POST", url, gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500441 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500442 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600443 if resp != nil {
444 res.Header = resp.Header
445 }
Jon Perritt5db08922014-09-30 21:32:48 -0500446 res.Err = err
447 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500448}
Jon Perritt90957602015-02-01 17:03:06 -0700449
450// HTTPMethod represents an HTTP method string (e.g. "GET").
451type HTTPMethod string
452
453var (
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.
461type CreateTempURLOpts struct {
Jon Perritt28792af2015-02-02 11:00:04 -0700462 // (REQUIRED) Method is the HTTP method to allow for users of the temp URL. Valid values
Jon Perritt90957602015-02-01 17:03:06 -0700463 // are "GET" and "POST".
464 Method HTTPMethod
Jon Perritt28792af2015-02-02 11:00:04 -0700465 // (REQUIRED) TTL is the number of seconds the temp URL should be active.
Jon Perritt90957602015-02-01 17:03:06 -0700466 TTL int
Jon Perritt28792af2015-02-02 11:00:04 -0700467 // (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 Perritt90957602015-02-01 17:03:06 -0700471}
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.
476func CreateTempURL(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateTempURLOpts) (string, error) {
Jon Perritt28792af2015-02-02 11:00:04 -0700477 if opts.Split == "" {
478 opts.Split = "/v1/"
479 }
Jon Perritt90957602015-02-01 17:03:06 -0700480 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 Perritt28792af2015-02-02 11:00:04 -0700488 splitPath := strings.Split(url, opts.Split)
Jon Perritt90957602015-02-01 17:03:06 -0700489 baseURL, objectPath := splitPath[0], splitPath[1]
Jon Perritt28792af2015-02-02 11:00:04 -0700490 objectPath = opts.Split + objectPath
Jon Perritt90957602015-02-01 17:03:06 -0700491 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}