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