blob: 8c5d4bcb2d04323e63d4e1da0a35a8eca73cea68 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package objects
2
3import (
jrperritt05e31e62015-09-22 21:00:33 -06004 "bufio"
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"
jrperritt05e31e62015-09-22 21:00:33 -060010 "io/ioutil"
Jon Perritt90957602015-02-01 17:03:06 -070011 "strings"
Jon Perritt8c93a302014-09-28 22:35:57 -050012 "time"
Ash Wilson604320e2014-09-10 16:02:28 -040013
jrperrittd200ea32015-07-30 12:23:28 -060014 "github.com/rackspace/gophercloud"
15 "github.com/rackspace/gophercloud/openstack/objectstorage/v1/accounts"
16 "github.com/rackspace/gophercloud/pagination"
Jon Perritt816d2a02014-03-11 20:49:46 -050017)
18
Jon Perritte90aced2014-10-12 23:24:06 -050019// ListOptsBuilder allows extensions to add additional parameters to the List
20// request.
21type ListOptsBuilder interface {
22 ToObjectListParams() (bool, string, error)
23}
24
Jon Perritt8c93a302014-09-28 22:35:57 -050025// ListOpts is a structure that holds parameters for listing objects.
26type ListOpts struct {
Jon Perritt9415ca72014-11-03 11:58:48 -060027 // Full is a true/false value that represents the amount of object information
28 // returned. If Full is set to true, then the content-type, number of bytes, hash
29 // date last modified, and name are returned. If set to false or not set, then
30 // only the object names are returned.
Jon Perritt8c93a302014-09-28 22:35:57 -050031 Full bool
Jon Perritt04851d32014-10-14 02:07:13 -050032 Limit int `q:"limit"`
33 Marker string `q:"marker"`
34 EndMarker string `q:"end_marker"`
35 Format string `q:"format"`
36 Prefix string `q:"prefix"`
37 Delimiter string `q:"delimiter"`
38 Path string `q:"path"`
Ash Wilsonca6f7562014-09-16 15:43:54 -040039}
40
Jon Perritte90aced2014-10-12 23:24:06 -050041// ToObjectListParams formats a ListOpts into a query string and boolean
42// representing whether to list complete information for each object.
43func (opts ListOpts) ToObjectListParams() (bool, string, error) {
44 q, err := gophercloud.BuildQueryString(opts)
45 if err != nil {
46 return false, "", err
47 }
48 return opts.Full, q.String(), nil
49}
50
Jon Perritt816d2a02014-03-11 20:49:46 -050051// List is a function that retrieves all objects in a container. It also returns the details
52// for the container. To extract only the object information or names, pass the ListResult
Jon Perritteb575642014-04-24 15:16:31 -050053// response to the ExtractInfo or ExtractNames function, respectively.
Jon Perritte90aced2014-10-12 23:24:06 -050054func List(c *gophercloud.ServiceClient, containerName string, opts ListOptsBuilder) pagination.Pager {
55 headers := map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
Jon Perritt816d2a02014-03-11 20:49:46 -050056
Jon Perrittea4e3012014-10-09 22:03:19 -050057 url := listURL(c, containerName)
Jon Perrittde47eac2014-09-30 15:34:17 -050058 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050059 full, query, err := opts.ToObjectListParams()
Jon Perrittde47eac2014-09-30 15:34:17 -050060 if err != nil {
Jon Perrittde47eac2014-09-30 15:34:17 -050061 return pagination.Pager{Err: err}
62 }
Jon Perritte90aced2014-10-12 23:24:06 -050063 url += query
Jon Perritt816d2a02014-03-11 20:49:46 -050064
Jon Perritte90aced2014-10-12 23:24:06 -050065 if full {
66 headers = map[string]string{"Accept": "application/json", "Content-Type": "application/json"}
Jon Perrittde47eac2014-09-30 15:34:17 -050067 }
Ash Wilsonca6f7562014-09-16 15:43:54 -040068 }
69
Ash Wilsonb8b16f82014-10-20 10:19:49 -040070 createPage := func(r pagination.PageResult) pagination.Page {
71 p := ObjectPage{pagination.MarkerPageBase{PageResult: r}}
Ash Wilsonca6f7562014-09-16 15:43:54 -040072 p.MarkerPageBase.Owner = p
73 return p
Jon Perritt816d2a02014-03-11 20:49:46 -050074 }
75
Ash Wilsonca6f7562014-09-16 15:43:54 -040076 pager := pagination.NewPager(c, url, createPage)
77 pager.Headers = headers
78 return pager
Jon Perritt816d2a02014-03-11 20:49:46 -050079}
80
Jon Perritte90aced2014-10-12 23:24:06 -050081// DownloadOptsBuilder allows extensions to add additional parameters to the
82// Download request.
83type DownloadOptsBuilder interface {
84 ToObjectDownloadParams() (map[string]string, string, error)
85}
86
Jon Perritt8c93a302014-09-28 22:35:57 -050087// DownloadOpts is a structure that holds parameters for downloading an object.
88type DownloadOpts struct {
89 IfMatch string `h:"If-Match"`
90 IfModifiedSince time.Time `h:"If-Modified-Since"`
91 IfNoneMatch string `h:"If-None-Match"`
92 IfUnmodifiedSince time.Time `h:"If-Unmodified-Since"`
93 Range string `h:"Range"`
94 Expires string `q:"expires"`
95 MultipartManifest string `q:"multipart-manifest"`
96 Signature string `q:"signature"`
97}
98
Jon Perritte90aced2014-10-12 23:24:06 -050099// ToObjectDownloadParams formats a DownloadOpts into a query string and map of
100// headers.
Paul Querna7dc6fe62014-11-01 08:09:41 -0700101func (opts DownloadOpts) ToObjectDownloadParams() (map[string]string, string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500102 q, err := gophercloud.BuildQueryString(opts)
103 if err != nil {
104 return nil, "", err
105 }
106 h, err := gophercloud.BuildHeaders(opts)
107 if err != nil {
108 return nil, q.String(), err
109 }
110 return h, q.String(), nil
111}
112
Jon Perritt816d2a02014-03-11 20:49:46 -0500113// Download is a function that retrieves the content and metadata for an object.
Jon Perritte90aced2014-10-12 23:24:06 -0500114// To extract just the content, pass the DownloadResult response to the
115// ExtractContent function.
116func Download(c *gophercloud.ServiceClient, containerName, objectName string, opts DownloadOptsBuilder) DownloadResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500117 var res DownloadResult
Jon Perritt8c93a302014-09-28 22:35:57 -0500118
Jon Perrittea4e3012014-10-09 22:03:19 -0500119 url := downloadURL(c, containerName, objectName)
Ash Wilson77857dc2014-10-22 09:09:02 -0400120 h := c.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500121
Jon Perrittde47eac2014-09-30 15:34:17 -0500122 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -0500123 headers, query, err := opts.ToObjectDownloadParams()
Jon Perrittde47eac2014-09-30 15:34:17 -0500124 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500125 res.Err = err
126 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500127 }
128
129 for k, v := range headers {
130 h[k] = v
131 }
132
Jon Perritte90aced2014-10-12 23:24:06 -0500133 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500134 }
135
Ash Wilson4bf41a32015-02-12 15:52:44 -0500136 resp, err := c.Request("GET", url, gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500137 MoreHeaders: h,
Paul Querna9163df22014-11-01 09:38:51 -0700138 OkCodes: []int{200, 304},
Jon Perritt816d2a02014-03-11 20:49:46 -0500139 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600140 if resp != nil {
141 res.Header = resp.Header
142 res.Body = resp.Body
143 }
Jon Perritt5db08922014-09-30 21:32:48 -0500144 res.Err = err
Jamie Hannaford2e784862014-10-27 10:40:27 +0100145
Jon Perritt5db08922014-09-30 21:32:48 -0500146 return res
Jon Perritt8c93a302014-09-28 22:35:57 -0500147}
148
Jon Perritte90aced2014-10-12 23:24:06 -0500149// CreateOptsBuilder allows extensions to add additional parameters to the
150// Create request.
151type CreateOptsBuilder interface {
152 ToObjectCreateParams() (map[string]string, string, error)
153}
154
Jon Perritt8c93a302014-09-28 22:35:57 -0500155// CreateOpts is a structure that holds parameters for creating an object.
156type CreateOpts struct {
157 Metadata map[string]string
Rodrigo Lourencod1c7d252016-05-23 14:11:03 -0300158 CacheControl string `h:"Cache-Control"`
Jon Perritt8c93a302014-09-28 22:35:57 -0500159 ContentDisposition string `h:"Content-Disposition"`
160 ContentEncoding string `h:"Content-Encoding"`
Jon Perritte376fa52014-11-03 11:35:48 -0600161 ContentLength int64 `h:"Content-Length"`
Jon Perritt8c93a302014-09-28 22:35:57 -0500162 ContentType string `h:"Content-Type"`
163 CopyFrom string `h:"X-Copy-From"`
164 DeleteAfter int `h:"X-Delete-After"`
165 DeleteAt int `h:"X-Delete-At"`
166 DetectContentType string `h:"X-Detect-Content-Type"`
167 ETag string `h:"ETag"`
168 IfNoneMatch string `h:"If-None-Match"`
169 ObjectManifest string `h:"X-Object-Manifest"`
170 TransferEncoding string `h:"Transfer-Encoding"`
171 Expires string `q:"expires"`
jrperritt4fcd3b72015-09-23 11:17:23 -0600172 MultipartManifest string `q:"multipart-manifest"`
Jon Perritt8c93a302014-09-28 22:35:57 -0500173 Signature string `q:"signature"`
Jon Perritt816d2a02014-03-11 20:49:46 -0500174}
175
Jon Perritte90aced2014-10-12 23:24:06 -0500176// ToObjectCreateParams formats a CreateOpts into a query string and map of
177// headers.
178func (opts CreateOpts) ToObjectCreateParams() (map[string]string, string, error) {
179 q, err := gophercloud.BuildQueryString(opts)
180 if err != nil {
181 return nil, "", err
182 }
183 h, err := gophercloud.BuildHeaders(opts)
184 if err != nil {
185 return nil, q.String(), err
186 }
187
188 for k, v := range opts.Metadata {
189 h["X-Object-Meta-"+k] = v
190 }
191
192 return h, q.String(), nil
193}
194
Jamie Hannaford50fc97d2015-07-16 12:29:01 +0200195// Create is a function that creates a new object or replaces an existing object. If the returned response's ETag
196// header fails to match the local checksum, the failed request will automatically be retried up to a maximum of 3 times.
Brendan ODonnella69b3472015-04-27 13:59:41 -0500197func Create(c *gophercloud.ServiceClient, containerName, objectName string, content io.ReadSeeker, opts CreateOptsBuilder) CreateResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500198 var res CreateResult
Jon Perritt816d2a02014-03-11 20:49:46 -0500199
Jon Perrittea4e3012014-10-09 22:03:19 -0500200 url := createURL(c, containerName, objectName)
Ash Wilson322a7e62015-02-12 16:25:26 -0500201 h := make(map[string]string)
Jon Perritt816d2a02014-03-11 20:49:46 -0500202
Jon Perrittde47eac2014-09-30 15:34:17 -0500203 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -0500204 headers, query, err := opts.ToObjectCreateParams()
Jon Perrittde47eac2014-09-30 15:34:17 -0500205 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500206 res.Err = err
207 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500208 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500209
Jon Perrittde47eac2014-09-30 15:34:17 -0500210 for k, v := range headers {
211 h[k] = v
212 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500213
Jon Perritte90aced2014-10-12 23:24:06 -0500214 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500215 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500216
Jamie Hannaford08096232015-07-13 12:47:28 +0200217 hash := md5.New()
jrperritt05e31e62015-09-22 21:00:33 -0600218 bufioReader := bufio.NewReader(io.TeeReader(content, hash))
219 io.Copy(ioutil.Discard, bufioReader)
220 localChecksum := hash.Sum(nil)
Jamie Hannaford08096232015-07-13 12:47:28 +0200221
jrperritt05e31e62015-09-22 21:00:33 -0600222 h["ETag"] = fmt.Sprintf("%x", localChecksum)
223
224 _, err := content.Seek(0, 0)
jrperritte6e8c652015-07-30 12:17:01 -0600225 if err != nil {
226 res.Err = err
227 return res
228 }
229
jrperritt56d51e92015-07-30 11:50:53 -0600230 ropts := gophercloud.RequestOpts{
jrperritt05e31e62015-09-22 21:00:33 -0600231 RawBody: content,
jrperritt56d51e92015-07-30 11:50:53 -0600232 MoreHeaders: h,
233 }
234
jrperritt433cc792015-07-31 18:53:12 -0600235 resp, err := c.Request("PUT", url, ropts)
236 if err != nil {
237 res.Err = err
238 return res
239 }
240 if resp != nil {
241 res.Header = resp.Header
242 if resp.Header.Get("ETag") == fmt.Sprintf("%x", localChecksum) {
243 res.Err = err
Jamie Hannaford08096232015-07-13 12:47:28 +0200244 return res
245 }
jrperritt9ad92432015-07-31 19:10:30 -0600246 res.Err = fmt.Errorf("Local checksum does not match API ETag header")
Jamie Hannaford08096232015-07-13 12:47:28 +0200247 }
248
Jon Perritt5db08922014-09-30 21:32:48 -0500249 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500250}
251
Jon Perritte90aced2014-10-12 23:24:06 -0500252// CopyOptsBuilder allows extensions to add additional parameters to the
253// Copy request.
254type CopyOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500255 ToObjectCopyMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500256}
257
258// CopyOpts is a structure that holds parameters for copying one object to
259// another.
Jon Perritt8c93a302014-09-28 22:35:57 -0500260type CopyOpts struct {
261 Metadata map[string]string
262 ContentDisposition string `h:"Content-Disposition"`
263 ContentEncoding string `h:"Content-Encoding"`
264 ContentType string `h:"Content-Type"`
265 Destination string `h:"Destination,required"`
266}
267
Jon Perritt04851d32014-10-14 02:07:13 -0500268// ToObjectCopyMap formats a CopyOpts into a map of headers.
269func (opts CopyOpts) ToObjectCopyMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500270 if opts.Destination == "" {
271 return nil, fmt.Errorf("Required CopyOpts field 'Destination' not set.")
272 }
273 h, err := gophercloud.BuildHeaders(opts)
274 if err != nil {
275 return nil, err
276 }
277 for k, v := range opts.Metadata {
278 h["X-Object-Meta-"+k] = v
279 }
280 return h, nil
281}
282
Jon Perritt816d2a02014-03-11 20:49:46 -0500283// Copy is a function that copies one object to another.
Jon Perritte90aced2014-10-12 23:24:06 -0500284func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts CopyOptsBuilder) CopyResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500285 var res CopyResult
Ash Wilson77857dc2014-10-22 09:09:02 -0400286 h := c.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500287
Jon Perritt04851d32014-10-14 02:07:13 -0500288 headers, err := opts.ToObjectCopyMap()
Jon Perritt8c93a302014-09-28 22:35:57 -0500289 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500290 res.Err = err
291 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500292 }
Jon Perritte90aced2014-10-12 23:24:06 -0500293
Jon Perritt8c93a302014-09-28 22:35:57 -0500294 for k, v := range headers {
Jon Perritt816d2a02014-03-11 20:49:46 -0500295 h[k] = v
296 }
297
Jon Perrittea4e3012014-10-09 22:03:19 -0500298 url := copyURL(c, containerName, objectName)
Ash Wilson4bf41a32015-02-12 15:52:44 -0500299 resp, err := c.Request("COPY", url, gophercloud.RequestOpts{
Jon Perritt8c93a302014-09-28 22:35:57 -0500300 MoreHeaders: h,
301 OkCodes: []int{201},
302 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600303 if resp != nil {
304 res.Header = resp.Header
305 }
jrperrittf7a8e282014-10-28 10:00:48 -0500306 res.Err = err
Jon Perritt5db08922014-09-30 21:32:48 -0500307 return res
Jon Perritt8c93a302014-09-28 22:35:57 -0500308}
309
Jon Perritte90aced2014-10-12 23:24:06 -0500310// DeleteOptsBuilder allows extensions to add additional parameters to the
311// Delete request.
312type DeleteOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500313 ToObjectDeleteQuery() (string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500314}
315
Jon Perritt8c93a302014-09-28 22:35:57 -0500316// DeleteOpts is a structure that holds parameters for deleting an object.
317type DeleteOpts struct {
318 MultipartManifest string `q:"multipart-manifest"`
319}
320
Jon Perritt26780d52014-10-14 11:35:58 -0500321// ToObjectDeleteQuery formats a DeleteOpts into a query string.
322func (opts DeleteOpts) ToObjectDeleteQuery() (string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500323 q, err := gophercloud.BuildQueryString(opts)
324 if err != nil {
325 return "", err
326 }
327 return q.String(), nil
328}
329
Jon Perritt8c93a302014-09-28 22:35:57 -0500330// Delete is a function that deletes an object.
Jon Perritte90aced2014-10-12 23:24:06 -0500331func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts DeleteOptsBuilder) DeleteResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500332 var res DeleteResult
Jon Perrittea4e3012014-10-09 22:03:19 -0500333 url := deleteURL(c, containerName, objectName)
Jon Perritt8c93a302014-09-28 22:35:57 -0500334
Jon Perrittde47eac2014-09-30 15:34:17 -0500335 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500336 query, err := opts.ToObjectDeleteQuery()
Jon Perrittde47eac2014-09-30 15:34:17 -0500337 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500338 res.Err = err
339 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500340 }
Jon Perritte90aced2014-10-12 23:24:06 -0500341 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500342 }
343
Jamie Hannaford1d27afa2015-03-24 16:20:45 +0100344 resp, err := c.Delete(url, nil)
Jon Perritta2c88b22015-05-18 11:23:30 -0600345 if resp != nil {
346 res.Header = resp.Header
347 }
Jon Perritt10a7ec12014-10-27 11:29:33 -0500348 res.Err = err
Jon Perritt5db08922014-09-30 21:32:48 -0500349 return res
Jon Perritt8c93a302014-09-28 22:35:57 -0500350}
351
Jon Perritte90aced2014-10-12 23:24:06 -0500352// GetOptsBuilder allows extensions to add additional parameters to the
353// Get request.
354type GetOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500355 ToObjectGetQuery() (string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500356}
357
Jon Perritt8c93a302014-09-28 22:35:57 -0500358// GetOpts is a structure that holds parameters for getting an object's metadata.
359type GetOpts struct {
360 Expires string `q:"expires"`
361 Signature string `q:"signature"`
362}
363
Jon Perritt26780d52014-10-14 11:35:58 -0500364// ToObjectGetQuery formats a GetOpts into a query string.
365func (opts GetOpts) ToObjectGetQuery() (string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500366 q, err := gophercloud.BuildQueryString(opts)
367 if err != nil {
368 return "", err
369 }
370 return q.String(), nil
371}
372
Jon Perritt8c93a302014-09-28 22:35:57 -0500373// Get is a function that retrieves the metadata of an object. To extract just the custom
374// metadata, pass the GetResult response to the ExtractMetadata function.
Jon Perritte90aced2014-10-12 23:24:06 -0500375func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts GetOptsBuilder) GetResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500376 var res GetResult
Jon Perrittea4e3012014-10-09 22:03:19 -0500377 url := getURL(c, containerName, objectName)
Jon Perrittde47eac2014-09-30 15:34:17 -0500378
379 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500380 query, err := opts.ToObjectGetQuery()
Jon Perrittde47eac2014-09-30 15:34:17 -0500381 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500382 res.Err = err
383 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500384 }
Jon Perritte90aced2014-10-12 23:24:06 -0500385 url += query
Jon Perritt8c93a302014-09-28 22:35:57 -0500386 }
387
Ash Wilson4bf41a32015-02-12 15:52:44 -0500388 resp, err := c.Request("HEAD", url, gophercloud.RequestOpts{
389 OkCodes: []int{200, 204},
Jon Perritt8c93a302014-09-28 22:35:57 -0500390 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600391 if resp != nil {
392 res.Header = resp.Header
393 }
Jon Perritt5db08922014-09-30 21:32:48 -0500394 res.Err = err
Jon Perritt5db08922014-09-30 21:32:48 -0500395 return res
Jon Perritt8c93a302014-09-28 22:35:57 -0500396}
397
Jon Perritte90aced2014-10-12 23:24:06 -0500398// UpdateOptsBuilder allows extensions to add additional parameters to the
399// Update request.
400type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500401 ToObjectUpdateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500402}
403
Jon Perritt8c93a302014-09-28 22:35:57 -0500404// UpdateOpts is a structure that holds parameters for updating, creating, or deleting an
405// object's metadata.
406type UpdateOpts struct {
407 Metadata map[string]string
408 ContentDisposition string `h:"Content-Disposition"`
409 ContentEncoding string `h:"Content-Encoding"`
410 ContentType string `h:"Content-Type"`
411 DeleteAfter int `h:"X-Delete-After"`
412 DeleteAt int `h:"X-Delete-At"`
413 DetectContentType bool `h:"X-Detect-Content-Type"`
414}
415
Jon Perritt04851d32014-10-14 02:07:13 -0500416// ToObjectUpdateMap formats a UpdateOpts into a map of headers.
417func (opts UpdateOpts) ToObjectUpdateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500418 h, err := gophercloud.BuildHeaders(opts)
419 if err != nil {
420 return nil, err
421 }
422 for k, v := range opts.Metadata {
423 h["X-Object-Meta-"+k] = v
424 }
425 return h, nil
426}
427
Jon Perritt8c93a302014-09-28 22:35:57 -0500428// Update is a function that creates, updates, or deletes an object's metadata.
Jon Perritte90aced2014-10-12 23:24:06 -0500429func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts UpdateOptsBuilder) UpdateResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500430 var res UpdateResult
Ash Wilson77857dc2014-10-22 09:09:02 -0400431 h := c.AuthenticatedHeaders()
Jon Perritt8c93a302014-09-28 22:35:57 -0500432
Jon Perrittde47eac2014-09-30 15:34:17 -0500433 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500434 headers, err := opts.ToObjectUpdateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500435 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500436 res.Err = err
437 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500438 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500439
Jon Perrittde47eac2014-09-30 15:34:17 -0500440 for k, v := range headers {
441 h[k] = v
442 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500443 }
444
Jon Perrittea4e3012014-10-09 22:03:19 -0500445 url := updateURL(c, containerName, objectName)
Ash Wilson4bf41a32015-02-12 15:52:44 -0500446 resp, err := c.Request("POST", url, gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500447 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500448 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600449 if resp != nil {
450 res.Header = resp.Header
451 }
Jon Perritt5db08922014-09-30 21:32:48 -0500452 res.Err = err
453 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500454}
Jon Perritt90957602015-02-01 17:03:06 -0700455
456// HTTPMethod represents an HTTP method string (e.g. "GET").
457type HTTPMethod string
458
459var (
460 // GET represents an HTTP "GET" method.
461 GET HTTPMethod = "GET"
462 // POST represents an HTTP "POST" method.
463 POST HTTPMethod = "POST"
464)
465
466// CreateTempURLOpts are options for creating a temporary URL for an object.
467type CreateTempURLOpts struct {
Jon Perritt28792af2015-02-02 11:00:04 -0700468 // (REQUIRED) Method is the HTTP method to allow for users of the temp URL. Valid values
Jon Perritt90957602015-02-01 17:03:06 -0700469 // are "GET" and "POST".
470 Method HTTPMethod
Jon Perritt28792af2015-02-02 11:00:04 -0700471 // (REQUIRED) TTL is the number of seconds the temp URL should be active.
Jon Perritt90957602015-02-01 17:03:06 -0700472 TTL int
Jon Perritt28792af2015-02-02 11:00:04 -0700473 // (Optional) Split is the string on which to split the object URL. Since only
474 // the object path is used in the hash, the object URL needs to be parsed. If
475 // empty, the default OpenStack URL split point will be used ("/v1/").
476 Split string
Jon Perritt90957602015-02-01 17:03:06 -0700477}
478
479// CreateTempURL is a function for creating a temporary URL for an object. It
480// allows users to have "GET" or "POST" access to a particular tenant's object
481// for a limited amount of time.
482func CreateTempURL(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateTempURLOpts) (string, error) {
Jon Perritt28792af2015-02-02 11:00:04 -0700483 if opts.Split == "" {
484 opts.Split = "/v1/"
485 }
Jon Perritt90957602015-02-01 17:03:06 -0700486 duration := time.Duration(opts.TTL) * time.Second
487 expiry := time.Now().Add(duration).Unix()
488 getHeader, err := accounts.Get(c, nil).Extract()
489 if err != nil {
490 return "", err
491 }
492 secretKey := []byte(getHeader.TempURLKey)
493 url := getURL(c, containerName, objectName)
Jon Perritt28792af2015-02-02 11:00:04 -0700494 splitPath := strings.Split(url, opts.Split)
Jon Perritt90957602015-02-01 17:03:06 -0700495 baseURL, objectPath := splitPath[0], splitPath[1]
Jon Perritt28792af2015-02-02 11:00:04 -0700496 objectPath = opts.Split + objectPath
Jon Perritt90957602015-02-01 17:03:06 -0700497 body := fmt.Sprintf("%s\n%d\n%s", opts.Method, expiry, objectPath)
498 hash := hmac.New(sha1.New, secretKey)
499 hash.Write([]byte(body))
500 hexsum := fmt.Sprintf("%x", hash.Sum(nil))
501 return fmt.Sprintf("%s%s?temp_url_sig=%s&temp_url_expires=%d", baseURL, objectPath, hexsum, expiry), nil
502}