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