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