blob: 5f8e7c1da0d45bb9cc8885bc5b21e709f6b60eb7 [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.
Jon Perritt3860b512016-03-29 12:01:48 -0500110func Download(c *gophercloud.ServiceClient, containerName, objectName string, opts DownloadOptsBuilder) (r DownloadResult) {
Jon Perrittea4e3012014-10-09 22:03:19 -0500111 url := downloadURL(c, containerName, objectName)
Jon Perrittfea90732016-03-15 02:57:05 -0500112 h := make(map[string]string)
Jon Perrittde47eac2014-09-30 15:34:17 -0500113 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -0500114 headers, query, err := opts.ToObjectDownloadParams()
Jon Perrittde47eac2014-09-30 15:34:17 -0500115 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500116 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500117 return
Jon Perrittde47eac2014-09-30 15:34:17 -0500118 }
Jon Perrittde47eac2014-09-30 15:34:17 -0500119 for k, v := range headers {
120 h[k] = v
121 }
Jon Perritte90aced2014-10-12 23:24:06 -0500122 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500123 }
124
Jon Perrittfea90732016-03-15 02:57:05 -0500125 resp, err := c.Get(url, nil, &gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500126 MoreHeaders: h,
Paul Querna9163df22014-11-01 09:38:51 -0700127 OkCodes: []int{200, 304},
Jon Perritt816d2a02014-03-11 20:49:46 -0500128 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600129 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500130 r.Header = resp.Header
131 r.Body = resp.Body
Jon Perritta2c88b22015-05-18 11:23:30 -0600132 }
Jon Perrittfea90732016-03-15 02:57:05 -0500133 r.Err = err
Jon Perritt8c93a302014-09-28 22:35:57 -0500134}
135
Jon Perritte90aced2014-10-12 23:24:06 -0500136// CreateOptsBuilder allows extensions to add additional parameters to the
137// Create request.
138type CreateOptsBuilder interface {
Jon Perrittfea90732016-03-15 02:57:05 -0500139 ToObjectCreateParams() (io.Reader, map[string]string, string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500140}
141
Jon Perritt8c93a302014-09-28 22:35:57 -0500142// CreateOpts is a structure that holds parameters for creating an object.
143type CreateOpts struct {
Jon Perrittfea90732016-03-15 02:57:05 -0500144 Content io.Reader
Jon Perritt8c93a302014-09-28 22:35:57 -0500145 Metadata map[string]string
146 ContentDisposition string `h:"Content-Disposition"`
147 ContentEncoding string `h:"Content-Encoding"`
Jon Perritte376fa52014-11-03 11:35:48 -0600148 ContentLength int64 `h:"Content-Length"`
Jon Perritt8c93a302014-09-28 22:35:57 -0500149 ContentType string `h:"Content-Type"`
150 CopyFrom string `h:"X-Copy-From"`
151 DeleteAfter int `h:"X-Delete-After"`
152 DeleteAt int `h:"X-Delete-At"`
153 DetectContentType string `h:"X-Detect-Content-Type"`
154 ETag string `h:"ETag"`
155 IfNoneMatch string `h:"If-None-Match"`
156 ObjectManifest string `h:"X-Object-Manifest"`
157 TransferEncoding string `h:"Transfer-Encoding"`
158 Expires string `q:"expires"`
jrperritt4fcd3b72015-09-23 11:17:23 -0600159 MultipartManifest string `q:"multipart-manifest"`
Jon Perritt8c93a302014-09-28 22:35:57 -0500160 Signature string `q:"signature"`
Jon Perritt816d2a02014-03-11 20:49:46 -0500161}
162
Jon Perritte90aced2014-10-12 23:24:06 -0500163// ToObjectCreateParams formats a CreateOpts into a query string and map of
164// headers.
Jon Perrittfea90732016-03-15 02:57:05 -0500165func (opts CreateOpts) ToObjectCreateParams() (io.Reader, map[string]string, string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500166 q, err := gophercloud.BuildQueryString(opts)
167 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500168 return nil, nil, "", err
Jon Perritte90aced2014-10-12 23:24:06 -0500169 }
170 h, err := gophercloud.BuildHeaders(opts)
171 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500172 return nil, nil, "", err
Jon Perritte90aced2014-10-12 23:24:06 -0500173 }
174
175 for k, v := range opts.Metadata {
176 h["X-Object-Meta-"+k] = v
177 }
178
Jon Perrittfea90732016-03-15 02:57:05 -0500179 hash := md5.New()
180 buf := bytes.NewBuffer([]byte{})
181 _, err = io.Copy(io.MultiWriter(hash, buf), opts.Content)
182 if err != nil {
183 return nil, nil, "", err
184 }
185 localChecksum := fmt.Sprintf("%x", hash.Sum(nil))
186 h["ETag"] = localChecksum
187
188 return buf, h, q.String(), nil
Jon Perritte90aced2014-10-12 23:24:06 -0500189}
190
Jamie Hannaford50fc97d2015-07-16 12:29:01 +0200191// Create is a function that creates a new object or replaces an existing object. If the returned response's ETag
192// header fails to match the local checksum, the failed request will automatically be retried up to a maximum of 3 times.
Jon Perritt3860b512016-03-29 12:01:48 -0500193func Create(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittea4e3012014-10-09 22:03:19 -0500194 url := createURL(c, containerName, objectName)
Ash Wilson322a7e62015-02-12 16:25:26 -0500195 h := make(map[string]string)
Jon Perrittfea90732016-03-15 02:57:05 -0500196 var b io.Reader
Jon Perrittde47eac2014-09-30 15:34:17 -0500197 if opts != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500198 tmpB, headers, query, err := opts.ToObjectCreateParams()
Jon Perrittde47eac2014-09-30 15:34:17 -0500199 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500200 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500201 return
Jon Perrittde47eac2014-09-30 15:34:17 -0500202 }
Jon Perrittde47eac2014-09-30 15:34:17 -0500203 for k, v := range headers {
204 h[k] = v
205 }
Jon Perritte90aced2014-10-12 23:24:06 -0500206 url += query
Jon Perrittfea90732016-03-15 02:57:05 -0500207 b = tmpB
Jon Perritt8c93a302014-09-28 22:35:57 -0500208 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500209
Jon Perrittfea90732016-03-15 02:57:05 -0500210 resp, err := c.Put(url, nil, nil, &gophercloud.RequestOpts{
211 RawBody: b,
jrperritt56d51e92015-07-30 11:50:53 -0600212 MoreHeaders: h,
Jon Perrittfea90732016-03-15 02:57:05 -0500213 })
214 r.Err = err
jrperritt433cc792015-07-31 18:53:12 -0600215 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500216 r.Header = resp.Header
Jamie Hannaford08096232015-07-13 12:47:28 +0200217 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500218}
219
Jon Perritte90aced2014-10-12 23:24:06 -0500220// CopyOptsBuilder allows extensions to add additional parameters to the
221// Copy request.
222type CopyOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500223 ToObjectCopyMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500224}
225
226// CopyOpts is a structure that holds parameters for copying one object to
227// another.
Jon Perritt8c93a302014-09-28 22:35:57 -0500228type CopyOpts struct {
229 Metadata map[string]string
230 ContentDisposition string `h:"Content-Disposition"`
231 ContentEncoding string `h:"Content-Encoding"`
232 ContentType string `h:"Content-Type"`
Jon Perrittfea90732016-03-15 02:57:05 -0500233 Destination string `h:"Destination" required:"true"`
Jon Perritt8c93a302014-09-28 22:35:57 -0500234}
235
Jon Perritt04851d32014-10-14 02:07:13 -0500236// ToObjectCopyMap formats a CopyOpts into a map of headers.
237func (opts CopyOpts) ToObjectCopyMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500238 h, err := gophercloud.BuildHeaders(opts)
239 if err != nil {
240 return nil, err
241 }
242 for k, v := range opts.Metadata {
243 h["X-Object-Meta-"+k] = v
244 }
245 return h, nil
246}
247
Jon Perritt816d2a02014-03-11 20:49:46 -0500248// Copy is a function that copies one object to another.
Jon Perritt3860b512016-03-29 12:01:48 -0500249func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts CopyOptsBuilder) (r CopyResult) {
Jon Perrittfea90732016-03-15 02:57:05 -0500250 h := make(map[string]string)
Jon Perritt04851d32014-10-14 02:07:13 -0500251 headers, err := opts.ToObjectCopyMap()
Jon Perritt8c93a302014-09-28 22:35:57 -0500252 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500253 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500254 return
Jon Perritt816d2a02014-03-11 20:49:46 -0500255 }
Jon Perritte90aced2014-10-12 23:24:06 -0500256
Jon Perritt8c93a302014-09-28 22:35:57 -0500257 for k, v := range headers {
Jon Perritt816d2a02014-03-11 20:49:46 -0500258 h[k] = v
259 }
260
Jon Perrittea4e3012014-10-09 22:03:19 -0500261 url := copyURL(c, containerName, objectName)
Jon Perritta33da232016-03-02 04:43:08 -0600262 resp, err := c.Request("COPY", url, &gophercloud.RequestOpts{
Jon Perritt8c93a302014-09-28 22:35:57 -0500263 MoreHeaders: h,
264 OkCodes: []int{201},
265 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600266 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500267 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600268 }
Jon Perrittfea90732016-03-15 02:57:05 -0500269 r.Err = err
Jon Perritt8c93a302014-09-28 22:35:57 -0500270}
271
Jon Perritte90aced2014-10-12 23:24:06 -0500272// DeleteOptsBuilder allows extensions to add additional parameters to the
273// Delete request.
274type DeleteOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500275 ToObjectDeleteQuery() (string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500276}
277
Jon Perritt8c93a302014-09-28 22:35:57 -0500278// DeleteOpts is a structure that holds parameters for deleting an object.
279type DeleteOpts struct {
280 MultipartManifest string `q:"multipart-manifest"`
281}
282
Jon Perritt26780d52014-10-14 11:35:58 -0500283// ToObjectDeleteQuery formats a DeleteOpts into a query string.
284func (opts DeleteOpts) ToObjectDeleteQuery() (string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500285 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittfea90732016-03-15 02:57:05 -0500286 return q.String(), err
Jon Perritte90aced2014-10-12 23:24:06 -0500287}
288
Jon Perritt8c93a302014-09-28 22:35:57 -0500289// Delete is a function that deletes an object.
Jon Perritt3860b512016-03-29 12:01:48 -0500290func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts DeleteOptsBuilder) (r DeleteResult) {
Jon Perrittea4e3012014-10-09 22:03:19 -0500291 url := deleteURL(c, containerName, objectName)
Jon Perrittde47eac2014-09-30 15:34:17 -0500292 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500293 query, err := opts.ToObjectDeleteQuery()
Jon Perrittde47eac2014-09-30 15:34:17 -0500294 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500295 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500296 return
Jon Perrittde47eac2014-09-30 15:34:17 -0500297 }
Jon Perritte90aced2014-10-12 23:24:06 -0500298 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500299 }
Jamie Hannaford1d27afa2015-03-24 16:20:45 +0100300 resp, err := c.Delete(url, nil)
Jon Perritta2c88b22015-05-18 11:23:30 -0600301 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500302 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600303 }
Jon Perrittfea90732016-03-15 02:57:05 -0500304 r.Err = err
Jon Perritt8c93a302014-09-28 22:35:57 -0500305}
306
Jon Perritte90aced2014-10-12 23:24:06 -0500307// GetOptsBuilder allows extensions to add additional parameters to the
308// Get request.
309type GetOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500310 ToObjectGetQuery() (string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500311}
312
Jon Perritt8c93a302014-09-28 22:35:57 -0500313// GetOpts is a structure that holds parameters for getting an object's metadata.
314type GetOpts struct {
315 Expires string `q:"expires"`
316 Signature string `q:"signature"`
317}
318
Jon Perritt26780d52014-10-14 11:35:58 -0500319// ToObjectGetQuery formats a GetOpts into a query string.
320func (opts GetOpts) ToObjectGetQuery() (string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500321 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittfea90732016-03-15 02:57:05 -0500322 return q.String(), err
Jon Perritte90aced2014-10-12 23:24:06 -0500323}
324
Jon Perritt8c93a302014-09-28 22:35:57 -0500325// Get is a function that retrieves the metadata of an object. To extract just the custom
326// metadata, pass the GetResult response to the ExtractMetadata function.
Jon Perritt3860b512016-03-29 12:01:48 -0500327func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts GetOptsBuilder) (r GetResult) {
Jon Perrittea4e3012014-10-09 22:03:19 -0500328 url := getURL(c, containerName, objectName)
Jon Perrittde47eac2014-09-30 15:34:17 -0500329 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500330 query, err := opts.ToObjectGetQuery()
Jon Perrittde47eac2014-09-30 15:34:17 -0500331 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500332 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500333 return
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 }
Jon Perritta33da232016-03-02 04:43:08 -0600337 resp, err := c.Request("HEAD", url, &gophercloud.RequestOpts{
Ash Wilson4bf41a32015-02-12 15:52:44 -0500338 OkCodes: []int{200, 204},
Jon Perritt8c93a302014-09-28 22:35:57 -0500339 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600340 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500341 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600342 }
Jon Perrittfea90732016-03-15 02:57:05 -0500343 r.Err = err
Jon Perritt8c93a302014-09-28 22:35:57 -0500344}
345
Jon Perritte90aced2014-10-12 23:24:06 -0500346// UpdateOptsBuilder allows extensions to add additional parameters to the
347// Update request.
348type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500349 ToObjectUpdateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500350}
351
Jon Perritt8c93a302014-09-28 22:35:57 -0500352// UpdateOpts is a structure that holds parameters for updating, creating, or deleting an
353// object's metadata.
354type UpdateOpts struct {
355 Metadata map[string]string
356 ContentDisposition string `h:"Content-Disposition"`
357 ContentEncoding string `h:"Content-Encoding"`
358 ContentType string `h:"Content-Type"`
359 DeleteAfter int `h:"X-Delete-After"`
360 DeleteAt int `h:"X-Delete-At"`
361 DetectContentType bool `h:"X-Detect-Content-Type"`
362}
363
Jon Perritt04851d32014-10-14 02:07:13 -0500364// ToObjectUpdateMap formats a UpdateOpts into a map of headers.
365func (opts UpdateOpts) ToObjectUpdateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500366 h, err := gophercloud.BuildHeaders(opts)
367 if err != nil {
368 return nil, err
369 }
370 for k, v := range opts.Metadata {
371 h["X-Object-Meta-"+k] = v
372 }
373 return h, nil
374}
375
Jon Perritt8c93a302014-09-28 22:35:57 -0500376// Update is a function that creates, updates, or deletes an object's metadata.
Jon Perritt3860b512016-03-29 12:01:48 -0500377func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts UpdateOptsBuilder) (r UpdateResult) {
Ash Wilson77857dc2014-10-22 09:09:02 -0400378 h := c.AuthenticatedHeaders()
Jon Perrittde47eac2014-09-30 15:34:17 -0500379 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500380 headers, err := opts.ToObjectUpdateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500381 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500382 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500383 return
Jon Perrittde47eac2014-09-30 15:34:17 -0500384 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500385
Jon Perrittde47eac2014-09-30 15:34:17 -0500386 for k, v := range headers {
387 h[k] = v
388 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500389 }
Jon Perrittea4e3012014-10-09 22:03:19 -0500390 url := updateURL(c, containerName, objectName)
Jon Perrittfea90732016-03-15 02:57:05 -0500391 resp, err := c.Post(url, nil, nil, &gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500392 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500393 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600394 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500395 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600396 }
Jon Perrittfea90732016-03-15 02:57:05 -0500397 r.Err = err
Jon Perritt816d2a02014-03-11 20:49:46 -0500398}
Jon Perritt90957602015-02-01 17:03:06 -0700399
400// HTTPMethod represents an HTTP method string (e.g. "GET").
401type HTTPMethod string
402
403var (
404 // GET represents an HTTP "GET" method.
405 GET HTTPMethod = "GET"
406 // POST represents an HTTP "POST" method.
407 POST HTTPMethod = "POST"
408)
409
410// CreateTempURLOpts are options for creating a temporary URL for an object.
411type CreateTempURLOpts struct {
Jon Perritt28792af2015-02-02 11:00:04 -0700412 // (REQUIRED) Method is the HTTP method to allow for users of the temp URL. Valid values
Jon Perritt90957602015-02-01 17:03:06 -0700413 // are "GET" and "POST".
414 Method HTTPMethod
Jon Perritt28792af2015-02-02 11:00:04 -0700415 // (REQUIRED) TTL is the number of seconds the temp URL should be active.
Jon Perritt90957602015-02-01 17:03:06 -0700416 TTL int
Jon Perritt28792af2015-02-02 11:00:04 -0700417 // (Optional) Split is the string on which to split the object URL. Since only
418 // the object path is used in the hash, the object URL needs to be parsed. If
419 // empty, the default OpenStack URL split point will be used ("/v1/").
420 Split string
Jon Perritt90957602015-02-01 17:03:06 -0700421}
422
423// CreateTempURL is a function for creating a temporary URL for an object. It
424// allows users to have "GET" or "POST" access to a particular tenant's object
425// for a limited amount of time.
426func CreateTempURL(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateTempURLOpts) (string, error) {
Jon Perritt28792af2015-02-02 11:00:04 -0700427 if opts.Split == "" {
428 opts.Split = "/v1/"
429 }
Jon Perritt90957602015-02-01 17:03:06 -0700430 duration := time.Duration(opts.TTL) * time.Second
431 expiry := time.Now().Add(duration).Unix()
432 getHeader, err := accounts.Get(c, nil).Extract()
433 if err != nil {
434 return "", err
435 }
436 secretKey := []byte(getHeader.TempURLKey)
437 url := getURL(c, containerName, objectName)
Jon Perritt28792af2015-02-02 11:00:04 -0700438 splitPath := strings.Split(url, opts.Split)
Jon Perritt90957602015-02-01 17:03:06 -0700439 baseURL, objectPath := splitPath[0], splitPath[1]
Jon Perritt28792af2015-02-02 11:00:04 -0700440 objectPath = opts.Split + objectPath
Jon Perritt90957602015-02-01 17:03:06 -0700441 body := fmt.Sprintf("%s\n%d\n%s", opts.Method, expiry, objectPath)
442 hash := hmac.New(sha1.New, secretKey)
443 hash.Write([]byte(body))
444 hexsum := fmt.Sprintf("%x", hash.Sum(nil))
445 return fmt.Sprintf("%s%s?temp_url_sig=%s&temp_url_expires=%d", baseURL, objectPath, hexsum, expiry), nil
446}