Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 5 | "io" |
| 6 | "time" |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 7 | |
| 8 | "github.com/racker/perigee" |
| 9 | "github.com/rackspace/gophercloud" |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 11 | ) |
| 12 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 13 | // ListOptsBuilder allows extensions to add additional parameters to the List |
| 14 | // request. |
| 15 | type ListOptsBuilder interface { |
| 16 | ToObjectListParams() (bool, string, error) |
| 17 | } |
| 18 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 19 | // ListOpts is a structure that holds parameters for listing objects. |
| 20 | type ListOpts struct { |
| 21 | Full bool |
| 22 | Limit int `q:"limit"` |
| 23 | Marker string `q:"marker"` |
| 24 | EndMarker string `q:"end_marker"` |
| 25 | Format string `q:"format"` |
| 26 | Prefix string `q:"prefix"` |
| 27 | Delimiter [1]byte `q:"delimiter"` |
| 28 | Path string `q:"path"` |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 29 | } |
| 30 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 31 | // ToObjectListParams formats a ListOpts into a query string and boolean |
| 32 | // representing whether to list complete information for each object. |
| 33 | func (opts ListOpts) ToObjectListParams() (bool, string, error) { |
| 34 | q, err := gophercloud.BuildQueryString(opts) |
| 35 | if err != nil { |
| 36 | return false, "", err |
| 37 | } |
| 38 | return opts.Full, q.String(), nil |
| 39 | } |
| 40 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 41 | // List is a function that retrieves all objects in a container. It also returns the details |
| 42 | // 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] | 43 | // response to the ExtractInfo or ExtractNames function, respectively. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 44 | func List(c *gophercloud.ServiceClient, containerName string, opts ListOptsBuilder) pagination.Pager { |
| 45 | headers := map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"} |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 46 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 47 | url := listURL(c, containerName) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 48 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 49 | full, query, err := opts.ToObjectListParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 50 | if err != nil { |
| 51 | fmt.Printf("Error building query string: %v", err) |
| 52 | return pagination.Pager{Err: err} |
| 53 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 54 | url += query |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 55 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 56 | if full { |
| 57 | headers = map[string]string{"Accept": "application/json", "Content-Type": "application/json"} |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 58 | } |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 62 | p := ObjectPage{pagination.MarkerPageBase{LastHTTPResponse: r}} |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 63 | p.MarkerPageBase.Owner = p |
| 64 | return p |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 67 | pager := pagination.NewPager(c, url, createPage) |
| 68 | pager.Headers = headers |
| 69 | return pager |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 72 | // DownloadOptsBuilder allows extensions to add additional parameters to the |
| 73 | // Download request. |
| 74 | type DownloadOptsBuilder interface { |
| 75 | ToObjectDownloadParams() (map[string]string, string, error) |
| 76 | } |
| 77 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 78 | // DownloadOpts is a structure that holds parameters for downloading an object. |
| 79 | type DownloadOpts struct { |
| 80 | IfMatch string `h:"If-Match"` |
| 81 | IfModifiedSince time.Time `h:"If-Modified-Since"` |
| 82 | IfNoneMatch string `h:"If-None-Match"` |
| 83 | IfUnmodifiedSince time.Time `h:"If-Unmodified-Since"` |
| 84 | Range string `h:"Range"` |
| 85 | Expires string `q:"expires"` |
| 86 | MultipartManifest string `q:"multipart-manifest"` |
| 87 | Signature string `q:"signature"` |
| 88 | } |
| 89 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 90 | // ToObjectDownloadParams formats a DownloadOpts into a query string and map of |
| 91 | // headers. |
| 92 | func (opts ListOpts) ToObjectDownloadParams() (map[string]string, string, error) { |
| 93 | q, err := gophercloud.BuildQueryString(opts) |
| 94 | if err != nil { |
| 95 | return nil, "", err |
| 96 | } |
| 97 | h, err := gophercloud.BuildHeaders(opts) |
| 98 | if err != nil { |
| 99 | return nil, q.String(), err |
| 100 | } |
| 101 | return h, q.String(), nil |
| 102 | } |
| 103 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 104 | // 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] | 105 | // To extract just the content, pass the DownloadResult response to the |
| 106 | // ExtractContent function. |
| 107 | func Download(c *gophercloud.ServiceClient, containerName, objectName string, opts DownloadOptsBuilder) DownloadResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 108 | var res DownloadResult |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 109 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 110 | url := downloadURL(c, containerName, objectName) |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 111 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 112 | |
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 | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 116 | res.Err = err |
| 117 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | for k, v := range headers { |
| 121 | h[k] = v |
| 122 | } |
| 123 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 124 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 127 | resp, err := perigee.Request("GET", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 128 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 129 | OkCodes: []int{200}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 130 | }) |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 131 | res.Err = err |
| 132 | res.Resp = &resp.HttpResponse |
| 133 | return res |
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 { |
| 139 | ToObjectCreateParams() (map[string]string, string, error) |
| 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 { |
| 144 | Metadata map[string]string |
| 145 | ContentDisposition string `h:"Content-Disposition"` |
| 146 | ContentEncoding string `h:"Content-Encoding"` |
| 147 | ContentLength int `h:"Content-Length"` |
| 148 | ContentType string `h:"Content-Type"` |
| 149 | CopyFrom string `h:"X-Copy-From"` |
| 150 | DeleteAfter int `h:"X-Delete-After"` |
| 151 | DeleteAt int `h:"X-Delete-At"` |
| 152 | DetectContentType string `h:"X-Detect-Content-Type"` |
| 153 | ETag string `h:"ETag"` |
| 154 | IfNoneMatch string `h:"If-None-Match"` |
| 155 | ObjectManifest string `h:"X-Object-Manifest"` |
| 156 | TransferEncoding string `h:"Transfer-Encoding"` |
| 157 | Expires string `q:"expires"` |
| 158 | MultipartManifest string `q:"multiple-manifest"` |
| 159 | Signature string `q:"signature"` |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 160 | } |
| 161 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 162 | // ToObjectCreateParams formats a CreateOpts into a query string and map of |
| 163 | // headers. |
| 164 | func (opts CreateOpts) ToObjectCreateParams() (map[string]string, string, error) { |
| 165 | q, err := gophercloud.BuildQueryString(opts) |
| 166 | if err != nil { |
| 167 | return nil, "", err |
| 168 | } |
| 169 | h, err := gophercloud.BuildHeaders(opts) |
| 170 | if err != nil { |
| 171 | return nil, q.String(), err |
| 172 | } |
| 173 | |
| 174 | for k, v := range opts.Metadata { |
| 175 | h["X-Object-Meta-"+k] = v |
| 176 | } |
| 177 | |
| 178 | return h, q.String(), nil |
| 179 | } |
| 180 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 181 | // Create is a function that creates a new object or replaces an existing object. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 182 | func Create(c *gophercloud.ServiceClient, containerName, objectName string, content io.Reader, opts CreateOptsBuilder) CreateResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 183 | var res CreateResult |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 184 | var reqBody []byte |
| 185 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 186 | url := createURL(c, containerName, objectName) |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 187 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 188 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 189 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 190 | headers, query, err := opts.ToObjectCreateParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 191 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 192 | res.Err = err |
| 193 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 194 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 195 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 196 | for k, v := range headers { |
| 197 | h[k] = v |
| 198 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 199 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 200 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 201 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 202 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 203 | if content != nil { |
Jon Perritt | 884e031 | 2014-08-14 17:25:38 -0500 | [diff] [blame] | 204 | reqBody = make([]byte, 0) |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 205 | _, err := content.Read(reqBody) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 206 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 207 | res.Err = err |
| 208 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 212 | resp, err := perigee.Request("PUT", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 213 | ReqBody: reqBody, |
| 214 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 215 | OkCodes: []int{201}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 216 | }) |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 217 | res.Resp = &resp.HttpResponse |
| 218 | res.Err = err |
| 219 | return res |
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 { |
| 225 | ToObjectCopyParams() (map[string]string, error) |
| 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"` |
| 235 | Destination string `h:"Destination,required"` |
| 236 | } |
| 237 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 238 | // ToObjectCopyParams formats a CopyOpts into a map of headers. |
| 239 | func (opts CopyOpts) ToObjectCopyParams() (map[string]string, error) { |
| 240 | if opts.Destination == "" { |
| 241 | return nil, fmt.Errorf("Required CopyOpts field 'Destination' not set.") |
| 242 | } |
| 243 | h, err := gophercloud.BuildHeaders(opts) |
| 244 | if err != nil { |
| 245 | return nil, err |
| 246 | } |
| 247 | for k, v := range opts.Metadata { |
| 248 | h["X-Object-Meta-"+k] = v |
| 249 | } |
| 250 | return h, nil |
| 251 | } |
| 252 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 253 | // Copy is a function that copies one object to another. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 254 | func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts CopyOptsBuilder) CopyResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 255 | var res CopyResult |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 256 | h := c.Provider.AuthenticatedHeaders() |
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 | headers, err := opts.ToObjectCopyParams() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 259 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 260 | res.Err = err |
| 261 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 262 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 263 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 264 | for k, v := range headers { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 265 | h[k] = v |
| 266 | } |
| 267 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 268 | url := copyURL(c, containerName, objectName) |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 269 | resp, err := perigee.Request("COPY", url, perigee.Options{ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 270 | MoreHeaders: h, |
| 271 | OkCodes: []int{201}, |
| 272 | }) |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 273 | res.Resp = &resp.HttpResponse |
| 274 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 275 | } |
| 276 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 277 | // DeleteOptsBuilder allows extensions to add additional parameters to the |
| 278 | // Delete request. |
| 279 | type DeleteOptsBuilder interface { |
| 280 | ToObjectDeleteParams() (string, error) |
| 281 | } |
| 282 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 283 | // DeleteOpts is a structure that holds parameters for deleting an object. |
| 284 | type DeleteOpts struct { |
| 285 | MultipartManifest string `q:"multipart-manifest"` |
| 286 | } |
| 287 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 288 | // ToObjectDeleteParams formats a DeleteOpts into a query string. |
| 289 | func (opts DeleteOpts) ToObjectDeleteParams() (string, error) { |
| 290 | q, err := gophercloud.BuildQueryString(opts) |
| 291 | if err != nil { |
| 292 | return "", err |
| 293 | } |
| 294 | return q.String(), nil |
| 295 | } |
| 296 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 297 | // Delete is a function that deletes an object. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 298 | func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts DeleteOptsBuilder) DeleteResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 299 | var res DeleteResult |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 300 | url := deleteURL(c, containerName, objectName) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 301 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 302 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 303 | query, err := opts.ToObjectDeleteParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 304 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 305 | res.Err = err |
| 306 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 307 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 308 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 309 | } |
| 310 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 311 | resp, err := perigee.Request("DELETE", url, perigee.Options{ |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 312 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 313 | OkCodes: []int{204}, |
| 314 | }) |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 315 | res.Resp = &resp.HttpResponse |
| 316 | res.Err = err |
| 317 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 318 | } |
| 319 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 320 | // GetOptsBuilder allows extensions to add additional parameters to the |
| 321 | // Get request. |
| 322 | type GetOptsBuilder interface { |
| 323 | ToObjectGetParams() (string, error) |
| 324 | } |
| 325 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 326 | // GetOpts is a structure that holds parameters for getting an object's metadata. |
| 327 | type GetOpts struct { |
| 328 | Expires string `q:"expires"` |
| 329 | Signature string `q:"signature"` |
| 330 | } |
| 331 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 332 | // ToObjectGetParams formats a GetOpts into a query string. |
| 333 | func (opts GetOpts) ToObjectGetParams() (string, error) { |
| 334 | q, err := gophercloud.BuildQueryString(opts) |
| 335 | if err != nil { |
| 336 | return "", err |
| 337 | } |
| 338 | return q.String(), nil |
| 339 | } |
| 340 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 341 | // Get is a function that retrieves the metadata of an object. To extract just the custom |
| 342 | // metadata, pass the GetResult response to the ExtractMetadata function. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 343 | func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts GetOptsBuilder) GetResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 344 | var res GetResult |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 345 | url := getURL(c, containerName, objectName) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 346 | |
| 347 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 348 | query, err := opts.ToObjectGetParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 349 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 350 | res.Err = err |
| 351 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 352 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 353 | url += query |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 354 | } |
| 355 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 356 | resp, err := perigee.Request("HEAD", url, perigee.Options{ |
| 357 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 358 | OkCodes: []int{200, 204}, |
| 359 | }) |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 360 | res.Err = err |
| 361 | res.Resp = &resp.HttpResponse |
| 362 | return res |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 363 | } |
| 364 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 365 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 366 | // Update request. |
| 367 | type UpdateOptsBuilder interface { |
| 368 | ToObjectUpdateParams() (map[string]string, error) |
| 369 | } |
| 370 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 371 | // UpdateOpts is a structure that holds parameters for updating, creating, or deleting an |
| 372 | // object's metadata. |
| 373 | type UpdateOpts struct { |
| 374 | Metadata map[string]string |
| 375 | ContentDisposition string `h:"Content-Disposition"` |
| 376 | ContentEncoding string `h:"Content-Encoding"` |
| 377 | ContentType string `h:"Content-Type"` |
| 378 | DeleteAfter int `h:"X-Delete-After"` |
| 379 | DeleteAt int `h:"X-Delete-At"` |
| 380 | DetectContentType bool `h:"X-Detect-Content-Type"` |
| 381 | } |
| 382 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 383 | // ToObjectUpdateParams formats a UpdateOpts into a map of headers. |
| 384 | func (opts UpdateOpts) ToObjectUpdateParams() (map[string]string, error) { |
| 385 | h, err := gophercloud.BuildHeaders(opts) |
| 386 | if err != nil { |
| 387 | return nil, err |
| 388 | } |
| 389 | for k, v := range opts.Metadata { |
| 390 | h["X-Object-Meta-"+k] = v |
| 391 | } |
| 392 | return h, nil |
| 393 | } |
| 394 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 395 | // 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] | 396 | func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts UpdateOptsBuilder) UpdateResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 397 | var res UpdateResult |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 398 | h := c.Provider.AuthenticatedHeaders() |
| 399 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 400 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 401 | headers, err := opts.ToObjectUpdateParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 402 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 403 | res.Err = err |
| 404 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 405 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 406 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 407 | for k, v := range headers { |
| 408 | h[k] = v |
| 409 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 410 | } |
| 411 | |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 412 | url := updateURL(c, containerName, objectName) |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 413 | resp, err := perigee.Request("POST", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 414 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 415 | OkCodes: []int{202}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 416 | }) |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 417 | res.Resp = &resp.HttpResponse |
| 418 | res.Err = err |
| 419 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 420 | } |