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 |
| 158 | ContentDisposition string `h:"Content-Disposition"` |
| 159 | ContentEncoding string `h:"Content-Encoding"` |
Jon Perritt | e376fa5 | 2014-11-03 11:35:48 -0600 | [diff] [blame] | 160 | ContentLength int64 `h:"Content-Length"` |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 161 | ContentType string `h:"Content-Type"` |
| 162 | CopyFrom string `h:"X-Copy-From"` |
| 163 | DeleteAfter int `h:"X-Delete-After"` |
| 164 | DeleteAt int `h:"X-Delete-At"` |
| 165 | DetectContentType string `h:"X-Detect-Content-Type"` |
| 166 | ETag string `h:"ETag"` |
| 167 | IfNoneMatch string `h:"If-None-Match"` |
| 168 | ObjectManifest string `h:"X-Object-Manifest"` |
| 169 | TransferEncoding string `h:"Transfer-Encoding"` |
| 170 | Expires string `q:"expires"` |
jrperritt | 4fcd3b7 | 2015-09-23 11:17:23 -0600 | [diff] [blame] | 171 | MultipartManifest string `q:"multipart-manifest"` |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 172 | Signature string `q:"signature"` |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 173 | } |
| 174 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 175 | // ToObjectCreateParams formats a CreateOpts into a query string and map of |
| 176 | // headers. |
| 177 | func (opts CreateOpts) ToObjectCreateParams() (map[string]string, string, error) { |
| 178 | q, err := gophercloud.BuildQueryString(opts) |
| 179 | if err != nil { |
| 180 | return nil, "", err |
| 181 | } |
| 182 | h, err := gophercloud.BuildHeaders(opts) |
| 183 | if err != nil { |
| 184 | return nil, q.String(), err |
| 185 | } |
| 186 | |
| 187 | for k, v := range opts.Metadata { |
| 188 | h["X-Object-Meta-"+k] = v |
| 189 | } |
| 190 | |
| 191 | return h, q.String(), nil |
| 192 | } |
| 193 | |
Jamie Hannaford | 50fc97d | 2015-07-16 12:29:01 +0200 | [diff] [blame] | 194 | // Create is a function that creates a new object or replaces an existing object. If the returned response's ETag |
| 195 | // 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] | 196 | 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] | 197 | var res CreateResult |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 198 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 199 | url := createURL(c, containerName, objectName) |
Ash Wilson | 322a7e6 | 2015-02-12 16:25:26 -0500 | [diff] [blame] | 200 | h := make(map[string]string) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 201 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 202 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 203 | headers, query, err := opts.ToObjectCreateParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 204 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 205 | res.Err = err |
| 206 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 207 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 208 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 209 | for k, v := range headers { |
| 210 | h[k] = v |
| 211 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 212 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 213 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 214 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 215 | |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 216 | hash := md5.New() |
jrperritt | 05e31e6 | 2015-09-22 21:00:33 -0600 | [diff] [blame] | 217 | bufioReader := bufio.NewReader(io.TeeReader(content, hash)) |
| 218 | io.Copy(ioutil.Discard, bufioReader) |
| 219 | localChecksum := hash.Sum(nil) |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 220 | |
jrperritt | 05e31e6 | 2015-09-22 21:00:33 -0600 | [diff] [blame] | 221 | h["ETag"] = fmt.Sprintf("%x", localChecksum) |
| 222 | |
| 223 | _, err := content.Seek(0, 0) |
jrperritt | e6e8c65 | 2015-07-30 12:17:01 -0600 | [diff] [blame] | 224 | if err != nil { |
| 225 | res.Err = err |
| 226 | return res |
| 227 | } |
| 228 | |
jrperritt | 56d51e9 | 2015-07-30 11:50:53 -0600 | [diff] [blame] | 229 | ropts := gophercloud.RequestOpts{ |
jrperritt | 05e31e6 | 2015-09-22 21:00:33 -0600 | [diff] [blame] | 230 | RawBody: content, |
jrperritt | 56d51e9 | 2015-07-30 11:50:53 -0600 | [diff] [blame] | 231 | MoreHeaders: h, |
| 232 | } |
| 233 | |
jrperritt | 433cc79 | 2015-07-31 18:53:12 -0600 | [diff] [blame] | 234 | resp, err := c.Request("PUT", url, ropts) |
| 235 | if err != nil { |
| 236 | res.Err = err |
| 237 | return res |
| 238 | } |
| 239 | if resp != nil { |
| 240 | res.Header = resp.Header |
| 241 | if resp.Header.Get("ETag") == fmt.Sprintf("%x", localChecksum) { |
| 242 | res.Err = err |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 243 | return res |
| 244 | } |
jrperritt | 9ad9243 | 2015-07-31 19:10:30 -0600 | [diff] [blame] | 245 | res.Err = fmt.Errorf("Local checksum does not match API ETag header") |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 248 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 251 | // CopyOptsBuilder allows extensions to add additional parameters to the |
| 252 | // Copy request. |
| 253 | type CopyOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 254 | ToObjectCopyMap() (map[string]string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // CopyOpts is a structure that holds parameters for copying one object to |
| 258 | // another. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 259 | type CopyOpts struct { |
| 260 | Metadata map[string]string |
| 261 | ContentDisposition string `h:"Content-Disposition"` |
| 262 | ContentEncoding string `h:"Content-Encoding"` |
| 263 | ContentType string `h:"Content-Type"` |
| 264 | Destination string `h:"Destination,required"` |
| 265 | } |
| 266 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 267 | // ToObjectCopyMap formats a CopyOpts into a map of headers. |
| 268 | func (opts CopyOpts) ToObjectCopyMap() (map[string]string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 269 | if opts.Destination == "" { |
| 270 | return nil, fmt.Errorf("Required CopyOpts field 'Destination' not set.") |
| 271 | } |
| 272 | h, err := gophercloud.BuildHeaders(opts) |
| 273 | if err != nil { |
| 274 | return nil, err |
| 275 | } |
| 276 | for k, v := range opts.Metadata { |
| 277 | h["X-Object-Meta-"+k] = v |
| 278 | } |
| 279 | return h, nil |
| 280 | } |
| 281 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 282 | // Copy is a function that copies one object to another. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 283 | func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts CopyOptsBuilder) CopyResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 284 | var res CopyResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 285 | h := c.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 286 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 287 | headers, err := opts.ToObjectCopyMap() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 288 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 289 | res.Err = err |
| 290 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 291 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 292 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 293 | for k, v := range headers { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 294 | h[k] = v |
| 295 | } |
| 296 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 297 | url := copyURL(c, containerName, objectName) |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 298 | resp, err := c.Request("COPY", url, gophercloud.RequestOpts{ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 299 | MoreHeaders: h, |
| 300 | OkCodes: []int{201}, |
| 301 | }) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 302 | if resp != nil { |
| 303 | res.Header = resp.Header |
| 304 | } |
jrperritt | f7a8e28 | 2014-10-28 10:00:48 -0500 | [diff] [blame] | 305 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 306 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 307 | } |
| 308 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 309 | // DeleteOptsBuilder allows extensions to add additional parameters to the |
| 310 | // Delete request. |
| 311 | type DeleteOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 312 | ToObjectDeleteQuery() (string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 313 | } |
| 314 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 315 | // DeleteOpts is a structure that holds parameters for deleting an object. |
| 316 | type DeleteOpts struct { |
| 317 | MultipartManifest string `q:"multipart-manifest"` |
| 318 | } |
| 319 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 320 | // ToObjectDeleteQuery formats a DeleteOpts into a query string. |
| 321 | func (opts DeleteOpts) ToObjectDeleteQuery() (string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 322 | q, err := gophercloud.BuildQueryString(opts) |
| 323 | if err != nil { |
| 324 | return "", err |
| 325 | } |
| 326 | return q.String(), nil |
| 327 | } |
| 328 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 329 | // Delete is a function that deletes an object. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 330 | func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts DeleteOptsBuilder) DeleteResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 331 | var res DeleteResult |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 332 | url := deleteURL(c, containerName, objectName) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 333 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 334 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 335 | query, err := opts.ToObjectDeleteQuery() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 336 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 337 | res.Err = err |
| 338 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 339 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 340 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 341 | } |
| 342 | |
Jamie Hannaford | 1d27afa | 2015-03-24 16:20:45 +0100 | [diff] [blame] | 343 | resp, err := c.Delete(url, nil) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 344 | if resp != nil { |
| 345 | res.Header = resp.Header |
| 346 | } |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame] | 347 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 348 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 349 | } |
| 350 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 351 | // GetOptsBuilder allows extensions to add additional parameters to the |
| 352 | // Get request. |
| 353 | type GetOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 354 | ToObjectGetQuery() (string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 355 | } |
| 356 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 357 | // GetOpts is a structure that holds parameters for getting an object's metadata. |
| 358 | type GetOpts struct { |
| 359 | Expires string `q:"expires"` |
| 360 | Signature string `q:"signature"` |
| 361 | } |
| 362 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 363 | // ToObjectGetQuery formats a GetOpts into a query string. |
| 364 | func (opts GetOpts) ToObjectGetQuery() (string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 365 | q, err := gophercloud.BuildQueryString(opts) |
| 366 | if err != nil { |
| 367 | return "", err |
| 368 | } |
| 369 | return q.String(), nil |
| 370 | } |
| 371 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 372 | // Get is a function that retrieves the metadata of an object. To extract just the custom |
| 373 | // metadata, pass the GetResult response to the ExtractMetadata function. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 374 | func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts GetOptsBuilder) GetResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 375 | var res GetResult |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 376 | url := getURL(c, containerName, objectName) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 377 | |
| 378 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 379 | query, err := opts.ToObjectGetQuery() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 380 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 381 | res.Err = err |
| 382 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 383 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 384 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 385 | } |
| 386 | |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 387 | resp, err := c.Request("HEAD", url, gophercloud.RequestOpts{ |
| 388 | OkCodes: []int{200, 204}, |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 389 | }) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 390 | if resp != nil { |
| 391 | res.Header = resp.Header |
| 392 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 393 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 394 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 395 | } |
| 396 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 397 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 398 | // Update request. |
| 399 | type UpdateOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 400 | ToObjectUpdateMap() (map[string]string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 401 | } |
| 402 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 403 | // UpdateOpts is a structure that holds parameters for updating, creating, or deleting an |
| 404 | // object's metadata. |
| 405 | type UpdateOpts struct { |
| 406 | Metadata map[string]string |
| 407 | ContentDisposition string `h:"Content-Disposition"` |
| 408 | ContentEncoding string `h:"Content-Encoding"` |
| 409 | ContentType string `h:"Content-Type"` |
| 410 | DeleteAfter int `h:"X-Delete-After"` |
| 411 | DeleteAt int `h:"X-Delete-At"` |
| 412 | DetectContentType bool `h:"X-Detect-Content-Type"` |
| 413 | } |
| 414 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 415 | // ToObjectUpdateMap formats a UpdateOpts into a map of headers. |
| 416 | func (opts UpdateOpts) ToObjectUpdateMap() (map[string]string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 417 | h, err := gophercloud.BuildHeaders(opts) |
| 418 | if err != nil { |
| 419 | return nil, err |
| 420 | } |
| 421 | for k, v := range opts.Metadata { |
| 422 | h["X-Object-Meta-"+k] = v |
| 423 | } |
| 424 | return h, nil |
| 425 | } |
| 426 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 427 | // 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] | 428 | func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts UpdateOptsBuilder) UpdateResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 429 | var res UpdateResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 430 | h := c.AuthenticatedHeaders() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 431 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 432 | if opts != nil { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 433 | headers, err := opts.ToObjectUpdateMap() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 434 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 435 | res.Err = err |
| 436 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 437 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 438 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 439 | for k, v := range headers { |
| 440 | h[k] = v |
| 441 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 442 | } |
| 443 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 444 | url := updateURL(c, containerName, objectName) |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 445 | resp, err := c.Request("POST", url, gophercloud.RequestOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 446 | MoreHeaders: h, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 447 | }) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 448 | if resp != nil { |
| 449 | res.Header = resp.Header |
| 450 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 451 | res.Err = err |
| 452 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 453 | } |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 454 | |
| 455 | // HTTPMethod represents an HTTP method string (e.g. "GET"). |
| 456 | type HTTPMethod string |
| 457 | |
| 458 | var ( |
| 459 | // GET represents an HTTP "GET" method. |
| 460 | GET HTTPMethod = "GET" |
| 461 | // POST represents an HTTP "POST" method. |
| 462 | POST HTTPMethod = "POST" |
| 463 | ) |
| 464 | |
| 465 | // CreateTempURLOpts are options for creating a temporary URL for an object. |
| 466 | type CreateTempURLOpts struct { |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 467 | // (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] | 468 | // are "GET" and "POST". |
| 469 | Method HTTPMethod |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 470 | // (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] | 471 | TTL int |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 472 | // (Optional) Split is the string on which to split the object URL. Since only |
| 473 | // the object path is used in the hash, the object URL needs to be parsed. If |
| 474 | // empty, the default OpenStack URL split point will be used ("/v1/"). |
| 475 | Split string |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | // CreateTempURL is a function for creating a temporary URL for an object. It |
| 479 | // allows users to have "GET" or "POST" access to a particular tenant's object |
| 480 | // for a limited amount of time. |
| 481 | func CreateTempURL(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateTempURLOpts) (string, error) { |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 482 | if opts.Split == "" { |
| 483 | opts.Split = "/v1/" |
| 484 | } |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 485 | duration := time.Duration(opts.TTL) * time.Second |
| 486 | expiry := time.Now().Add(duration).Unix() |
| 487 | getHeader, err := accounts.Get(c, nil).Extract() |
| 488 | if err != nil { |
| 489 | return "", err |
| 490 | } |
| 491 | secretKey := []byte(getHeader.TempURLKey) |
| 492 | url := getURL(c, containerName, objectName) |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 493 | splitPath := strings.Split(url, opts.Split) |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 494 | baseURL, objectPath := splitPath[0], splitPath[1] |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 495 | objectPath = opts.Split + objectPath |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 496 | body := fmt.Sprintf("%s\n%d\n%s", opts.Method, expiry, objectPath) |
| 497 | hash := hmac.New(sha1.New, secretKey) |
| 498 | hash.Write([]byte(body)) |
| 499 | hexsum := fmt.Sprintf("%x", hash.Sum(nil)) |
| 500 | return fmt.Sprintf("%s%s?temp_url_sig=%s&temp_url_expires=%d", baseURL, objectPath, hexsum, expiry), nil |
| 501 | } |