blob: 2505bdb8bfa00ff330a23d506af863e78337855f [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package objects
2
3import (
Jon Perrittfea90732016-03-15 02:57:05 -05004 "bytes"
Jon Perritt90957602015-02-01 17:03:06 -07005 "crypto/hmac"
Jamie Hannaford08096232015-07-13 12:47:28 +02006 "crypto/md5"
Jon Perritt90957602015-02-01 17:03:06 -07007 "crypto/sha1"
Jon Perritt816d2a02014-03-11 20:49:46 -05008 "fmt"
Jon Perritt8c93a302014-09-28 22:35:57 -05009 "io"
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
Jon Perritt27249f42016-02-18 10:35:59 -060013 "github.com/gophercloud/gophercloud"
14 "github.com/gophercloud/gophercloud/openstack/objectstorage/v1/accounts"
15 "github.com/gophercloud/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)
Jon Perrittfea90732016-03-15 02:57:05 -050044 return opts.Full, q.String(), err
Jon Perritte90aced2014-10-12 23:24:06 -050045}
46
Jon Perritt816d2a02014-03-11 20:49:46 -050047// List is a function that retrieves all objects in a container. It also returns the details
48// for the container. To extract only the object information or names, pass the ListResult
Jon Perritteb575642014-04-24 15:16:31 -050049// response to the ExtractInfo or ExtractNames function, respectively.
Jon Perritte90aced2014-10-12 23:24:06 -050050func List(c *gophercloud.ServiceClient, containerName string, opts ListOptsBuilder) pagination.Pager {
51 headers := map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
Jon Perritt816d2a02014-03-11 20:49:46 -050052
Jon Perrittea4e3012014-10-09 22:03:19 -050053 url := listURL(c, containerName)
Jon Perrittde47eac2014-09-30 15:34:17 -050054 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050055 full, query, err := opts.ToObjectListParams()
Jon Perrittde47eac2014-09-30 15:34:17 -050056 if err != nil {
Jon Perrittde47eac2014-09-30 15:34:17 -050057 return pagination.Pager{Err: err}
58 }
Jon Perritte90aced2014-10-12 23:24:06 -050059 url += query
Jon Perritt816d2a02014-03-11 20:49:46 -050060
Jon Perritte90aced2014-10-12 23:24:06 -050061 if full {
62 headers = map[string]string{"Accept": "application/json", "Content-Type": "application/json"}
Jon Perrittde47eac2014-09-30 15:34:17 -050063 }
Ash Wilsonca6f7562014-09-16 15:43:54 -040064 }
65
Jon Perrittfea90732016-03-15 02:57:05 -050066 pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
Ash Wilsonb8b16f82014-10-20 10:19:49 -040067 p := ObjectPage{pagination.MarkerPageBase{PageResult: r}}
Ash Wilsonca6f7562014-09-16 15:43:54 -040068 p.MarkerPageBase.Owner = p
69 return p
Jon Perrittfea90732016-03-15 02:57:05 -050070 })
Ash Wilsonca6f7562014-09-16 15:43:54 -040071 pager.Headers = headers
72 return pager
Jon Perritt816d2a02014-03-11 20:49:46 -050073}
74
Jon Perritte90aced2014-10-12 23:24:06 -050075// DownloadOptsBuilder allows extensions to add additional parameters to the
76// Download request.
77type DownloadOptsBuilder interface {
78 ToObjectDownloadParams() (map[string]string, string, error)
79}
80
Jon Perritt8c93a302014-09-28 22:35:57 -050081// DownloadOpts is a structure that holds parameters for downloading an object.
82type DownloadOpts struct {
83 IfMatch string `h:"If-Match"`
84 IfModifiedSince time.Time `h:"If-Modified-Since"`
85 IfNoneMatch string `h:"If-None-Match"`
86 IfUnmodifiedSince time.Time `h:"If-Unmodified-Since"`
87 Range string `h:"Range"`
88 Expires string `q:"expires"`
89 MultipartManifest string `q:"multipart-manifest"`
90 Signature string `q:"signature"`
91}
92
Jon Perritte90aced2014-10-12 23:24:06 -050093// ToObjectDownloadParams formats a DownloadOpts into a query string and map of
94// headers.
Paul Querna7dc6fe62014-11-01 08:09:41 -070095func (opts DownloadOpts) ToObjectDownloadParams() (map[string]string, string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -050096 q, err := gophercloud.BuildQueryString(opts)
97 if err != nil {
98 return nil, "", err
99 }
100 h, err := gophercloud.BuildHeaders(opts)
101 if err != nil {
102 return nil, q.String(), err
103 }
104 return h, q.String(), nil
105}
106
Jon Perritt816d2a02014-03-11 20:49:46 -0500107// Download is a function that retrieves the content and metadata for an object.
Jon Perritte90aced2014-10-12 23:24:06 -0500108// To extract just the content, pass the DownloadResult response to the
109// ExtractContent function.
110func Download(c *gophercloud.ServiceClient, containerName, objectName string, opts DownloadOptsBuilder) DownloadResult {
Jon Perrittfea90732016-03-15 02:57:05 -0500111 var r DownloadResult
Jon Perrittea4e3012014-10-09 22:03:19 -0500112 url := downloadURL(c, containerName, objectName)
Jon Perrittfea90732016-03-15 02:57:05 -0500113 h := make(map[string]string)
Jon Perrittde47eac2014-09-30 15:34:17 -0500114 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -0500115 headers, query, err := opts.ToObjectDownloadParams()
Jon Perrittde47eac2014-09-30 15:34:17 -0500116 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500117 r.Err = err
118 return r
Jon Perrittde47eac2014-09-30 15:34:17 -0500119 }
Jon Perrittde47eac2014-09-30 15:34:17 -0500120 for k, v := range headers {
121 h[k] = v
122 }
Jon Perritte90aced2014-10-12 23:24:06 -0500123 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500124 }
125
Jon Perrittfea90732016-03-15 02:57:05 -0500126 resp, err := c.Get(url, nil, &gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500127 MoreHeaders: h,
Paul Querna9163df22014-11-01 09:38:51 -0700128 OkCodes: []int{200, 304},
Jon Perritt816d2a02014-03-11 20:49:46 -0500129 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600130 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500131 r.Header = resp.Header
132 r.Body = resp.Body
Jon Perritta2c88b22015-05-18 11:23:30 -0600133 }
Jon Perrittfea90732016-03-15 02:57:05 -0500134 r.Err = err
135 return r
Jon Perritt8c93a302014-09-28 22:35:57 -0500136}
137
Jon Perritte90aced2014-10-12 23:24:06 -0500138// CreateOptsBuilder allows extensions to add additional parameters to the
139// Create request.
140type CreateOptsBuilder interface {
Jon Perrittfea90732016-03-15 02:57:05 -0500141 ToObjectCreateParams() (io.Reader, map[string]string, string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500142}
143
Jon Perritt8c93a302014-09-28 22:35:57 -0500144// CreateOpts is a structure that holds parameters for creating an object.
145type CreateOpts struct {
Jon Perrittfea90732016-03-15 02:57:05 -0500146 Content io.Reader
Jon Perritt8c93a302014-09-28 22:35:57 -0500147 Metadata map[string]string
148 ContentDisposition string `h:"Content-Disposition"`
149 ContentEncoding string `h:"Content-Encoding"`
Jon Perritte376fa52014-11-03 11:35:48 -0600150 ContentLength int64 `h:"Content-Length"`
Jon Perritt8c93a302014-09-28 22:35:57 -0500151 ContentType string `h:"Content-Type"`
152 CopyFrom string `h:"X-Copy-From"`
153 DeleteAfter int `h:"X-Delete-After"`
154 DeleteAt int `h:"X-Delete-At"`
155 DetectContentType string `h:"X-Detect-Content-Type"`
156 ETag string `h:"ETag"`
157 IfNoneMatch string `h:"If-None-Match"`
158 ObjectManifest string `h:"X-Object-Manifest"`
159 TransferEncoding string `h:"Transfer-Encoding"`
160 Expires string `q:"expires"`
jrperritt4fcd3b72015-09-23 11:17:23 -0600161 MultipartManifest string `q:"multipart-manifest"`
Jon Perritt8c93a302014-09-28 22:35:57 -0500162 Signature string `q:"signature"`
Jon Perritt816d2a02014-03-11 20:49:46 -0500163}
164
Jon Perritte90aced2014-10-12 23:24:06 -0500165// ToObjectCreateParams formats a CreateOpts into a query string and map of
166// headers.
Jon Perrittfea90732016-03-15 02:57:05 -0500167func (opts CreateOpts) ToObjectCreateParams() (io.Reader, map[string]string, string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500168 q, err := gophercloud.BuildQueryString(opts)
169 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500170 return nil, nil, "", err
Jon Perritte90aced2014-10-12 23:24:06 -0500171 }
172 h, err := gophercloud.BuildHeaders(opts)
173 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500174 return nil, nil, "", err
Jon Perritte90aced2014-10-12 23:24:06 -0500175 }
176
177 for k, v := range opts.Metadata {
178 h["X-Object-Meta-"+k] = v
179 }
180
Jon Perrittfea90732016-03-15 02:57:05 -0500181 hash := md5.New()
182 buf := bytes.NewBuffer([]byte{})
183 _, err = io.Copy(io.MultiWriter(hash, buf), opts.Content)
184 if err != nil {
185 return nil, nil, "", err
186 }
187 localChecksum := fmt.Sprintf("%x", hash.Sum(nil))
188 h["ETag"] = localChecksum
189
190 return buf, h, q.String(), nil
Jon Perritte90aced2014-10-12 23:24:06 -0500191}
192
Jamie Hannaford50fc97d2015-07-16 12:29:01 +0200193// Create is a function that creates a new object or replaces an existing object. If the returned response's ETag
194// header fails to match the local checksum, the failed request will automatically be retried up to a maximum of 3 times.
Jon Perrittfea90732016-03-15 02:57:05 -0500195func Create(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateOptsBuilder) CreateResult {
196 var r CreateResult
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 Perrittfea90732016-03-15 02:57:05 -0500199 var b io.Reader
Jon Perrittde47eac2014-09-30 15:34:17 -0500200 if opts != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500201 tmpB, headers, query, err := opts.ToObjectCreateParams()
Jon Perrittde47eac2014-09-30 15:34:17 -0500202 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500203 r.Err = err
204 return r
Jon Perrittde47eac2014-09-30 15:34:17 -0500205 }
Jon Perrittde47eac2014-09-30 15:34:17 -0500206 for k, v := range headers {
207 h[k] = v
208 }
Jon Perritte90aced2014-10-12 23:24:06 -0500209 url += query
Jon Perrittfea90732016-03-15 02:57:05 -0500210 b = tmpB
Jon Perritt8c93a302014-09-28 22:35:57 -0500211 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500212
Jon Perrittfea90732016-03-15 02:57:05 -0500213 resp, err := c.Put(url, nil, nil, &gophercloud.RequestOpts{
214 RawBody: b,
jrperritt56d51e92015-07-30 11:50:53 -0600215 MoreHeaders: h,
Jon Perrittfea90732016-03-15 02:57:05 -0500216 })
217 r.Err = err
jrperritt433cc792015-07-31 18:53:12 -0600218 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500219 r.Header = resp.Header
Jamie Hannaford08096232015-07-13 12:47:28 +0200220 }
Jon Perrittfea90732016-03-15 02:57:05 -0500221 return r
Jon Perritt816d2a02014-03-11 20:49:46 -0500222}
223
Jon Perritte90aced2014-10-12 23:24:06 -0500224// CopyOptsBuilder allows extensions to add additional parameters to the
225// Copy request.
226type CopyOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500227 ToObjectCopyMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500228}
229
230// CopyOpts is a structure that holds parameters for copying one object to
231// another.
Jon Perritt8c93a302014-09-28 22:35:57 -0500232type CopyOpts struct {
233 Metadata map[string]string
234 ContentDisposition string `h:"Content-Disposition"`
235 ContentEncoding string `h:"Content-Encoding"`
236 ContentType string `h:"Content-Type"`
Jon Perrittfea90732016-03-15 02:57:05 -0500237 Destination string `h:"Destination" required:"true"`
Jon Perritt8c93a302014-09-28 22:35:57 -0500238}
239
Jon Perritt04851d32014-10-14 02:07:13 -0500240// ToObjectCopyMap formats a CopyOpts into a map of headers.
241func (opts CopyOpts) ToObjectCopyMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500242 h, err := gophercloud.BuildHeaders(opts)
243 if err != nil {
244 return nil, err
245 }
246 for k, v := range opts.Metadata {
247 h["X-Object-Meta-"+k] = v
248 }
249 return h, nil
250}
251
Jon Perritt816d2a02014-03-11 20:49:46 -0500252// Copy is a function that copies one object to another.
Jon Perritte90aced2014-10-12 23:24:06 -0500253func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts CopyOptsBuilder) CopyResult {
Jon Perrittfea90732016-03-15 02:57:05 -0500254 var r CopyResult
255 h := make(map[string]string)
Jon Perritt04851d32014-10-14 02:07:13 -0500256 headers, err := opts.ToObjectCopyMap()
Jon Perritt8c93a302014-09-28 22:35:57 -0500257 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500258 r.Err = err
259 return r
Jon Perritt816d2a02014-03-11 20:49:46 -0500260 }
Jon Perritte90aced2014-10-12 23:24:06 -0500261
Jon Perritt8c93a302014-09-28 22:35:57 -0500262 for k, v := range headers {
Jon Perritt816d2a02014-03-11 20:49:46 -0500263 h[k] = v
264 }
265
Jon Perrittea4e3012014-10-09 22:03:19 -0500266 url := copyURL(c, containerName, objectName)
Jon Perritta33da232016-03-02 04:43:08 -0600267 resp, err := c.Request("COPY", url, &gophercloud.RequestOpts{
Jon Perritt8c93a302014-09-28 22:35:57 -0500268 MoreHeaders: h,
269 OkCodes: []int{201},
270 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600271 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500272 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600273 }
Jon Perrittfea90732016-03-15 02:57:05 -0500274 r.Err = err
275 return r
Jon Perritt8c93a302014-09-28 22:35:57 -0500276}
277
Jon Perritte90aced2014-10-12 23:24:06 -0500278// DeleteOptsBuilder allows extensions to add additional parameters to the
279// Delete request.
280type DeleteOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500281 ToObjectDeleteQuery() (string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500282}
283
Jon Perritt8c93a302014-09-28 22:35:57 -0500284// DeleteOpts is a structure that holds parameters for deleting an object.
285type DeleteOpts struct {
286 MultipartManifest string `q:"multipart-manifest"`
287}
288
Jon Perritt26780d52014-10-14 11:35:58 -0500289// ToObjectDeleteQuery formats a DeleteOpts into a query string.
290func (opts DeleteOpts) ToObjectDeleteQuery() (string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500291 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittfea90732016-03-15 02:57:05 -0500292 return q.String(), err
Jon Perritte90aced2014-10-12 23:24:06 -0500293}
294
Jon Perritt8c93a302014-09-28 22:35:57 -0500295// Delete is a function that deletes an object.
Jon Perritte90aced2014-10-12 23:24:06 -0500296func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts DeleteOptsBuilder) DeleteResult {
Jon Perrittfea90732016-03-15 02:57:05 -0500297 var r DeleteResult
Jon Perrittea4e3012014-10-09 22:03:19 -0500298 url := deleteURL(c, containerName, objectName)
Jon Perrittde47eac2014-09-30 15:34:17 -0500299 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500300 query, err := opts.ToObjectDeleteQuery()
Jon Perrittde47eac2014-09-30 15:34:17 -0500301 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500302 r.Err = err
303 return r
Jon Perrittde47eac2014-09-30 15:34:17 -0500304 }
Jon Perritte90aced2014-10-12 23:24:06 -0500305 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500306 }
Jamie Hannaford1d27afa2015-03-24 16:20:45 +0100307 resp, err := c.Delete(url, nil)
Jon Perritta2c88b22015-05-18 11:23:30 -0600308 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500309 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600310 }
Jon Perrittfea90732016-03-15 02:57:05 -0500311 r.Err = err
312 return r
Jon Perritt8c93a302014-09-28 22:35:57 -0500313}
314
Jon Perritte90aced2014-10-12 23:24:06 -0500315// GetOptsBuilder allows extensions to add additional parameters to the
316// Get request.
317type GetOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500318 ToObjectGetQuery() (string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500319}
320
Jon Perritt8c93a302014-09-28 22:35:57 -0500321// GetOpts is a structure that holds parameters for getting an object's metadata.
322type GetOpts struct {
323 Expires string `q:"expires"`
324 Signature string `q:"signature"`
325}
326
Jon Perritt26780d52014-10-14 11:35:58 -0500327// ToObjectGetQuery formats a GetOpts into a query string.
328func (opts GetOpts) ToObjectGetQuery() (string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500329 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittfea90732016-03-15 02:57:05 -0500330 return q.String(), err
Jon Perritte90aced2014-10-12 23:24:06 -0500331}
332
Jon Perritt8c93a302014-09-28 22:35:57 -0500333// Get is a function that retrieves the metadata of an object. To extract just the custom
334// metadata, pass the GetResult response to the ExtractMetadata function.
Jon Perritte90aced2014-10-12 23:24:06 -0500335func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts GetOptsBuilder) GetResult {
Jon Perrittfea90732016-03-15 02:57:05 -0500336 var r GetResult
Jon Perrittea4e3012014-10-09 22:03:19 -0500337 url := getURL(c, containerName, objectName)
Jon Perrittde47eac2014-09-30 15:34:17 -0500338 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500339 query, err := opts.ToObjectGetQuery()
Jon Perrittde47eac2014-09-30 15:34:17 -0500340 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500341 r.Err = err
342 return r
Jon Perrittde47eac2014-09-30 15:34:17 -0500343 }
Jon Perritte90aced2014-10-12 23:24:06 -0500344 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500345 }
Jon Perritta33da232016-03-02 04:43:08 -0600346 resp, err := c.Request("HEAD", url, &gophercloud.RequestOpts{
Ash Wilson4bf41a32015-02-12 15:52:44 -0500347 OkCodes: []int{200, 204},
Jon Perritt8c93a302014-09-28 22:35:57 -0500348 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600349 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500350 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600351 }
Jon Perrittfea90732016-03-15 02:57:05 -0500352 r.Err = err
353 return r
Jon Perritt8c93a302014-09-28 22:35:57 -0500354}
355
Jon Perritte90aced2014-10-12 23:24:06 -0500356// UpdateOptsBuilder allows extensions to add additional parameters to the
357// Update request.
358type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500359 ToObjectUpdateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500360}
361
Jon Perritt8c93a302014-09-28 22:35:57 -0500362// UpdateOpts is a structure that holds parameters for updating, creating, or deleting an
363// object's metadata.
364type UpdateOpts struct {
365 Metadata map[string]string
366 ContentDisposition string `h:"Content-Disposition"`
367 ContentEncoding string `h:"Content-Encoding"`
368 ContentType string `h:"Content-Type"`
369 DeleteAfter int `h:"X-Delete-After"`
370 DeleteAt int `h:"X-Delete-At"`
371 DetectContentType bool `h:"X-Detect-Content-Type"`
372}
373
Jon Perritt04851d32014-10-14 02:07:13 -0500374// ToObjectUpdateMap formats a UpdateOpts into a map of headers.
375func (opts UpdateOpts) ToObjectUpdateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500376 h, err := gophercloud.BuildHeaders(opts)
377 if err != nil {
378 return nil, err
379 }
380 for k, v := range opts.Metadata {
381 h["X-Object-Meta-"+k] = v
382 }
383 return h, nil
384}
385
Jon Perritt8c93a302014-09-28 22:35:57 -0500386// Update is a function that creates, updates, or deletes an object's metadata.
Jon Perritte90aced2014-10-12 23:24:06 -0500387func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts UpdateOptsBuilder) UpdateResult {
Jon Perrittfea90732016-03-15 02:57:05 -0500388 var r UpdateResult
Ash Wilson77857dc2014-10-22 09:09:02 -0400389 h := c.AuthenticatedHeaders()
Jon Perrittde47eac2014-09-30 15:34:17 -0500390 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500391 headers, err := opts.ToObjectUpdateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500392 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500393 r.Err = err
394 return r
Jon Perrittde47eac2014-09-30 15:34:17 -0500395 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500396
Jon Perrittde47eac2014-09-30 15:34:17 -0500397 for k, v := range headers {
398 h[k] = v
399 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500400 }
Jon Perrittea4e3012014-10-09 22:03:19 -0500401 url := updateURL(c, containerName, objectName)
Jon Perrittfea90732016-03-15 02:57:05 -0500402 resp, err := c.Post(url, nil, nil, &gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500403 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500404 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600405 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500406 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600407 }
Jon Perrittfea90732016-03-15 02:57:05 -0500408 r.Err = err
409 return r
Jon Perritt816d2a02014-03-11 20:49:46 -0500410}
Jon Perritt90957602015-02-01 17:03:06 -0700411
412// HTTPMethod represents an HTTP method string (e.g. "GET").
413type HTTPMethod string
414
415var (
416 // GET represents an HTTP "GET" method.
417 GET HTTPMethod = "GET"
418 // POST represents an HTTP "POST" method.
419 POST HTTPMethod = "POST"
420)
421
422// CreateTempURLOpts are options for creating a temporary URL for an object.
423type CreateTempURLOpts struct {
Jon Perritt28792af2015-02-02 11:00:04 -0700424 // (REQUIRED) Method is the HTTP method to allow for users of the temp URL. Valid values
Jon Perritt90957602015-02-01 17:03:06 -0700425 // are "GET" and "POST".
426 Method HTTPMethod
Jon Perritt28792af2015-02-02 11:00:04 -0700427 // (REQUIRED) TTL is the number of seconds the temp URL should be active.
Jon Perritt90957602015-02-01 17:03:06 -0700428 TTL int
Jon Perritt28792af2015-02-02 11:00:04 -0700429 // (Optional) Split is the string on which to split the object URL. Since only
430 // the object path is used in the hash, the object URL needs to be parsed. If
431 // empty, the default OpenStack URL split point will be used ("/v1/").
432 Split string
Jon Perritt90957602015-02-01 17:03:06 -0700433}
434
435// CreateTempURL is a function for creating a temporary URL for an object. It
436// allows users to have "GET" or "POST" access to a particular tenant's object
437// for a limited amount of time.
438func CreateTempURL(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateTempURLOpts) (string, error) {
Jon Perritt28792af2015-02-02 11:00:04 -0700439 if opts.Split == "" {
440 opts.Split = "/v1/"
441 }
Jon Perritt90957602015-02-01 17:03:06 -0700442 duration := time.Duration(opts.TTL) * time.Second
443 expiry := time.Now().Add(duration).Unix()
444 getHeader, err := accounts.Get(c, nil).Extract()
445 if err != nil {
446 return "", err
447 }
448 secretKey := []byte(getHeader.TempURLKey)
449 url := getURL(c, containerName, objectName)
Jon Perritt28792af2015-02-02 11:00:04 -0700450 splitPath := strings.Split(url, opts.Split)
Jon Perritt90957602015-02-01 17:03:06 -0700451 baseURL, objectPath := splitPath[0], splitPath[1]
Jon Perritt28792af2015-02-02 11:00:04 -0700452 objectPath = opts.Split + objectPath
Jon Perritt90957602015-02-01 17:03:06 -0700453 body := fmt.Sprintf("%s\n%d\n%s", opts.Method, expiry, objectPath)
454 hash := hmac.New(sha1.New, secretKey)
455 hash.Write([]byte(body))
456 hexsum := fmt.Sprintf("%x", hash.Sum(nil))
457 return fmt.Sprintf("%s%s?temp_url_sig=%s&temp_url_expires=%d", baseURL, objectPath, hexsum, expiry), nil
458}