open.go 688 B

123456789101112131415161718192021222324252627282930313233
  1. package s3util
  2. import (
  3. "io"
  4. "net/http"
  5. "time"
  6. )
  7. // Open requests the S3 object at url. An HTTP status other than 200 is
  8. // considered an error.
  9. //
  10. // If c is nil, Open uses DefaultConfig.
  11. func Open(url string, c *Config) (io.ReadCloser, error) {
  12. if c == nil {
  13. c = DefaultConfig
  14. }
  15. // TODO(kr): maybe parallel range fetching
  16. r, _ := http.NewRequest("GET", url, nil)
  17. r.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
  18. c.Sign(r, *c.Keys)
  19. client := c.Client
  20. if client == nil {
  21. client = http.DefaultClient
  22. }
  23. resp, err := client.Do(r)
  24. if err != nil {
  25. return nil, err
  26. }
  27. if resp.StatusCode != 200 {
  28. return nil, newRespError(resp)
  29. }
  30. return resp.Body, nil
  31. }