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