Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
jrperritt | 046c882 | 2015-07-30 12:02:28 -0600 | [diff] [blame] | 4 | "bytes" |
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" |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 10 | "strings" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 11 | "time" |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 12 | |
jrperritt | d200ea3 | 2015-07-30 12:23:28 -0600 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud" |
| 14 | "github.com/rackspace/gophercloud/openstack/objectstorage/v1/accounts" |
| 15 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 16 | ) |
| 17 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 18 | // ListOptsBuilder allows extensions to add additional parameters to the List |
| 19 | // request. |
| 20 | type ListOptsBuilder interface { |
| 21 | ToObjectListParams() (bool, string, error) |
| 22 | } |
| 23 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 24 | // ListOpts is a structure that holds parameters for listing objects. |
| 25 | type ListOpts struct { |
Jon Perritt | 9415ca7 | 2014-11-03 11:58:48 -0600 | [diff] [blame] | 26 | // 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 Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 30 | Full bool |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 31 | 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 Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 38 | } |
| 39 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 40 | // ToObjectListParams formats a ListOpts into a query string and boolean |
| 41 | // representing whether to list complete information for each object. |
| 42 | func (opts ListOpts) ToObjectListParams() (bool, string, error) { |
| 43 | q, err := gophercloud.BuildQueryString(opts) |
| 44 | if err != nil { |
| 45 | return false, "", err |
| 46 | } |
| 47 | return opts.Full, q.String(), nil |
| 48 | } |
| 49 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 50 | // List is a function that retrieves all objects in a container. It also returns the details |
| 51 | // 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] | 52 | // response to the ExtractInfo or ExtractNames function, respectively. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 53 | func List(c *gophercloud.ServiceClient, containerName string, opts ListOptsBuilder) pagination.Pager { |
| 54 | headers := map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"} |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 55 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 56 | url := listURL(c, containerName) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 57 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 58 | full, query, err := opts.ToObjectListParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 59 | if err != nil { |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 60 | return pagination.Pager{Err: err} |
| 61 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 62 | url += query |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 63 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 64 | if full { |
| 65 | headers = map[string]string{"Accept": "application/json", "Content-Type": "application/json"} |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 66 | } |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 69 | createPage := func(r pagination.PageResult) pagination.Page { |
| 70 | p := ObjectPage{pagination.MarkerPageBase{PageResult: r}} |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 71 | p.MarkerPageBase.Owner = p |
| 72 | return p |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 73 | } |
| 74 | |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 75 | pager := pagination.NewPager(c, url, createPage) |
| 76 | pager.Headers = headers |
| 77 | return pager |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 78 | } |
| 79 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 80 | // DownloadOptsBuilder allows extensions to add additional parameters to the |
| 81 | // Download request. |
| 82 | type DownloadOptsBuilder interface { |
| 83 | ToObjectDownloadParams() (map[string]string, string, error) |
| 84 | } |
| 85 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 86 | // DownloadOpts is a structure that holds parameters for downloading an object. |
| 87 | type DownloadOpts struct { |
| 88 | IfMatch string `h:"If-Match"` |
| 89 | IfModifiedSince time.Time `h:"If-Modified-Since"` |
| 90 | IfNoneMatch string `h:"If-None-Match"` |
| 91 | IfUnmodifiedSince time.Time `h:"If-Unmodified-Since"` |
| 92 | Range string `h:"Range"` |
| 93 | Expires string `q:"expires"` |
| 94 | MultipartManifest string `q:"multipart-manifest"` |
| 95 | Signature string `q:"signature"` |
| 96 | } |
| 97 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 98 | // ToObjectDownloadParams formats a DownloadOpts into a query string and map of |
| 99 | // headers. |
Paul Querna | 7dc6fe6 | 2014-11-01 08:09:41 -0700 | [diff] [blame] | 100 | func (opts DownloadOpts) ToObjectDownloadParams() (map[string]string, string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 101 | q, err := gophercloud.BuildQueryString(opts) |
| 102 | if err != nil { |
| 103 | return nil, "", err |
| 104 | } |
| 105 | h, err := gophercloud.BuildHeaders(opts) |
| 106 | if err != nil { |
| 107 | return nil, q.String(), err |
| 108 | } |
| 109 | return h, q.String(), nil |
| 110 | } |
| 111 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 112 | // 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] | 113 | // To extract just the content, pass the DownloadResult response to the |
| 114 | // ExtractContent function. |
| 115 | func Download(c *gophercloud.ServiceClient, containerName, objectName string, opts DownloadOptsBuilder) DownloadResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 116 | var res DownloadResult |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 117 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 118 | url := downloadURL(c, containerName, objectName) |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 119 | h := c.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 120 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 121 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 122 | headers, query, err := opts.ToObjectDownloadParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 123 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 124 | res.Err = err |
| 125 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | for k, v := range headers { |
| 129 | h[k] = v |
| 130 | } |
| 131 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 132 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 135 | resp, err := c.Request("GET", url, gophercloud.RequestOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 136 | MoreHeaders: h, |
Paul Querna | 9163df2 | 2014-11-01 09:38:51 -0700 | [diff] [blame] | 137 | OkCodes: []int{200, 304}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 138 | }) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 139 | if resp != nil { |
| 140 | res.Header = resp.Header |
| 141 | res.Body = resp.Body |
| 142 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 143 | res.Err = err |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 144 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 145 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 146 | } |
| 147 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 148 | // CreateOptsBuilder allows extensions to add additional parameters to the |
| 149 | // Create request. |
| 150 | type CreateOptsBuilder interface { |
| 151 | ToObjectCreateParams() (map[string]string, string, error) |
| 152 | } |
| 153 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 154 | // CreateOpts is a structure that holds parameters for creating an object. |
| 155 | type CreateOpts struct { |
| 156 | Metadata map[string]string |
| 157 | ContentDisposition string `h:"Content-Disposition"` |
| 158 | ContentEncoding string `h:"Content-Encoding"` |
Jon Perritt | e376fa5 | 2014-11-03 11:35:48 -0600 | [diff] [blame] | 159 | ContentLength int64 `h:"Content-Length"` |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 160 | ContentType string `h:"Content-Type"` |
| 161 | CopyFrom string `h:"X-Copy-From"` |
| 162 | DeleteAfter int `h:"X-Delete-After"` |
| 163 | DeleteAt int `h:"X-Delete-At"` |
| 164 | DetectContentType string `h:"X-Detect-Content-Type"` |
| 165 | ETag string `h:"ETag"` |
| 166 | IfNoneMatch string `h:"If-None-Match"` |
| 167 | ObjectManifest string `h:"X-Object-Manifest"` |
| 168 | TransferEncoding string `h:"Transfer-Encoding"` |
| 169 | Expires string `q:"expires"` |
| 170 | MultipartManifest string `q:"multiple-manifest"` |
| 171 | Signature string `q:"signature"` |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 174 | // ToObjectCreateParams formats a CreateOpts into a query string and map of |
| 175 | // headers. |
| 176 | func (opts CreateOpts) ToObjectCreateParams() (map[string]string, string, error) { |
| 177 | q, err := gophercloud.BuildQueryString(opts) |
| 178 | if err != nil { |
| 179 | return nil, "", err |
| 180 | } |
| 181 | h, err := gophercloud.BuildHeaders(opts) |
| 182 | if err != nil { |
| 183 | return nil, q.String(), err |
| 184 | } |
| 185 | |
| 186 | for k, v := range opts.Metadata { |
| 187 | h["X-Object-Meta-"+k] = v |
| 188 | } |
| 189 | |
| 190 | return h, q.String(), nil |
| 191 | } |
| 192 | |
Jamie Hannaford | 50fc97d | 2015-07-16 12:29:01 +0200 | [diff] [blame] | 193 | // Create is a function that creates a new object or replaces an existing object. If the returned response's ETag |
| 194 | // 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] | 195 | 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] | 196 | var res CreateResult |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 197 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 198 | url := createURL(c, containerName, objectName) |
Ash Wilson | 322a7e6 | 2015-02-12 16:25:26 -0500 | [diff] [blame] | 199 | h := make(map[string]string) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 200 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 201 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 202 | headers, query, err := opts.ToObjectCreateParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 203 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 204 | res.Err = err |
| 205 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 206 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 207 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 208 | for k, v := range headers { |
| 209 | h[k] = v |
| 210 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 211 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 212 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 213 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 214 | |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 215 | hash := md5.New() |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 216 | |
jrperritt | e6e8c65 | 2015-07-30 12:17:01 -0600 | [diff] [blame] | 217 | contentBuffer := bytes.NewBuffer([]byte{}) |
| 218 | _, err := io.Copy(contentBuffer, io.TeeReader(content, hash)) |
| 219 | if err != nil { |
| 220 | res.Err = err |
| 221 | return res |
| 222 | } |
| 223 | |
| 224 | localChecksum := hash.Sum(nil) |
jrperritt | 56d51e9 | 2015-07-30 11:50:53 -0600 | [diff] [blame] | 225 | h["ETag"] = fmt.Sprintf("%x", localChecksum) |
| 226 | |
| 227 | ropts := gophercloud.RequestOpts{ |
jrperritt | 046c882 | 2015-07-30 12:02:28 -0600 | [diff] [blame] | 228 | RawBody: strings.NewReader(contentBuffer.String()), |
jrperritt | 56d51e9 | 2015-07-30 11:50:53 -0600 | [diff] [blame] | 229 | MoreHeaders: h, |
| 230 | } |
| 231 | |
jrperritt | 433cc79 | 2015-07-31 18:53:12 -0600 | [diff] [blame] | 232 | resp, err := c.Request("PUT", url, ropts) |
| 233 | if err != nil { |
| 234 | res.Err = err |
| 235 | return res |
| 236 | } |
| 237 | if resp != nil { |
| 238 | res.Header = resp.Header |
| 239 | if resp.Header.Get("ETag") == fmt.Sprintf("%x", localChecksum) { |
| 240 | res.Err = err |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 241 | return res |
| 242 | } |
jrperritt | 9ad9243 | 2015-07-31 19:10:30 -0600 | [diff] [blame] | 243 | res.Err = fmt.Errorf("Local checksum does not match API ETag header") |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 246 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 247 | } |
| 248 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 249 | // CopyOptsBuilder allows extensions to add additional parameters to the |
| 250 | // Copy request. |
| 251 | type CopyOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 252 | ToObjectCopyMap() (map[string]string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // CopyOpts is a structure that holds parameters for copying one object to |
| 256 | // another. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 257 | type CopyOpts struct { |
| 258 | Metadata map[string]string |
| 259 | ContentDisposition string `h:"Content-Disposition"` |
| 260 | ContentEncoding string `h:"Content-Encoding"` |
| 261 | ContentType string `h:"Content-Type"` |
| 262 | Destination string `h:"Destination,required"` |
| 263 | } |
| 264 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 265 | // ToObjectCopyMap formats a CopyOpts into a map of headers. |
| 266 | func (opts CopyOpts) ToObjectCopyMap() (map[string]string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 267 | if opts.Destination == "" { |
| 268 | return nil, fmt.Errorf("Required CopyOpts field 'Destination' not set.") |
| 269 | } |
| 270 | h, err := gophercloud.BuildHeaders(opts) |
| 271 | if err != nil { |
| 272 | return nil, err |
| 273 | } |
| 274 | for k, v := range opts.Metadata { |
| 275 | h["X-Object-Meta-"+k] = v |
| 276 | } |
| 277 | return h, nil |
| 278 | } |
| 279 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 280 | // Copy is a function that copies one object to another. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 281 | func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts CopyOptsBuilder) CopyResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 282 | var res CopyResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 283 | h := c.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 284 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 285 | headers, err := opts.ToObjectCopyMap() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 286 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 287 | res.Err = err |
| 288 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 289 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 290 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 291 | for k, v := range headers { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 292 | h[k] = v |
| 293 | } |
| 294 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 295 | url := copyURL(c, containerName, objectName) |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 296 | resp, err := c.Request("COPY", url, gophercloud.RequestOpts{ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 297 | MoreHeaders: h, |
| 298 | OkCodes: []int{201}, |
| 299 | }) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 300 | if resp != nil { |
| 301 | res.Header = resp.Header |
| 302 | } |
jrperritt | f7a8e28 | 2014-10-28 10:00:48 -0500 | [diff] [blame] | 303 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 304 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 307 | // DeleteOptsBuilder allows extensions to add additional parameters to the |
| 308 | // Delete request. |
| 309 | type DeleteOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 310 | ToObjectDeleteQuery() (string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 313 | // DeleteOpts is a structure that holds parameters for deleting an object. |
| 314 | type DeleteOpts struct { |
| 315 | MultipartManifest string `q:"multipart-manifest"` |
| 316 | } |
| 317 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 318 | // ToObjectDeleteQuery formats a DeleteOpts into a query string. |
| 319 | func (opts DeleteOpts) ToObjectDeleteQuery() (string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 320 | q, err := gophercloud.BuildQueryString(opts) |
| 321 | if err != nil { |
| 322 | return "", err |
| 323 | } |
| 324 | return q.String(), nil |
| 325 | } |
| 326 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 327 | // Delete is a function that deletes an object. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 328 | func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts DeleteOptsBuilder) DeleteResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 329 | var res DeleteResult |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 330 | url := deleteURL(c, containerName, objectName) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 331 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 332 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 333 | query, err := opts.ToObjectDeleteQuery() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 334 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 335 | res.Err = err |
| 336 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 337 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 338 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 339 | } |
| 340 | |
Jamie Hannaford | 1d27afa | 2015-03-24 16:20:45 +0100 | [diff] [blame] | 341 | resp, err := c.Delete(url, nil) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 342 | if resp != nil { |
| 343 | res.Header = resp.Header |
| 344 | } |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame] | 345 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 346 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 347 | } |
| 348 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 349 | // GetOptsBuilder allows extensions to add additional parameters to the |
| 350 | // Get request. |
| 351 | type GetOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 352 | ToObjectGetQuery() (string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 353 | } |
| 354 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 355 | // GetOpts is a structure that holds parameters for getting an object's metadata. |
| 356 | type GetOpts struct { |
| 357 | Expires string `q:"expires"` |
| 358 | Signature string `q:"signature"` |
| 359 | } |
| 360 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 361 | // ToObjectGetQuery formats a GetOpts into a query string. |
| 362 | func (opts GetOpts) ToObjectGetQuery() (string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 363 | q, err := gophercloud.BuildQueryString(opts) |
| 364 | if err != nil { |
| 365 | return "", err |
| 366 | } |
| 367 | return q.String(), nil |
| 368 | } |
| 369 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 370 | // Get is a function that retrieves the metadata of an object. To extract just the custom |
| 371 | // metadata, pass the GetResult response to the ExtractMetadata function. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 372 | func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts GetOptsBuilder) GetResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 373 | var res GetResult |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 374 | url := getURL(c, containerName, objectName) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 375 | |
| 376 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 377 | query, err := opts.ToObjectGetQuery() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 378 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 379 | res.Err = err |
| 380 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 381 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 382 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 383 | } |
| 384 | |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 385 | resp, err := c.Request("HEAD", url, gophercloud.RequestOpts{ |
| 386 | OkCodes: []int{200, 204}, |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 387 | }) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 388 | if resp != nil { |
| 389 | res.Header = resp.Header |
| 390 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 391 | res.Err = err |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 392 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 393 | } |
| 394 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 395 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 396 | // Update request. |
| 397 | type UpdateOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 398 | ToObjectUpdateMap() (map[string]string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 399 | } |
| 400 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 401 | // UpdateOpts is a structure that holds parameters for updating, creating, or deleting an |
| 402 | // object's metadata. |
| 403 | type UpdateOpts struct { |
| 404 | Metadata map[string]string |
| 405 | ContentDisposition string `h:"Content-Disposition"` |
| 406 | ContentEncoding string `h:"Content-Encoding"` |
| 407 | ContentType string `h:"Content-Type"` |
| 408 | DeleteAfter int `h:"X-Delete-After"` |
| 409 | DeleteAt int `h:"X-Delete-At"` |
| 410 | DetectContentType bool `h:"X-Detect-Content-Type"` |
| 411 | } |
| 412 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 413 | // ToObjectUpdateMap formats a UpdateOpts into a map of headers. |
| 414 | func (opts UpdateOpts) ToObjectUpdateMap() (map[string]string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 415 | h, err := gophercloud.BuildHeaders(opts) |
| 416 | if err != nil { |
| 417 | return nil, err |
| 418 | } |
| 419 | for k, v := range opts.Metadata { |
| 420 | h["X-Object-Meta-"+k] = v |
| 421 | } |
| 422 | return h, nil |
| 423 | } |
| 424 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 425 | // 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] | 426 | func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts UpdateOptsBuilder) UpdateResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 427 | var res UpdateResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 428 | h := c.AuthenticatedHeaders() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 429 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 430 | if opts != nil { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 431 | headers, err := opts.ToObjectUpdateMap() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 432 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 433 | res.Err = err |
| 434 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 435 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 436 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 437 | for k, v := range headers { |
| 438 | h[k] = v |
| 439 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 440 | } |
| 441 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 442 | url := updateURL(c, containerName, objectName) |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 443 | resp, err := c.Request("POST", url, gophercloud.RequestOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 444 | MoreHeaders: h, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 445 | }) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 446 | if resp != nil { |
| 447 | res.Header = resp.Header |
| 448 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 449 | res.Err = err |
| 450 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 451 | } |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 452 | |
| 453 | // HTTPMethod represents an HTTP method string (e.g. "GET"). |
| 454 | type HTTPMethod string |
| 455 | |
| 456 | var ( |
| 457 | // GET represents an HTTP "GET" method. |
| 458 | GET HTTPMethod = "GET" |
| 459 | // POST represents an HTTP "POST" method. |
| 460 | POST HTTPMethod = "POST" |
| 461 | ) |
| 462 | |
| 463 | // CreateTempURLOpts are options for creating a temporary URL for an object. |
| 464 | type CreateTempURLOpts struct { |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 465 | // (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] | 466 | // are "GET" and "POST". |
| 467 | Method HTTPMethod |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 468 | // (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] | 469 | TTL int |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 470 | // (Optional) Split is the string on which to split the object URL. Since only |
| 471 | // the object path is used in the hash, the object URL needs to be parsed. If |
| 472 | // empty, the default OpenStack URL split point will be used ("/v1/"). |
| 473 | Split string |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // CreateTempURL is a function for creating a temporary URL for an object. It |
| 477 | // allows users to have "GET" or "POST" access to a particular tenant's object |
| 478 | // for a limited amount of time. |
| 479 | func CreateTempURL(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateTempURLOpts) (string, error) { |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 480 | if opts.Split == "" { |
| 481 | opts.Split = "/v1/" |
| 482 | } |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 483 | duration := time.Duration(opts.TTL) * time.Second |
| 484 | expiry := time.Now().Add(duration).Unix() |
| 485 | getHeader, err := accounts.Get(c, nil).Extract() |
| 486 | if err != nil { |
| 487 | return "", err |
| 488 | } |
| 489 | secretKey := []byte(getHeader.TempURLKey) |
| 490 | url := getURL(c, containerName, objectName) |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 491 | splitPath := strings.Split(url, opts.Split) |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 492 | baseURL, objectPath := splitPath[0], splitPath[1] |
Jon Perritt | 28792af | 2015-02-02 11:00:04 -0700 | [diff] [blame] | 493 | objectPath = opts.Split + objectPath |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 494 | body := fmt.Sprintf("%s\n%d\n%s", opts.Method, expiry, objectPath) |
| 495 | hash := hmac.New(sha1.New, secretKey) |
| 496 | hash.Write([]byte(body)) |
| 497 | hexsum := fmt.Sprintf("%x", hash.Sum(nil)) |
| 498 | return fmt.Sprintf("%s%s?temp_url_sig=%s&temp_url_expires=%d", baseURL, objectPath, hexsum, expiry), nil |
| 499 | } |