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