blob: b9a0e7e6ed44aca489e3f81e8a7f93eed6c83e10 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package objects
2
3import (
Jon Perritt90957602015-02-01 17:03:06 -07004 "crypto/hmac"
5 "crypto/sha1"
Jon Perritt816d2a02014-03-11 20:49:46 -05006 "fmt"
Jon Perritt8c93a302014-09-28 22:35:57 -05007 "io"
Jon Perritt90957602015-02-01 17:03:06 -07008 "strings"
Jon Perritt8c93a302014-09-28 22:35:57 -05009 "time"
Ash Wilson604320e2014-09-10 16:02:28 -040010
Ash Wilson604320e2014-09-10 16:02:28 -040011 "github.com/rackspace/gophercloud"
Jon Perritt90957602015-02-01 17:03:06 -070012 "github.com/rackspace/gophercloud/openstack/objectstorage/v1/accounts"
Ash Wilsonca6f7562014-09-16 15:43:54 -040013 "github.com/rackspace/gophercloud/pagination"
Jon Perritt816d2a02014-03-11 20:49:46 -050014)
15
Jon Perritte90aced2014-10-12 23:24:06 -050016// ListOptsBuilder allows extensions to add additional parameters to the List
17// request.
18type ListOptsBuilder interface {
19 ToObjectListParams() (bool, string, error)
20}
21
Jon Perritt8c93a302014-09-28 22:35:57 -050022// ListOpts is a structure that holds parameters for listing objects.
23type ListOpts struct {
Jon Perritt9415ca72014-11-03 11:58:48 -060024 // 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 Perritt8c93a302014-09-28 22:35:57 -050028 Full bool
Jon Perritt04851d32014-10-14 02:07:13 -050029 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 Wilsonca6f7562014-09-16 15:43:54 -040036}
37
Jon Perritte90aced2014-10-12 23:24:06 -050038// ToObjectListParams formats a ListOpts into a query string and boolean
39// representing whether to list complete information for each object.
40func (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 Perritt816d2a02014-03-11 20:49:46 -050048// 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 Perritteb575642014-04-24 15:16:31 -050050// response to the ExtractInfo or ExtractNames function, respectively.
Jon Perritte90aced2014-10-12 23:24:06 -050051func List(c *gophercloud.ServiceClient, containerName string, opts ListOptsBuilder) pagination.Pager {
52 headers := map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
Jon Perritt816d2a02014-03-11 20:49:46 -050053
Jon Perrittea4e3012014-10-09 22:03:19 -050054 url := listURL(c, containerName)
Jon Perrittde47eac2014-09-30 15:34:17 -050055 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050056 full, query, err := opts.ToObjectListParams()
Jon Perrittde47eac2014-09-30 15:34:17 -050057 if err != nil {
Jon Perrittde47eac2014-09-30 15:34:17 -050058 return pagination.Pager{Err: err}
59 }
Jon Perritte90aced2014-10-12 23:24:06 -050060 url += query
Jon Perritt816d2a02014-03-11 20:49:46 -050061
Jon Perritte90aced2014-10-12 23:24:06 -050062 if full {
63 headers = map[string]string{"Accept": "application/json", "Content-Type": "application/json"}
Jon Perrittde47eac2014-09-30 15:34:17 -050064 }
Ash Wilsonca6f7562014-09-16 15:43:54 -040065 }
66
Ash Wilsonb8b16f82014-10-20 10:19:49 -040067 createPage := func(r pagination.PageResult) pagination.Page {
68 p := ObjectPage{pagination.MarkerPageBase{PageResult: r}}
Ash Wilsonca6f7562014-09-16 15:43:54 -040069 p.MarkerPageBase.Owner = p
70 return p
Jon Perritt816d2a02014-03-11 20:49:46 -050071 }
72
Ash Wilsonca6f7562014-09-16 15:43:54 -040073 pager := pagination.NewPager(c, url, createPage)
74 pager.Headers = headers
75 return pager
Jon Perritt816d2a02014-03-11 20:49:46 -050076}
77
Jon Perritte90aced2014-10-12 23:24:06 -050078// DownloadOptsBuilder allows extensions to add additional parameters to the
79// Download request.
80type DownloadOptsBuilder interface {
81 ToObjectDownloadParams() (map[string]string, string, error)
82}
83
Jon Perritt8c93a302014-09-28 22:35:57 -050084// DownloadOpts is a structure that holds parameters for downloading an object.
85type 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 Perritte90aced2014-10-12 23:24:06 -050096// ToObjectDownloadParams formats a DownloadOpts into a query string and map of
97// headers.
Paul Querna7dc6fe62014-11-01 08:09:41 -070098func (opts DownloadOpts) ToObjectDownloadParams() (map[string]string, string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -050099 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 Perritt816d2a02014-03-11 20:49:46 -0500110// Download is a function that retrieves the content and metadata for an object.
Jon Perritte90aced2014-10-12 23:24:06 -0500111// To extract just the content, pass the DownloadResult response to the
112// ExtractContent function.
113func Download(c *gophercloud.ServiceClient, containerName, objectName string, opts DownloadOptsBuilder) DownloadResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500114 var res DownloadResult
Jon Perritt8c93a302014-09-28 22:35:57 -0500115
Jon Perrittea4e3012014-10-09 22:03:19 -0500116 url := downloadURL(c, containerName, objectName)
Ash Wilson77857dc2014-10-22 09:09:02 -0400117 h := c.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500118
Jon Perrittde47eac2014-09-30 15:34:17 -0500119 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -0500120 headers, query, err := opts.ToObjectDownloadParams()
Jon Perrittde47eac2014-09-30 15:34:17 -0500121 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500122 res.Err = err
123 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500124 }
125
126 for k, v := range headers {
127 h[k] = v
128 }
129
Jon Perritte90aced2014-10-12 23:24:06 -0500130 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500131 }
132
Ash Wilson4bf41a32015-02-12 15:52:44 -0500133 resp, err := c.Request("GET", url, gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500134 MoreHeaders: h,
Paul Querna9163df22014-11-01 09:38:51 -0700135 OkCodes: []int{200, 304},
Jon Perritt816d2a02014-03-11 20:49:46 -0500136 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600137 if resp != nil {
138 res.Header = resp.Header
139 res.Body = resp.Body
140 }
Jon Perritt5db08922014-09-30 21:32:48 -0500141 res.Err = err
Jamie Hannaford2e784862014-10-27 10:40:27 +0100142
Jon Perritt5db08922014-09-30 21:32:48 -0500143 return res
Jon Perritt8c93a302014-09-28 22:35:57 -0500144}
145
Jon Perritte90aced2014-10-12 23:24:06 -0500146// CreateOptsBuilder allows extensions to add additional parameters to the
147// Create request.
148type CreateOptsBuilder interface {
149 ToObjectCreateParams() (map[string]string, string, error)
150}
151
Jon Perritt8c93a302014-09-28 22:35:57 -0500152// CreateOpts is a structure that holds parameters for creating an object.
153type CreateOpts struct {
154 Metadata map[string]string
155 ContentDisposition string `h:"Content-Disposition"`
156 ContentEncoding string `h:"Content-Encoding"`
Jon Perritte376fa52014-11-03 11:35:48 -0600157 ContentLength int64 `h:"Content-Length"`
Jon Perritt8c93a302014-09-28 22:35:57 -0500158 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 Perritt816d2a02014-03-11 20:49:46 -0500170}
171
Jon Perritte90aced2014-10-12 23:24:06 -0500172// ToObjectCreateParams formats a CreateOpts into a query string and map of
173// headers.
174func (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 Perritt816d2a02014-03-11 20:49:46 -0500191// Create is a function that creates a new object or replaces an existing object.
Brendan ODonnella69b3472015-04-27 13:59:41 -0500192func Create(c *gophercloud.ServiceClient, containerName, objectName string, content io.ReadSeeker, opts CreateOptsBuilder) CreateResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500193 var res CreateResult
Jon Perritt816d2a02014-03-11 20:49:46 -0500194
Jon Perrittea4e3012014-10-09 22:03:19 -0500195 url := createURL(c, containerName, objectName)
Ash Wilson322a7e62015-02-12 16:25:26 -0500196 h := make(map[string]string)
Jon Perritt816d2a02014-03-11 20:49:46 -0500197
Jon Perrittde47eac2014-09-30 15:34:17 -0500198 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -0500199 headers, query, err := opts.ToObjectCreateParams()
Jon Perrittde47eac2014-09-30 15:34:17 -0500200 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500201 res.Err = err
202 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500203 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500204
Jon Perrittde47eac2014-09-30 15:34:17 -0500205 for k, v := range headers {
206 h[k] = v
207 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500208
Jon Perritte90aced2014-10-12 23:24:06 -0500209 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500210 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500211
Ash Wilson322a7e62015-02-12 16:25:26 -0500212 ropts := gophercloud.RequestOpts{
213 RawBody: content,
Jon Perritt816d2a02014-03-11 20:49:46 -0500214 MoreHeaders: h,
Ash Wilson45e34342015-01-23 14:25:34 -0500215 }
216
Ash Wilson322a7e62015-02-12 16:25:26 -0500217 resp, err := c.Request("PUT", url, ropts)
Jon Perritta2c88b22015-05-18 11:23:30 -0600218 if resp != nil {
219 res.Header = resp.Header
220 }
Jon Perritt5db08922014-09-30 21:32:48 -0500221 res.Err = err
222 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500223}
224
Jon Perritte90aced2014-10-12 23:24:06 -0500225// CopyOptsBuilder allows extensions to add additional parameters to the
226// Copy request.
227type CopyOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500228 ToObjectCopyMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500229}
230
231// CopyOpts is a structure that holds parameters for copying one object to
232// another.
Jon Perritt8c93a302014-09-28 22:35:57 -0500233type 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 Perritt04851d32014-10-14 02:07:13 -0500241// ToObjectCopyMap formats a CopyOpts into a map of headers.
242func (opts CopyOpts) ToObjectCopyMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500243 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 Perritt816d2a02014-03-11 20:49:46 -0500256// Copy is a function that copies one object to another.
Jon Perritte90aced2014-10-12 23:24:06 -0500257func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts CopyOptsBuilder) CopyResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500258 var res CopyResult
Ash Wilson77857dc2014-10-22 09:09:02 -0400259 h := c.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500260
Jon Perritt04851d32014-10-14 02:07:13 -0500261 headers, err := opts.ToObjectCopyMap()
Jon Perritt8c93a302014-09-28 22:35:57 -0500262 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500263 res.Err = err
264 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500265 }
Jon Perritte90aced2014-10-12 23:24:06 -0500266
Jon Perritt8c93a302014-09-28 22:35:57 -0500267 for k, v := range headers {
Jon Perritt816d2a02014-03-11 20:49:46 -0500268 h[k] = v
269 }
270
Jon Perrittea4e3012014-10-09 22:03:19 -0500271 url := copyURL(c, containerName, objectName)
Ash Wilson4bf41a32015-02-12 15:52:44 -0500272 resp, err := c.Request("COPY", url, gophercloud.RequestOpts{
Jon Perritt8c93a302014-09-28 22:35:57 -0500273 MoreHeaders: h,
274 OkCodes: []int{201},
275 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600276 if resp != nil {
277 res.Header = resp.Header
278 }
jrperrittf7a8e282014-10-28 10:00:48 -0500279 res.Err = err
Jon Perritt5db08922014-09-30 21:32:48 -0500280 return res
Jon Perritt8c93a302014-09-28 22:35:57 -0500281}
282
Jon Perritte90aced2014-10-12 23:24:06 -0500283// DeleteOptsBuilder allows extensions to add additional parameters to the
284// Delete request.
285type DeleteOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500286 ToObjectDeleteQuery() (string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500287}
288
Jon Perritt8c93a302014-09-28 22:35:57 -0500289// DeleteOpts is a structure that holds parameters for deleting an object.
290type DeleteOpts struct {
291 MultipartManifest string `q:"multipart-manifest"`
292}
293
Jon Perritt26780d52014-10-14 11:35:58 -0500294// ToObjectDeleteQuery formats a DeleteOpts into a query string.
295func (opts DeleteOpts) ToObjectDeleteQuery() (string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500296 q, err := gophercloud.BuildQueryString(opts)
297 if err != nil {
298 return "", err
299 }
300 return q.String(), nil
301}
302
Jon Perritt8c93a302014-09-28 22:35:57 -0500303// Delete is a function that deletes an object.
Jon Perritte90aced2014-10-12 23:24:06 -0500304func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts DeleteOptsBuilder) DeleteResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500305 var res DeleteResult
Jon Perrittea4e3012014-10-09 22:03:19 -0500306 url := deleteURL(c, containerName, objectName)
Jon Perritt8c93a302014-09-28 22:35:57 -0500307
Jon Perrittde47eac2014-09-30 15:34:17 -0500308 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500309 query, err := opts.ToObjectDeleteQuery()
Jon Perrittde47eac2014-09-30 15:34:17 -0500310 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500311 res.Err = err
312 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500313 }
Jon Perritte90aced2014-10-12 23:24:06 -0500314 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500315 }
316
Jamie Hannaford1d27afa2015-03-24 16:20:45 +0100317 resp, err := c.Delete(url, nil)
Jon Perritta2c88b22015-05-18 11:23:30 -0600318 if resp != nil {
319 res.Header = resp.Header
320 }
Jon Perritt10a7ec12014-10-27 11:29:33 -0500321 res.Err = err
Jon Perritt5db08922014-09-30 21:32:48 -0500322 return res
Jon Perritt8c93a302014-09-28 22:35:57 -0500323}
324
Jon Perritte90aced2014-10-12 23:24:06 -0500325// GetOptsBuilder allows extensions to add additional parameters to the
326// Get request.
327type GetOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500328 ToObjectGetQuery() (string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500329}
330
Jon Perritt8c93a302014-09-28 22:35:57 -0500331// GetOpts is a structure that holds parameters for getting an object's metadata.
332type GetOpts struct {
333 Expires string `q:"expires"`
334 Signature string `q:"signature"`
335}
336
Jon Perritt26780d52014-10-14 11:35:58 -0500337// ToObjectGetQuery formats a GetOpts into a query string.
338func (opts GetOpts) ToObjectGetQuery() (string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500339 q, err := gophercloud.BuildQueryString(opts)
340 if err != nil {
341 return "", err
342 }
343 return q.String(), nil
344}
345
Jon Perritt8c93a302014-09-28 22:35:57 -0500346// 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 Perritte90aced2014-10-12 23:24:06 -0500348func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts GetOptsBuilder) GetResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500349 var res GetResult
Jon Perrittea4e3012014-10-09 22:03:19 -0500350 url := getURL(c, containerName, objectName)
Jon Perrittde47eac2014-09-30 15:34:17 -0500351
352 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500353 query, err := opts.ToObjectGetQuery()
Jon Perrittde47eac2014-09-30 15:34:17 -0500354 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500355 res.Err = err
356 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500357 }
Jon Perritte90aced2014-10-12 23:24:06 -0500358 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500359 }
360
Ash Wilson4bf41a32015-02-12 15:52:44 -0500361 resp, err := c.Request("HEAD", url, gophercloud.RequestOpts{
362 OkCodes: []int{200, 204},
Jon Perritt8c93a302014-09-28 22:35:57 -0500363 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600364 if resp != nil {
365 res.Header = resp.Header
366 }
Jon Perritt5db08922014-09-30 21:32:48 -0500367 res.Err = err
Jon Perritt5db08922014-09-30 21:32:48 -0500368 return res
Jon Perritt8c93a302014-09-28 22:35:57 -0500369}
370
Jon Perritte90aced2014-10-12 23:24:06 -0500371// UpdateOptsBuilder allows extensions to add additional parameters to the
372// Update request.
373type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500374 ToObjectUpdateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500375}
376
Jon Perritt8c93a302014-09-28 22:35:57 -0500377// UpdateOpts is a structure that holds parameters for updating, creating, or deleting an
378// object's metadata.
379type 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 Perritt04851d32014-10-14 02:07:13 -0500389// ToObjectUpdateMap formats a UpdateOpts into a map of headers.
390func (opts UpdateOpts) ToObjectUpdateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500391 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 Perritt8c93a302014-09-28 22:35:57 -0500401// Update is a function that creates, updates, or deletes an object's metadata.
Jon Perritte90aced2014-10-12 23:24:06 -0500402func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts UpdateOptsBuilder) UpdateResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500403 var res UpdateResult
Ash Wilson77857dc2014-10-22 09:09:02 -0400404 h := c.AuthenticatedHeaders()
Jon Perritt8c93a302014-09-28 22:35:57 -0500405
Jon Perrittde47eac2014-09-30 15:34:17 -0500406 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500407 headers, err := opts.ToObjectUpdateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500408 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500409 res.Err = err
410 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500411 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500412
Jon Perrittde47eac2014-09-30 15:34:17 -0500413 for k, v := range headers {
414 h[k] = v
415 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500416 }
417
Jon Perrittea4e3012014-10-09 22:03:19 -0500418 url := updateURL(c, containerName, objectName)
Ash Wilson4bf41a32015-02-12 15:52:44 -0500419 resp, err := c.Request("POST", url, gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500420 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500421 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600422 if resp != nil {
423 res.Header = resp.Header
424 }
Jon Perritt5db08922014-09-30 21:32:48 -0500425 res.Err = err
426 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500427}
Jon Perritt90957602015-02-01 17:03:06 -0700428
429// HTTPMethod represents an HTTP method string (e.g. "GET").
430type HTTPMethod string
431
432var (
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.
440type CreateTempURLOpts struct {
Jon Perritt28792af2015-02-02 11:00:04 -0700441 // (REQUIRED) Method is the HTTP method to allow for users of the temp URL. Valid values
Jon Perritt90957602015-02-01 17:03:06 -0700442 // are "GET" and "POST".
443 Method HTTPMethod
Jon Perritt28792af2015-02-02 11:00:04 -0700444 // (REQUIRED) TTL is the number of seconds the temp URL should be active.
Jon Perritt90957602015-02-01 17:03:06 -0700445 TTL int
Jon Perritt28792af2015-02-02 11:00:04 -0700446 // (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 Perritt90957602015-02-01 17:03:06 -0700450}
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.
455func CreateTempURL(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateTempURLOpts) (string, error) {
Jon Perritt28792af2015-02-02 11:00:04 -0700456 if opts.Split == "" {
457 opts.Split = "/v1/"
458 }
Jon Perritt90957602015-02-01 17:03:06 -0700459 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 Perritt28792af2015-02-02 11:00:04 -0700467 splitPath := strings.Split(url, opts.Split)
Jon Perritt90957602015-02-01 17:03:06 -0700468 baseURL, objectPath := splitPath[0], splitPath[1]
Jon Perritt28792af2015-02-02 11:00:04 -0700469 objectPath = opts.Split + objectPath
Jon Perritt90957602015-02-01 17:03:06 -0700470 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}