blob: 99ad9a7a2dcca891d3f83767d6a2a8834c62c316 [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
jrperritt29ae6b32016-04-13 12:59:37 -0500134 return
Jon Perritt8c93a302014-09-28 22:35:57 -0500135}
136
Jon Perritte90aced2014-10-12 23:24:06 -0500137// CreateOptsBuilder allows extensions to add additional parameters to the
138// Create request.
139type CreateOptsBuilder interface {
Jon Perrittfea90732016-03-15 02:57:05 -0500140 ToObjectCreateParams() (io.Reader, map[string]string, string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500141}
142
Jon Perritt8c93a302014-09-28 22:35:57 -0500143// CreateOpts is a structure that holds parameters for creating an object.
144type CreateOpts struct {
Jon Perrittfea90732016-03-15 02:57:05 -0500145 Content io.Reader
Jon Perritt8c93a302014-09-28 22:35:57 -0500146 Metadata map[string]string
147 ContentDisposition string `h:"Content-Disposition"`
148 ContentEncoding string `h:"Content-Encoding"`
Jon Perritte376fa52014-11-03 11:35:48 -0600149 ContentLength int64 `h:"Content-Length"`
Jon Perritt8c93a302014-09-28 22:35:57 -0500150 ContentType string `h:"Content-Type"`
151 CopyFrom string `h:"X-Copy-From"`
152 DeleteAfter int `h:"X-Delete-After"`
153 DeleteAt int `h:"X-Delete-At"`
154 DetectContentType string `h:"X-Detect-Content-Type"`
155 ETag string `h:"ETag"`
156 IfNoneMatch string `h:"If-None-Match"`
157 ObjectManifest string `h:"X-Object-Manifest"`
158 TransferEncoding string `h:"Transfer-Encoding"`
159 Expires string `q:"expires"`
jrperritt4fcd3b72015-09-23 11:17:23 -0600160 MultipartManifest string `q:"multipart-manifest"`
Jon Perritt8c93a302014-09-28 22:35:57 -0500161 Signature string `q:"signature"`
Jon Perritt816d2a02014-03-11 20:49:46 -0500162}
163
Jon Perritte90aced2014-10-12 23:24:06 -0500164// ToObjectCreateParams formats a CreateOpts into a query string and map of
165// headers.
Jon Perrittfea90732016-03-15 02:57:05 -0500166func (opts CreateOpts) ToObjectCreateParams() (io.Reader, map[string]string, string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500167 q, err := gophercloud.BuildQueryString(opts)
168 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500169 return nil, nil, "", err
Jon Perritte90aced2014-10-12 23:24:06 -0500170 }
171 h, err := gophercloud.BuildHeaders(opts)
172 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500173 return nil, nil, "", err
Jon Perritte90aced2014-10-12 23:24:06 -0500174 }
175
176 for k, v := range opts.Metadata {
177 h["X-Object-Meta-"+k] = v
178 }
179
Jon Perrittfea90732016-03-15 02:57:05 -0500180 hash := md5.New()
181 buf := bytes.NewBuffer([]byte{})
182 _, err = io.Copy(io.MultiWriter(hash, buf), opts.Content)
183 if err != nil {
184 return nil, nil, "", err
185 }
186 localChecksum := fmt.Sprintf("%x", hash.Sum(nil))
187 h["ETag"] = localChecksum
188
189 return buf, h, q.String(), nil
Jon Perritte90aced2014-10-12 23:24:06 -0500190}
191
Jamie Hannaford50fc97d2015-07-16 12:29:01 +0200192// Create is a function that creates a new object or replaces an existing object. If the returned response's ETag
193// 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 -0500194func Create(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateOptsBuilder) (r CreateResult) {
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 Perrittfea90732016-03-15 02:57:05 -0500197 var b io.Reader
Jon Perrittde47eac2014-09-30 15:34:17 -0500198 if opts != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500199 tmpB, headers, query, err := opts.ToObjectCreateParams()
Jon Perrittde47eac2014-09-30 15:34:17 -0500200 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500201 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500202 return
Jon Perrittde47eac2014-09-30 15:34:17 -0500203 }
Jon Perrittde47eac2014-09-30 15:34:17 -0500204 for k, v := range headers {
205 h[k] = v
206 }
Jon Perritte90aced2014-10-12 23:24:06 -0500207 url += query
Jon Perrittfea90732016-03-15 02:57:05 -0500208 b = tmpB
Jon Perritt8c93a302014-09-28 22:35:57 -0500209 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500210
Jon Perrittfea90732016-03-15 02:57:05 -0500211 resp, err := c.Put(url, nil, nil, &gophercloud.RequestOpts{
212 RawBody: b,
jrperritt56d51e92015-07-30 11:50:53 -0600213 MoreHeaders: h,
Jon Perrittfea90732016-03-15 02:57:05 -0500214 })
215 r.Err = err
jrperritt433cc792015-07-31 18:53:12 -0600216 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500217 r.Header = resp.Header
Jamie Hannaford08096232015-07-13 12:47:28 +0200218 }
jrperritt29ae6b32016-04-13 12:59:37 -0500219 return
Jon Perritt816d2a02014-03-11 20:49:46 -0500220}
221
Jon Perritte90aced2014-10-12 23:24:06 -0500222// CopyOptsBuilder allows extensions to add additional parameters to the
223// Copy request.
224type CopyOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500225 ToObjectCopyMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500226}
227
228// CopyOpts is a structure that holds parameters for copying one object to
229// another.
Jon Perritt8c93a302014-09-28 22:35:57 -0500230type CopyOpts struct {
231 Metadata map[string]string
232 ContentDisposition string `h:"Content-Disposition"`
233 ContentEncoding string `h:"Content-Encoding"`
234 ContentType string `h:"Content-Type"`
Jon Perrittfea90732016-03-15 02:57:05 -0500235 Destination string `h:"Destination" required:"true"`
Jon Perritt8c93a302014-09-28 22:35:57 -0500236}
237
Jon Perritt04851d32014-10-14 02:07:13 -0500238// ToObjectCopyMap formats a CopyOpts into a map of headers.
239func (opts CopyOpts) ToObjectCopyMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500240 h, err := gophercloud.BuildHeaders(opts)
241 if err != nil {
242 return nil, err
243 }
244 for k, v := range opts.Metadata {
245 h["X-Object-Meta-"+k] = v
246 }
247 return h, nil
248}
249
Jon Perritt816d2a02014-03-11 20:49:46 -0500250// Copy is a function that copies one object to another.
Jon Perritt3860b512016-03-29 12:01:48 -0500251func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts CopyOptsBuilder) (r CopyResult) {
Jon Perrittfea90732016-03-15 02:57:05 -0500252 h := make(map[string]string)
Jon Perritt04851d32014-10-14 02:07:13 -0500253 headers, err := opts.ToObjectCopyMap()
Jon Perritt8c93a302014-09-28 22:35:57 -0500254 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500255 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500256 return
Jon Perritt816d2a02014-03-11 20:49:46 -0500257 }
Jon Perritte90aced2014-10-12 23:24:06 -0500258
Jon Perritt8c93a302014-09-28 22:35:57 -0500259 for k, v := range headers {
Jon Perritt816d2a02014-03-11 20:49:46 -0500260 h[k] = v
261 }
262
Jon Perrittea4e3012014-10-09 22:03:19 -0500263 url := copyURL(c, containerName, objectName)
Jon Perritta33da232016-03-02 04:43:08 -0600264 resp, err := c.Request("COPY", url, &gophercloud.RequestOpts{
Jon Perritt8c93a302014-09-28 22:35:57 -0500265 MoreHeaders: h,
266 OkCodes: []int{201},
267 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600268 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500269 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600270 }
Jon Perrittfea90732016-03-15 02:57:05 -0500271 r.Err = err
jrperritt29ae6b32016-04-13 12:59:37 -0500272 return
Jon Perritt8c93a302014-09-28 22:35:57 -0500273}
274
Jon Perritte90aced2014-10-12 23:24:06 -0500275// DeleteOptsBuilder allows extensions to add additional parameters to the
276// Delete request.
277type DeleteOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500278 ToObjectDeleteQuery() (string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500279}
280
Jon Perritt8c93a302014-09-28 22:35:57 -0500281// DeleteOpts is a structure that holds parameters for deleting an object.
282type DeleteOpts struct {
283 MultipartManifest string `q:"multipart-manifest"`
284}
285
Jon Perritt26780d52014-10-14 11:35:58 -0500286// ToObjectDeleteQuery formats a DeleteOpts into a query string.
287func (opts DeleteOpts) ToObjectDeleteQuery() (string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500288 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittfea90732016-03-15 02:57:05 -0500289 return q.String(), err
Jon Perritte90aced2014-10-12 23:24:06 -0500290}
291
Jon Perritt8c93a302014-09-28 22:35:57 -0500292// Delete is a function that deletes an object.
Jon Perritt3860b512016-03-29 12:01:48 -0500293func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts DeleteOptsBuilder) (r DeleteResult) {
Jon Perrittea4e3012014-10-09 22:03:19 -0500294 url := deleteURL(c, containerName, objectName)
Jon Perrittde47eac2014-09-30 15:34:17 -0500295 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500296 query, err := opts.ToObjectDeleteQuery()
Jon Perrittde47eac2014-09-30 15:34:17 -0500297 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500298 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500299 return
Jon Perrittde47eac2014-09-30 15:34:17 -0500300 }
Jon Perritte90aced2014-10-12 23:24:06 -0500301 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500302 }
Jamie Hannaford1d27afa2015-03-24 16:20:45 +0100303 resp, err := c.Delete(url, nil)
Jon Perritta2c88b22015-05-18 11:23:30 -0600304 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500305 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600306 }
Jon Perrittfea90732016-03-15 02:57:05 -0500307 r.Err = err
jrperritt29ae6b32016-04-13 12:59:37 -0500308 return
Jon Perritt8c93a302014-09-28 22:35:57 -0500309}
310
Jon Perritte90aced2014-10-12 23:24:06 -0500311// GetOptsBuilder allows extensions to add additional parameters to the
312// Get request.
313type GetOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500314 ToObjectGetQuery() (string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500315}
316
Jon Perritt8c93a302014-09-28 22:35:57 -0500317// GetOpts is a structure that holds parameters for getting an object's metadata.
318type GetOpts struct {
319 Expires string `q:"expires"`
320 Signature string `q:"signature"`
321}
322
Jon Perritt26780d52014-10-14 11:35:58 -0500323// ToObjectGetQuery formats a GetOpts into a query string.
324func (opts GetOpts) ToObjectGetQuery() (string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500325 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittfea90732016-03-15 02:57:05 -0500326 return q.String(), err
Jon Perritte90aced2014-10-12 23:24:06 -0500327}
328
Jon Perritt8c93a302014-09-28 22:35:57 -0500329// Get is a function that retrieves the metadata of an object. To extract just the custom
330// metadata, pass the GetResult response to the ExtractMetadata function.
Jon Perritt3860b512016-03-29 12:01:48 -0500331func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts GetOptsBuilder) (r GetResult) {
Jon Perrittea4e3012014-10-09 22:03:19 -0500332 url := getURL(c, containerName, objectName)
Jon Perrittde47eac2014-09-30 15:34:17 -0500333 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500334 query, err := opts.ToObjectGetQuery()
Jon Perrittde47eac2014-09-30 15:34:17 -0500335 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500336 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500337 return
Jon Perrittde47eac2014-09-30 15:34:17 -0500338 }
Jon Perritte90aced2014-10-12 23:24:06 -0500339 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500340 }
Jon Perritta33da232016-03-02 04:43:08 -0600341 resp, err := c.Request("HEAD", url, &gophercloud.RequestOpts{
Ash Wilson4bf41a32015-02-12 15:52:44 -0500342 OkCodes: []int{200, 204},
Jon Perritt8c93a302014-09-28 22:35:57 -0500343 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600344 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500345 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600346 }
Jon Perrittfea90732016-03-15 02:57:05 -0500347 r.Err = err
jrperritt29ae6b32016-04-13 12:59:37 -0500348 return
Jon Perritt8c93a302014-09-28 22:35:57 -0500349}
350
Jon Perritte90aced2014-10-12 23:24:06 -0500351// UpdateOptsBuilder allows extensions to add additional parameters to the
352// Update request.
353type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500354 ToObjectUpdateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500355}
356
Jon Perritt8c93a302014-09-28 22:35:57 -0500357// UpdateOpts is a structure that holds parameters for updating, creating, or deleting an
358// object's metadata.
359type UpdateOpts struct {
360 Metadata map[string]string
361 ContentDisposition string `h:"Content-Disposition"`
362 ContentEncoding string `h:"Content-Encoding"`
363 ContentType string `h:"Content-Type"`
364 DeleteAfter int `h:"X-Delete-After"`
365 DeleteAt int `h:"X-Delete-At"`
366 DetectContentType bool `h:"X-Detect-Content-Type"`
367}
368
Jon Perritt04851d32014-10-14 02:07:13 -0500369// ToObjectUpdateMap formats a UpdateOpts into a map of headers.
370func (opts UpdateOpts) ToObjectUpdateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500371 h, err := gophercloud.BuildHeaders(opts)
372 if err != nil {
373 return nil, err
374 }
375 for k, v := range opts.Metadata {
376 h["X-Object-Meta-"+k] = v
377 }
378 return h, nil
379}
380
Jon Perritt8c93a302014-09-28 22:35:57 -0500381// Update is a function that creates, updates, or deletes an object's metadata.
Jon Perritt3860b512016-03-29 12:01:48 -0500382func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts UpdateOptsBuilder) (r UpdateResult) {
Ash Wilson77857dc2014-10-22 09:09:02 -0400383 h := c.AuthenticatedHeaders()
Jon Perrittde47eac2014-09-30 15:34:17 -0500384 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500385 headers, err := opts.ToObjectUpdateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500386 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500387 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500388 return
Jon Perrittde47eac2014-09-30 15:34:17 -0500389 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500390
Jon Perrittde47eac2014-09-30 15:34:17 -0500391 for k, v := range headers {
392 h[k] = v
393 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500394 }
Jon Perrittea4e3012014-10-09 22:03:19 -0500395 url := updateURL(c, containerName, objectName)
Jon Perrittfea90732016-03-15 02:57:05 -0500396 resp, err := c.Post(url, nil, nil, &gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500397 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500398 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600399 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500400 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600401 }
Jon Perrittfea90732016-03-15 02:57:05 -0500402 r.Err = err
jrperritt29ae6b32016-04-13 12:59:37 -0500403 return
Jon Perritt816d2a02014-03-11 20:49:46 -0500404}
Jon Perritt90957602015-02-01 17:03:06 -0700405
406// HTTPMethod represents an HTTP method string (e.g. "GET").
407type HTTPMethod string
408
409var (
410 // GET represents an HTTP "GET" method.
411 GET HTTPMethod = "GET"
412 // POST represents an HTTP "POST" method.
413 POST HTTPMethod = "POST"
414)
415
416// CreateTempURLOpts are options for creating a temporary URL for an object.
417type CreateTempURLOpts struct {
Jon Perritt28792af2015-02-02 11:00:04 -0700418 // (REQUIRED) Method is the HTTP method to allow for users of the temp URL. Valid values
Jon Perritt90957602015-02-01 17:03:06 -0700419 // are "GET" and "POST".
420 Method HTTPMethod
Jon Perritt28792af2015-02-02 11:00:04 -0700421 // (REQUIRED) TTL is the number of seconds the temp URL should be active.
Jon Perritt90957602015-02-01 17:03:06 -0700422 TTL int
Jon Perritt28792af2015-02-02 11:00:04 -0700423 // (Optional) Split is the string on which to split the object URL. Since only
424 // the object path is used in the hash, the object URL needs to be parsed. If
425 // empty, the default OpenStack URL split point will be used ("/v1/").
426 Split string
Jon Perritt90957602015-02-01 17:03:06 -0700427}
428
429// CreateTempURL is a function for creating a temporary URL for an object. It
430// allows users to have "GET" or "POST" access to a particular tenant's object
431// for a limited amount of time.
432func CreateTempURL(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateTempURLOpts) (string, error) {
Jon Perritt28792af2015-02-02 11:00:04 -0700433 if opts.Split == "" {
434 opts.Split = "/v1/"
435 }
Jon Perritt90957602015-02-01 17:03:06 -0700436 duration := time.Duration(opts.TTL) * time.Second
437 expiry := time.Now().Add(duration).Unix()
438 getHeader, err := accounts.Get(c, nil).Extract()
439 if err != nil {
440 return "", err
441 }
442 secretKey := []byte(getHeader.TempURLKey)
443 url := getURL(c, containerName, objectName)
Jon Perritt28792af2015-02-02 11:00:04 -0700444 splitPath := strings.Split(url, opts.Split)
Jon Perritt90957602015-02-01 17:03:06 -0700445 baseURL, objectPath := splitPath[0], splitPath[1]
Jon Perritt28792af2015-02-02 11:00:04 -0700446 objectPath = opts.Split + objectPath
Jon Perritt90957602015-02-01 17:03:06 -0700447 body := fmt.Sprintf("%s\n%d\n%s", opts.Method, expiry, objectPath)
448 hash := hmac.New(sha1.New, secretKey)
449 hash.Write([]byte(body))
450 hexsum := fmt.Sprintf("%x", hash.Sum(nil))
451 return fmt.Sprintf("%s%s?temp_url_sig=%s&temp_url_expires=%d", baseURL, objectPath, hexsum, expiry), nil
452}