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 | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 13 | // ListOpts is a structure that holds parameters for listing objects. |
| 14 | type ListOpts struct { |
| 15 | Full bool |
| 16 | Limit int `q:"limit"` |
| 17 | Marker string `q:"marker"` |
| 18 | EndMarker string `q:"end_marker"` |
| 19 | Format string `q:"format"` |
| 20 | Prefix string `q:"prefix"` |
| 21 | Delimiter [1]byte `q:"delimiter"` |
| 22 | Path string `q:"path"` |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 23 | } |
| 24 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 25 | // List is a function that retrieves all objects in a container. It also returns the details |
| 26 | // 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] | 27 | // response to the ExtractInfo or ExtractNames function, respectively. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 28 | func List(c *gophercloud.ServiceClient, containerName string, opts ListOpts) pagination.Pager { |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 29 | var headers map[string]string |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 30 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 31 | query, err := gophercloud.BuildQueryString(opts) |
| 32 | if err != nil { |
| 33 | fmt.Printf("Error building query string: %v", err) |
| 34 | return pagination.Pager{Err: err} |
| 35 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 36 | |
| 37 | if !opts.Full { |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 38 | headers = map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"} |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 42 | p := ObjectPage{pagination.MarkerPageBase{LastHTTPResponse: r}} |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 43 | p.MarkerPageBase.Owner = p |
| 44 | return p |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 45 | } |
| 46 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 47 | url := containerURL(c, containerName) + query |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 48 | pager := pagination.NewPager(c, url, createPage) |
| 49 | pager.Headers = headers |
| 50 | return pager |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 51 | } |
| 52 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 53 | // DownloadOpts is a structure that holds parameters for downloading an object. |
| 54 | type DownloadOpts struct { |
| 55 | IfMatch string `h:"If-Match"` |
| 56 | IfModifiedSince time.Time `h:"If-Modified-Since"` |
| 57 | IfNoneMatch string `h:"If-None-Match"` |
| 58 | IfUnmodifiedSince time.Time `h:"If-Unmodified-Since"` |
| 59 | Range string `h:"Range"` |
| 60 | Expires string `q:"expires"` |
| 61 | MultipartManifest string `q:"multipart-manifest"` |
| 62 | Signature string `q:"signature"` |
| 63 | } |
| 64 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 65 | // Download is a function that retrieves the content and metadata for an object. |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 66 | // To extract just the content, pass the DownloadResult response to the ExtractContent |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 67 | // function. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 68 | func Download(c *gophercloud.ServiceClient, containerName, objectName string, opts DownloadOpts) DownloadResult { |
| 69 | var dr DownloadResult |
| 70 | |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 71 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 72 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 73 | headers, err := gophercloud.BuildHeaders(opts) |
| 74 | if err != nil { |
| 75 | dr.Err = err |
| 76 | return dr |
| 77 | } |
| 78 | |
| 79 | for k, v := range headers { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 80 | h[k] = v |
| 81 | } |
| 82 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 83 | query, err := gophercloud.BuildQueryString(opts) |
| 84 | if err != nil { |
| 85 | dr.Err = err |
| 86 | return dr |
| 87 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 88 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 89 | url := objectURL(c, containerName, objectName) + query |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 90 | resp, err := perigee.Request("GET", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 91 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 92 | OkCodes: []int{200}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 93 | }) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 94 | dr.Err = err |
| 95 | dr.Resp = &resp.HttpResponse |
| 96 | return dr |
| 97 | } |
| 98 | |
| 99 | // CreateOpts is a structure that holds parameters for creating an object. |
| 100 | type CreateOpts struct { |
| 101 | Metadata map[string]string |
| 102 | ContentDisposition string `h:"Content-Disposition"` |
| 103 | ContentEncoding string `h:"Content-Encoding"` |
| 104 | ContentLength int `h:"Content-Length"` |
| 105 | ContentType string `h:"Content-Type"` |
| 106 | CopyFrom string `h:"X-Copy-From"` |
| 107 | DeleteAfter int `h:"X-Delete-After"` |
| 108 | DeleteAt int `h:"X-Delete-At"` |
| 109 | DetectContentType string `h:"X-Detect-Content-Type"` |
| 110 | ETag string `h:"ETag"` |
| 111 | IfNoneMatch string `h:"If-None-Match"` |
| 112 | ObjectManifest string `h:"X-Object-Manifest"` |
| 113 | TransferEncoding string `h:"Transfer-Encoding"` |
| 114 | Expires string `q:"expires"` |
| 115 | MultipartManifest string `q:"multiple-manifest"` |
| 116 | Signature string `q:"signature"` |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Create is a function that creates a new object or replaces an existing object. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 120 | func Create(c *gophercloud.ServiceClient, containerName, objectName string, content io.Reader, opts CreateOpts) error { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 121 | var reqBody []byte |
| 122 | |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 123 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 124 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 125 | headers, err := gophercloud.BuildHeaders(opts) |
| 126 | if err != nil { |
| 127 | return nil |
| 128 | } |
| 129 | |
| 130 | for k, v := range headers { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 131 | h[k] = v |
| 132 | } |
| 133 | |
| 134 | for k, v := range opts.Metadata { |
| 135 | h["X-Object-Meta-"+k] = v |
| 136 | } |
| 137 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 138 | query, err := gophercloud.BuildQueryString(opts) |
| 139 | if err != nil { |
| 140 | return err |
| 141 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 142 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 143 | if content != nil { |
Jon Perritt | 884e031 | 2014-08-14 17:25:38 -0500 | [diff] [blame] | 144 | reqBody = make([]byte, 0) |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 145 | _, err := content.Read(reqBody) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 146 | if err != nil { |
| 147 | return err |
| 148 | } |
| 149 | } |
| 150 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 151 | url := objectURL(c, containerName, objectName) + query |
| 152 | _, err = perigee.Request("PUT", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 153 | ReqBody: reqBody, |
| 154 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 155 | OkCodes: []int{201}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 156 | }) |
| 157 | return err |
| 158 | } |
| 159 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 160 | // CopyOpts is a structure that holds parameters for copying one object to another. |
| 161 | type CopyOpts struct { |
| 162 | Metadata map[string]string |
| 163 | ContentDisposition string `h:"Content-Disposition"` |
| 164 | ContentEncoding string `h:"Content-Encoding"` |
| 165 | ContentType string `h:"Content-Type"` |
| 166 | Destination string `h:"Destination,required"` |
| 167 | } |
| 168 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 169 | // Copy is a function that copies one object to another. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 170 | func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts CopyOpts) error { |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 171 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 172 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 173 | headers, err := gophercloud.BuildHeaders(opts) |
| 174 | if err != nil { |
| 175 | return err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 176 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 177 | for k, v := range headers { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 178 | h[k] = v |
| 179 | } |
| 180 | |
| 181 | for k, v := range opts.Metadata { |
| 182 | h["X-Object-Meta-"+k] = v |
| 183 | } |
| 184 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 185 | url := objectURL(c, containerName, objectName) |
| 186 | _, err = perigee.Request("COPY", url, perigee.Options{ |
| 187 | MoreHeaders: h, |
| 188 | OkCodes: []int{201}, |
| 189 | }) |
| 190 | return err |
| 191 | } |
| 192 | |
| 193 | // DeleteOpts is a structure that holds parameters for deleting an object. |
| 194 | type DeleteOpts struct { |
| 195 | MultipartManifest string `q:"multipart-manifest"` |
| 196 | } |
| 197 | |
| 198 | // Delete is a function that deletes an object. |
| 199 | func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts DeleteOpts) error { |
| 200 | h := c.Provider.AuthenticatedHeaders() |
| 201 | |
| 202 | query, err := gophercloud.BuildQueryString(opts) |
| 203 | if err != nil { |
| 204 | return err |
| 205 | } |
| 206 | |
| 207 | url := objectURL(c, containerName, objectName) + query |
| 208 | _, err = perigee.Request("DELETE", url, perigee.Options{ |
| 209 | MoreHeaders: h, |
| 210 | OkCodes: []int{204}, |
| 211 | }) |
| 212 | return err |
| 213 | } |
| 214 | |
| 215 | // GetOpts is a structure that holds parameters for getting an object's metadata. |
| 216 | type GetOpts struct { |
| 217 | Expires string `q:"expires"` |
| 218 | Signature string `q:"signature"` |
| 219 | } |
| 220 | |
| 221 | // Get is a function that retrieves the metadata of an object. To extract just the custom |
| 222 | // metadata, pass the GetResult response to the ExtractMetadata function. |
| 223 | func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts GetOpts) GetResult { |
| 224 | var gr GetResult |
| 225 | query, err := gophercloud.BuildQueryString(opts) |
| 226 | if err != nil { |
| 227 | gr.Err = err |
| 228 | return gr |
| 229 | } |
| 230 | |
| 231 | url := objectURL(c, containerName, objectName) + query |
| 232 | resp, err := perigee.Request("HEAD", url, perigee.Options{ |
| 233 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 234 | OkCodes: []int{200, 204}, |
| 235 | }) |
| 236 | gr.Err = err |
| 237 | gr.Resp = &resp.HttpResponse |
| 238 | return gr |
| 239 | } |
| 240 | |
| 241 | // UpdateOpts is a structure that holds parameters for updating, creating, or deleting an |
| 242 | // object's metadata. |
| 243 | type UpdateOpts struct { |
| 244 | Metadata map[string]string |
| 245 | ContentDisposition string `h:"Content-Disposition"` |
| 246 | ContentEncoding string `h:"Content-Encoding"` |
| 247 | ContentType string `h:"Content-Type"` |
| 248 | DeleteAfter int `h:"X-Delete-After"` |
| 249 | DeleteAt int `h:"X-Delete-At"` |
| 250 | DetectContentType bool `h:"X-Detect-Content-Type"` |
| 251 | } |
| 252 | |
| 253 | // Update is a function that creates, updates, or deletes an object's metadata. |
| 254 | func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts UpdateOpts) error { |
| 255 | h := c.Provider.AuthenticatedHeaders() |
| 256 | |
| 257 | headers, err := gophercloud.BuildHeaders(opts) |
| 258 | if err != nil { |
| 259 | return nil |
| 260 | } |
| 261 | |
| 262 | for k, v := range headers { |
| 263 | h[k] = v |
| 264 | } |
| 265 | |
| 266 | for k, v := range opts.Metadata { |
| 267 | h["X-Object-Meta-"+k] = v |
| 268 | } |
| 269 | |
| 270 | url := objectURL(c, containerName, objectName) |
| 271 | _, err = perigee.Request("POST", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 272 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 273 | OkCodes: []int{202}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 274 | }) |
| 275 | return err |
| 276 | } |