Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
jrperritt | 05e31e6 | 2015-09-22 21:00:33 -0600 | [diff] [blame] | 4 | "bufio" |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 5 | "crypto/hmac" |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 6 | "crypto/md5" |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 7 | "crypto/sha1" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 8 | "fmt" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 9 | "io" |
jrperritt | 05e31e6 | 2015-09-22 21:00:33 -0600 | [diff] [blame] | 10 | "io/ioutil" |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 11 | "strings" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 12 | "time" |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 13 | |
jrperritt | d200ea3 | 2015-07-30 12:23:28 -0600 | [diff] [blame] | 14 | "github.com/rackspace/gophercloud" |
| 15 | "github.com/rackspace/gophercloud/openstack/objectstorage/v1/accounts" |
| 16 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 17 | ) |
| 18 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 19 | // ListOptsBuilder allows extensions to add additional parameters to the List |
| 20 | // request. |
| 21 | type ListOptsBuilder interface { |
| 22 | ToObjectListParams() (bool, string, error) |
| 23 | } |
| 24 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 25 | // ListOpts is a structure that holds parameters for listing objects. |
| 26 | type ListOpts struct { |
Jon Perritt | 9415ca7 | 2014-11-03 11:58:48 -0600 | [diff] [blame] | 27 | // 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 Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 31 | Full bool |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 32 | 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 Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 39 | } |
| 40 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 41 | // ToObjectListParams formats a ListOpts into a query string and boolean |
| 42 | // representing whether to list complete information for each object. |
| 43 | func (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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 51 | // 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 Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 53 | // response to the ExtractInfo or ExtractNames function, respectively. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 54 | func List(c *gophercloud.ServiceClient, containerName string, opts ListOptsBuilder) pagination.Pager { |
| 55 | headers := map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"} |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 56 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 57 | url := listURL(c, containerName) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 58 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 59 | full, query, err := opts.ToObjectListParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 60 | if err != nil { |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 61 | return pagination.Pager{Err: err} |
| 62 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 63 | url += query |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 64 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 65 | if full { |
| 66 | headers = map[string]string{"Accept": "application/json", "Content-Type": "application/json"} |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 67 | } |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 70 | createPage := func(r pagination.PageResult) pagination.Page { |
| 71 | p := ObjectPage{pagination.MarkerPageBase{PageResult: r}} |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 72 | p.MarkerPageBase.Owner = p |
| 73 | return p |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 76 | pager := pagination.NewPager(c, url, createPage) |
| 77 | pager.Headers = headers |
| 78 | return pager |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 81 | // DownloadOptsBuilder allows extensions to add additional parameters to the |
| 82 | // Download request. |
| 83 | type DownloadOptsBuilder interface { |
| 84 | ToObjectDownloadParams() (map[string]string, string, error) |
| 85 | } |
| 86 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 87 | // DownloadOpts is a structure that holds parameters for downloading an object. |
| 88 | type 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 Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 99 | // ToObjectDownloadParams formats a DownloadOpts into a query string and map of |
| 100 | // headers. |
Paul Querna | 7dc6fe6 | 2014-11-01 08:09:41 -0700 | [diff] [blame] | 101 | func (opts DownloadOpts) ToObjectDownloadParams() (map[string]string, string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 102 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 113 | // Download is a function that retrieves the content and metadata for an object. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 114 | // To extract just the content, pass the DownloadResult response to the |
| 115 | // ExtractContent function. |
| 116 | func Download(c *gophercloud.ServiceClient, containerName, objectName string, opts DownloadOptsBuilder) DownloadResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 117 | var res DownloadResult |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 118 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 119 | url := downloadURL(c, containerName, objectName) |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 120 | h := c.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 121 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 122 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 123 | headers, query, err := opts.ToObjectDownloadParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 124 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 125 | res.Err = err |
| 126 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | for k, v := range headers { |
| 130 | h[k] = v |
| 131 | } |
| 132 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 133 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 136 | resp, err := c.Request("GET", url, gophercloud.RequestOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 137 | MoreHeaders: h, |
Paul Querna | 9163df2 | 2014-11-01 09:38:51 -0700 | [diff] [blame] | 138 | OkCodes: []int{200, 304}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 139 | }) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 140 | if resp != nil { |
| 141 | res.Header = resp.Header |
| 142 | res.Body = resp.Body |
| 143 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 144 | res.Err = err |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 145 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 146 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 147 | } |
| 148 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 149 | // CreateOptsBuilder allows extensions to add additional parameters to the |
| 150 | // Create request. |
| 151 | type CreateOptsBuilder interface { |
| 152 | ToObjectCreateParams() (map[string]string, string, error) |
| 153 | } |
| 154 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 155 | // CreateOpts is a structure that holds parameters for creating an object. |
| 156 | type CreateOpts struct { |
| 157 | Metadata map[string]string |
Rodrigo Lourenco | d1c7d25 | 2016-05-23 14:11:03 -0300 | [diff] [blame^] | 158 | CacheControl string `h:"Cache-Control"` |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 159 | ContentDisposition string `h:"Content-Disposition"` |
| 160 | ContentEncoding string `h:"Content-Encoding"` |
Jon Perritt | e376fa5 | 2014-11-03 11:35:48 -0600 | [diff] [blame] | 161 | ContentLength int64 `h:"Content-Length"` |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 162 | 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"` |
jrperritt | 4fcd3b7 | 2015-09-23 11:17:23 -0600 | [diff] [blame] | 172 | MultipartManifest string `q:"multipart-manifest"` |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 173 | Signature string `q:"signature"` |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 174 | } |
| 175 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 176 | // ToObjectCreateParams formats a CreateOpts into a query string and map of |
| 177 | // headers. |
| 178 | func (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 Hannaford | 50fc97d | 2015-07-16 12:29:01 +0200 | [diff] [blame] | 195 | // 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 ODonnell | a69b347 | 2015-04-27 13:59:41 -0500 | [diff] [blame] | 197 | func Create(c *gophercloud.ServiceClient, containerName, objectName string, content io.ReadSeeker, opts CreateOptsBuilder) CreateResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 198 | var res CreateResult |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 199 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 200 | url := createURL(c, containerName, objectName) |
Ash Wilson | 322a7e6 | 2015-02-12 16:25:26 -0500 | [diff] [blame] | 201 | h := make(map[string]string) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 202 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 203 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 204 | headers, query, err := opts.ToObjectCreateParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 205 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 206 | res.Err = err |
| 207 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 208 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 209 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 210 | for k, v := range headers { |
| 211 | h[k] = v |
| 212 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 213 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 214 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 215 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 216 | |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 217 | hash := md5.New() |
jrperritt | 05e31e6 | 2015-09-22 21:00:33 -0600 | [diff] [blame] | 218 | bufioReader := bufio.NewReader(io.TeeReader(content, hash)) |
| 219 | io.Copy(ioutil.Discard, bufioReader) |
| 220 | localChecksum := hash.Sum(nil) |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 221 | |
jrperritt | 05e31e6 | 2015-09-22 21:00:33 -0600 | [diff] [blame] | 222 | h["ETag"] = fmt.Sprintf("%x", localChecksum) |
| 223 | |
| 224 | _, err := content.Seek(0, 0) |
jrperritt | e6e8c65 | 2015-07-30 12:17:01 -0600 | [diff] [blame] | 225 | if err != nil { |
| 226 | res.Err = err |
| 227 | return res |
| 228 | } |
| 229 | |
jrperritt | 56d51e9 | 2015-07-30 11:50:53 -0600 | [diff] [blame] | 230 | ropts := gophercloud.RequestOpts{ |
jrperritt | 05e31e6 | 2015-09-22 21:00:33 -0600 | [diff] [blame] | 231 | RawBody: content, |
jrperritt | 56d51e9 | 2015-07-30 11:50:53 -0600 | [diff] [blame] | 232 | MoreHeaders: h, |
| 233 | } |
| 234 | |
jrperritt | 433cc79 | 2015-07-31 18:53:12 -0600 | [diff] [blame] | 235 | 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 Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 244 | return res |
| 245 | } |
jrperritt | 9ad9243 | 2015-07-31 19:10:30 -0600 | [diff] [blame] | 246 | res.Err = fmt.Errorf("Local checksum does not match API ETag header") |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 249 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 250 | } |
| 251 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 252 | // CopyOptsBuilder allows extensions to add additional parameters to the |
| 253 | // Copy request. |
| 254 | type CopyOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 255 | ToObjectCopyMap() (map[string]string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // CopyOpts is a structure that holds parameters for copying one object to |
| 259 | // another. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 260 | type 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 Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 268 | // ToObjectCopyMap formats a CopyOpts into a map of headers. |
| 269 | func (opts CopyOpts) ToObjectCopyMap() (map[string]string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 270 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 283 | // Copy is a function that copies one object to another. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 284 | func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts CopyOptsBuilder) CopyResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 285 | var res CopyResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 286 | h := c.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 287 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 288 | headers, err := opts.ToObjectCopyMap() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 289 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 290 | res.Err = err |
| 291 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 292 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 293 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 294 | for k, v := range headers { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 295 | h[k] = v |
| 296 | } |
| 297 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 298 | url := copyURL(c, containerName, objectName) |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 299 | resp, err := c.Request("COPY", url, gophercloud.RequestOpts{ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 300 | MoreHeaders: h, |
| 301 | OkCodes: []int{201}, |
| 302 | }) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 303 | if resp != nil { |
| 304 | res.Header = resp.Header |
| 305 | } |
jrperritt | f7a8e28 | 2014-10-28 10:00:48 -0500 | [diff] [blame] | 306 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 307 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 308 | } |
| 309 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 310 | // DeleteOptsBuilder allows extensions to add additional parameters to the |
| 311 | // Delete request. |
| 312 | type DeleteOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 313 | ToObjectDeleteQuery() (string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 314 | } |
| 315 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 316 | // DeleteOpts is a structure that holds parameters for deleting an object. |
| 317 | type DeleteOpts struct { |
| 318 | MultipartManifest string `q:"multipart-manifest"` |
| 319 | } |
| 320 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 321 | // ToObjectDeleteQuery formats a DeleteOpts into a query string. |
| 322 | func (opts DeleteOpts) ToObjectDeleteQuery() (string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 323 | q, err := gophercloud.BuildQueryString(opts) |
| 324 | if err != nil { |
| 325 | return "", err |
| 326 | } |
| 327 | return q.String(), nil |
| 328 | } |
| 329 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 330 | // Delete is a function that deletes an object. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 331 | func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts DeleteOptsBuilder) DeleteResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 332 | var res DeleteResult |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 333 | url := deleteURL(c, containerName, objectName) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 334 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 335 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 336 | query, err := opts.ToObjectDeleteQuery() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 337 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 338 | res.Err = err |
| 339 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 340 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 341 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 342 | } |
| 343 | |
Jamie Hannaford | 1d27afa | 2015-03-24 16:20:45 +0100 | [diff] [blame] | 344 | resp, err := c.Delete(url, nil) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 345 | if resp != nil { |
| 346 | res.Header = resp.Header |
| 347 | } |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame] | 348 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 349 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 350 | } |
| 351 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 352 | // GetOptsBuilder allows extensions to add additional parameters to the |
| 353 | // Get request. |
| 354 | type GetOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 355 | ToObjectGetQuery() (string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 356 | } |
| 357 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 358 | // GetOpts is a structure that holds parameters for getting an object's metadata. |
| 359 | type GetOpts struct { |
| 360 | Expires string `q:"expires"` |
| 361 | Signature string `q:"signature"` |
| 362 | } |
| 363 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 364 | // ToObjectGetQuery formats a GetOpts into a query string. |
| 365 | func (opts GetOpts) ToObjectGetQuery() (string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 366 | q, err := gophercloud.BuildQueryString(opts) |
| 367 | if err != nil { |
| 368 | return "", err |
| 369 | } |
| 370 | return q.String(), nil |
| 371 | } |
| 372 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 373 | // 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 Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 375 | func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts GetOptsBuilder) GetResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 376 | var res GetResult |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 377 | url := getURL(c, containerName, objectName) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 378 | |
| 379 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 380 | query, err := opts.ToObjectGetQuery() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 381 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 382 | res.Err = err |
| 383 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 384 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 385 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 386 | } |
| 387 | |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 388 | resp, err := c.Request("HEAD", url, gophercloud.RequestOpts{ |
| 389 | OkCodes: []int{200, 204}, |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 390 | }) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 391 | if resp != nil { |
| 392 | res.Header = resp.Header |
| 393 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 394 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 395 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 398 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 399 | // Update request. |
| 400 | type UpdateOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 401 | ToObjectUpdateMap() (map[string]string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 402 | } |
| 403 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 404 | // UpdateOpts is a structure that holds parameters for updating, creating, or deleting an |
| 405 | // object's metadata. |
| 406 | type 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 Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 416 | // ToObjectUpdateMap formats a UpdateOpts into a map of headers. |
| 417 | func (opts UpdateOpts) ToObjectUpdateMap() (map[string]string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 418 | 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 Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 428 | // Update is a function that creates, updates, or deletes an object's metadata. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 429 | func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts UpdateOptsBuilder) UpdateResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 430 | var res UpdateResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 431 | h := c.AuthenticatedHeaders() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 432 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 433 | if opts != nil { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 434 | headers, err := opts.ToObjectUpdateMap() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 435 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 436 | res.Err = err |
| 437 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 438 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 439 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 440 | for k, v := range headers { |
| 441 | h[k] = v |
| 442 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 443 | } |
| 444 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 445 | url := updateURL(c, containerName, objectName) |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 446 | resp, err := c.Request("POST", url, gophercloud.RequestOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 447 | MoreHeaders: h, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 448 | }) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 449 | if resp != nil { |
| 450 | res.Header = resp.Header |
| 451 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 452 | res.Err = err |
| 453 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 454 | } |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 455 | |
| 456 | // HTTPMethod represents an HTTP method string (e.g. "GET"). |
| 457 | type HTTPMethod string |
| 458 | |
| 459 | var ( |
| 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. |
| 467 | type CreateTempURLOpts struct { |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 468 | // (REQUIRED) Method is the HTTP method to allow for users of the temp URL. Valid values |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 469 | // are "GET" and "POST". |
| 470 | Method HTTPMethod |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 471 | // (REQUIRED) TTL is the number of seconds the temp URL should be active. |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 472 | TTL int |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 473 | // (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 Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 477 | } |
| 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. |
| 482 | func CreateTempURL(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateTempURLOpts) (string, error) { |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 483 | if opts.Split == "" { |
| 484 | opts.Split = "/v1/" |
| 485 | } |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 486 | 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 Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 494 | splitPath := strings.Split(url, opts.Split) |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 495 | baseURL, objectPath := splitPath[0], splitPath[1] |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 496 | objectPath = opts.Split + objectPath |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 497 | 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 | } |