func (g *S3Upload) Upload() error { keys := s3.Keys{ AccessKey: g.accessKey, SecretKey: g.secretKey, } urls := fmt.Sprintf("http://%s.s3.amazonaws.com/%s", g.bucket, g.path) body := bytes.NewReader(g.object) req, _ := http.NewRequest("PUT", urls, body) req.ContentLength = int64(body.Len()) req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) req.Header.Set("X-Amz-Acl", "public-read") req.Header.Set("Content-Type", g.getMimetype(g.path)) s3.Sign(req, keys) resp, err := http.DefaultClient.Do(req) if err != nil { return err } defer resp.Body.Close() return nil }
func (backend *S3Backend) Delete(hash string) error { r, err := http.NewRequest("DELETE", backend.key(hash), nil) if err != nil { return err } r.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) ks3.Sign(r, *backend.keys) _, err = http.DefaultClient.Do(r) return err }
func UploadToS3(upload S3Upload, client http.Client) error { url := fmt.Sprintf( "https://%s.s3.amazonaws.com%s", upload.Bucket, upload.Path, ) req, err := http.NewRequest("PUT", url, bytes.NewBuffer(upload.Content)) if err != nil { return err } req.ContentLength = int64(len(upload.Content)) req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) if upload.ACL == "" { req.Header.Set("X-Amz-Acl", S3ACLPublicRead) } else { req.Header.Set("X-Amz-Acl", upload.ACL) } if upload.MimeType != "" { req.Header.Set("Content-Type", upload.MimeType) } // S3 does not support keep alive, and yet does not send "Connection: Close" // header as it should. I *think* in absence of this, go keeps the connection // half open. And one server we found a lot of connections in closed state, // leading to fd leak. req.Header.Set("Connection", "Close") s3.Sign(req, s3keys) res, err := http.DefaultClient.Do(req) if err != nil { return err } if res.StatusCode > 399 { return errors.New( fmt.Sprintf("Upload Failed: %d for %s.", res.StatusCode, url), ) } _, err = io.Copy(ioutil.Discard, res.Body) if err != nil { return err } err = res.Body.Close() if err != nil { return err } log.Println("gutils.S3: Uploaded " + url) return nil }
func (backend *S3Backend) Exists(hash string) (bool, error) { r, err := http.NewRequest("HEAD", backend.key(hash), nil) r.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) ks3.Sign(r, *backend.keys) resp, err := http.DefaultClient.Do(r) if err != nil { return false, fmt.Errorf("S3Backend: error performing HEAD request for blob %v, err:%v", hash, err) } if resp.StatusCode == 200 { return true, nil } return false, nil }
func del(keys s3.Keys, path string) error { req, _ := http.NewRequest("DELETE", path, nil) req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) s3.Sign(req, keys) resp, err := http.DefaultClient.Do(req) if err != nil { return err } resp.Body.Close() if resp.StatusCode >= 300 { return fmt.Errorf("delete %s returned status %d", path, resp.StatusCode) } return nil }
func get(keys s3.Keys, path string) (io.ReadCloser, error) { req, _ := http.NewRequest("GET", path, nil) req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) s3.Sign(req, keys) resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err } if resp.StatusCode != 200 { resp.Body.Close() return nil, fmt.Errorf("get %s returned status %d", path, resp.StatusCode) } return resp.Body, nil }
func (backend *S3Backend) PlainText(hash string) ([]byte, error) { r, err := http.NewRequest("GET", backend.key(hash), nil) r.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) r.Header.Set("Range", fmt.Sprintf("bytes=0-85")) ks3.Sign(r, *backend.keys) resp, err := http.DefaultClient.Do(r) defer resp.Body.Close() res, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("S3Backend: error performing GET range request for blob %v, err:%v", hash, err) } if resp.StatusCode == 206 { return res, nil } return nil, fmt.Errorf("req failed %v: %s", resp.StatusCode, res) }
func s3put(bb *bytes.Buffer, url string) error { r, _ := http.NewRequest("PUT", url, bb) r.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) r.Header.Set("Content-Disposition", "attachment") r.Header.Set("X-Amz-Acl", "public-read") r.Header.Set("Content-Md5", b64md5(bb.Bytes())) s3.Sign(r, s3keys) resp, err := http.DefaultClient.Do(r) if err != nil { return err } if resp.StatusCode != 200 { body, _ := ioutil.ReadAll(resp.Body) return fmt.Errorf("s3 status %v putting %q: %q", resp.Status, url, string(body)) } return nil }
func put(keys s3.Keys, path string, body []byte, header http.Header) error { req, _ := http.NewRequest("PUT", path, bytes.NewReader(body)) req.ContentLength = int64(len(body)) req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) for k, v := range header { req.Header[k] = v } s3.Sign(req, keys) resp, err := http.DefaultClient.Do(req) if err != nil { return err } resp.Body.Close() if resp.StatusCode != 200 { return fmt.Errorf("put %s returned status %d", path, resp.StatusCode) } return nil }
func ExampleSign() { keys := s3.Keys{ AccessKey: os.Getenv("S3_ACCESS_KEY"), SecretKey: os.Getenv("S3_SECRET_KEY"), } data := strings.NewReader("hello, world") r, _ := http.NewRequest("PUT", "https://example.s3.amazonaws.com/foo", data) r.ContentLength = int64(data.Len()) r.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) r.Header.Set("X-Amz-Acl", "public-read") s3.Sign(r, keys) resp, err := http.DefaultClient.Do(r) if err != nil { log.Fatal(err) } fmt.Println(resp.StatusCode) }
func s3put(data []byte, filename string) (sha1 string, err error) { sha1 = string(b32sha1(data)) url := s3url(sha1) r, _ := http.NewRequest("PUT", url, bytes.NewReader(data)) r.ContentLength = int64(len(data)) r.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) r.Header.Set("Content-Disposition", "attachment; filename="+filename) r.Header.Set("X-Amz-Acl", "public-read") r.Header.Set("Content-Md5", string(b16md5(data))) s3.Sign(r, s3keys) resp, err := http.DefaultClient.Do(r) if err != nil { return "", err } if resp.StatusCode != 200 { body, _ := ioutil.ReadAll(resp.Body) return "", fmt.Errorf("s3 status %v putting %q: %q", resp.Status, url, string(body)) } return sha1, nil }
func (i *Image) getImageFromS3(AWSAccess string, AWSSecret string) (err error) { req, _ := http.NewRequest("GET", fmt.Sprintf("https://%v.s3.amazonaws.com/%v%v", i.Bucket, i.Path, i.FileName), nil) req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) req.Header.Set("X-Amz-Acl", "public-read") s3.Sign(req, s3.Keys{ AccessKey: AWSAccess, SecretKey: AWSSecret, }) resp, err := http.DefaultClient.Do(req) if err == nil && resp.StatusCode == http.StatusOK { defer resp.Body.Close() i.Image, err = ioutil.ReadAll(resp.Body) if err != nil { fmt.Println(err) } else if i.Debug { fmt.Println("Retrieved image from from S3") } return nil } else if resp.StatusCode != http.StatusOK { err = errors.New("Error while making request") } return err }